python 检测图片是否有马赛克


Posted in Python onDecember 01, 2020

首先是Canny边缘检测,将图片的边缘检测出来,参考博客https://www.cnblogs.com/techyan1990/p/7291771.html

原理讲的很清晰,给原博主一个赞

边缘检测之后按照正方形检索来判定是否是马赛克内容

原理知晓了之后就很好做了

话说MATLAB转化为python的过程还是很有趣的

from PIL import Image
import numpy as np
import math
import warnings

#算法来源,博客https://www.cnblogs.com/techyan1990/p/7291771.html和https://blog.csdn.net/zhancf/article/details/49736823
highhold=200#高阈值
lowhold=40#低阈值
warnings.filterwarnings("ignore")
demo=Image.open("noise_check//23.jpg")
im=np.array(demo.convert('L'))#灰度化矩阵
print(im.shape)
print(im.dtype)
height=im.shape[0]#尺寸
width=im.shape[1]
gm=[[0 for i in range(width)]for j in range(height)]#梯度强度
gx=[[0 for i in range(width)]for j in range(height)]#梯度x
gy=[[0 for i in range(width)]for j in range(height)]#梯度y
theta=0#梯度方向角度360度
dirr=[[0 for i in range(width)]for j in range(height)]#0,1,2,3方位判定值
highorlow=[[0 for i in range(width)]for j in range(height)]#强边缘、弱边缘、忽略判定值2,1,0
rm=np.array([[0 for i in range(width)]for j in range(height)])#输出矩阵
#高斯滤波平滑,3x3
for i in range(1,height-1,1):
 for j in range(1,width-1,1):
 rm[i][j]=im[i-1][j-1]*0.0924+im[i-1][j]*0.1192+im[i-1][j+1]*0.0924+im[i][j-1]*0.1192+im[i][j]*0.1538+im[i][j+1]*0.1192+im[i+1][j-1]*0.0924+im[i+1][j]*0.1192+im[i+1][j+1]*0.0924
for i in range(1,height-1,1):#梯度强度和方向
 for j in range(1,width-1,1):
 gx[i][j]=-rm[i-1][j-1]+rm[i-1][j+1]-2*rm[i][j-1]+2*rm[i][j+1]-rm[i+1][j-1]+rm[i+1][j+1]
 gy[i][j]=rm[i-1][j-1]+2*rm[i-1][j]+rm[i-1][j+1]-rm[i+1][j-1]-2*rm[i+1][j]-rm[i+1][j+1]
 gm[i][j]=pow(gx[i][j]*gx[i][j]+gy[i][j]*gy[i][j],0.5)
 theta=math.atan(gy[i][j]/gx[i][j])*180/3.1415926
 if theta>=0 and theta<45:
  dirr[i][j]=2
 elif theta>=45 and theta<90:
  dirr[i][j]=3
 elif theta>=90 and theta<135:
  dirr[i][j]=0
 else:
  dirr[i][j]=1
for i in range(1,height-1,1):#非极大值抑制,双阈值监测
 for j in range(1,width-1,1):
 NW=gm[i-1][j-1]
 N=gm[i-1][j]
 NE=gm[i-1][j+1]
 W=gm[i][j-1]
 E=gm[i][j+1]
 SW=gm[i+1][j-1]
 S=gm[i+1][j]
 SE=gm[i+1][j+1]
 if dirr[i][j]==0:
  d=abs(gy[i][j]/gx[i][j])
  gp1=(1-d)*E+d*NE
  gp2=(1-d)*W+d*SW
 elif dirr[i][j]==1:
  d=abs(gx[i][j]/gy[i][j])
  gp1=(1-d)*N+d*NE
  gp2=(1-d)*S+d*SW
 elif dirr[i][j]==2:
  d=abs(gx[i][j]/gy[i][j])
  gp1=(1-d)*N+d*NW
  gp2=(1-d)*S+d*SE
 elif dirr[i][j]==3:
  d=abs(gy[i][j]/gx[i][j])
  gp1=(1-d)*W+d*NW
  gp2=(1-d)*E+d*SE
 if gm[i][j]>=gp1 and gm[i][j]>=gp2:
  if gm[i][j]>=highhold:
  highorlow[i][j]=2
  rm[i][j]=1
  elif gm[i][j]>=lowhold:
  highorlow[i][j]=1
  else:
  highorlow[i][j]=0
  rm[i][j]=0
 else:
  highorlow[i][j]=0
  rm[i][j]=0
for i in range(1,height-1,1):#抑制孤立低阈值点
 for j in range(1,width-1,1):
 if highorlow[i][j]==1 and (highorlow[i-1][j-1]==2 or highorlow[i-1][j]==2 or highorlow[i-1][j+1]==2 or highorlow[i][j-1]==2 or highorlow[i][j+1]==2 or highorlow[i+1][j-1]==2 or highorlow[i+1][j]==2 or highorlow[i+1][j+1]==2):
  #highorlow[i][j]=2
  rm[i][j]=1
#img=Image.fromarray(rm)#矩阵化为图片
#img.show()
#正方形法判定是否有马赛克
value=35
lowvalue=16
imgnumber=[0 for i in range(value)]
for i in range(1,height-1,1):#性价比高的8点判定法
 for j in range(1,width-1,1):
 for k in range(lowvalue,value):
  count=0
  if i+k-1>=height or j+k-1>=width:continue
  if rm[i][j]!=0:count+=1#4个顶点
  if rm[i+k-1][j]!=0:count+=1
  if rm[i][j+k-1]!=0:count+=1
  if rm[i+k-1][j+k-1]!=0:count+=1
  e=(k-1)//2
  if rm[i+e][j]!=0:count+=1
  if rm[i][j+e]!=0:count+=1
  if rm[i+e][j+k-1]!=0:count+=1
  if rm[i+k-1][j+e]!=0:count+=1
  if count>=6:
  imgnumber[k]+=1
for i in range(lowvalue,value):
 print("length:{} number:{}".format(i,imgnumber[i]))

结果图可以上一下了

可以看出在一定程度上能够检测出马赛克内容

原图

python 检测图片是否有马赛克

边缘图案

python 检测图片是否有马赛克

正方形数量

python 检测图片是否有马赛克

以上就是python 检测图片是否有马赛克的详细内容,更多关于python 检测图片马赛克的资料请关注三水点靠木其它相关文章!

Python 相关文章推荐
在Python中使用SimpleParse模块进行解析的教程
Apr 11 Python
python制作爬虫并将抓取结果保存到excel中
Apr 06 Python
Python利用递归和walk()遍历目录文件的方法示例
Jul 14 Python
python+selenium实现登录账户后自动点击的示例
Dec 22 Python
一个月入门Python爬虫学习,轻松爬取大规模数据
Jan 03 Python
使用python 写一个静态服务(实战)
Jun 28 Python
django 控制页面跳转的例子
Aug 06 Python
Pandas 缺失数据处理的实现
Nov 04 Python
Python3直接爬取图片URL并保存示例
Dec 18 Python
win10从零安装配置pytorch全过程图文详解
May 08 Python
PyCharm MySQL可视化Database配置过程图解
Jun 09 Python
Python用tkinter实现自定义记事本的方法详解
Mar 31 Python
python中pop()函数的语法与实例
Dec 01 #Python
python爬虫多次请求超时的几种重试方法(6种)
Dec 01 #Python
python爬虫搭配起Bilibili唧唧的流程分析
Dec 01 #Python
python爬虫看看虎牙女主播中谁最“顶”步骤详解
Dec 01 #Python
详解Django自定义图片和文件上传路径(upload_to)的2种方式
Dec 01 #Python
使用python爬取抖音app视频的实例代码
Dec 01 #Python
基于Python实现粒子滤波效果
Dec 01 #Python
You might like
php以fastCGI的方式运行时文件系统权限问题及解决方法
2015/05/11 PHP
js 函数的执行环境和作用域链的深入解析
2009/11/01 Javascript
jquery.ui.progressbar 中文文档
2009/11/26 Javascript
在iframe里的页面编写js,实现在父窗口上创建动画效果展开和收缩的div(不变动iframe父窗口代码)
2011/12/20 Javascript
JavaScript中的eval()函数详解
2013/08/22 Javascript
jQuery的animate函数学习记录
2014/08/08 Javascript
基于jquery的手风琴图片展示效果实现方法
2014/12/16 Javascript
微信小程序 wx.request(OBJECT)发起请求详解
2016/10/13 Javascript
Javascript 对cookie操作详解及实例
2016/12/29 Javascript
Bootstrap 手风琴菜单的实现代码
2017/01/20 Javascript
jQuery实现的form转json经典示例
2017/10/10 jQuery
讲解vue-router之什么是动态路由
2018/05/28 Javascript
解决微信小程序中转换时间格式IOS不兼容的问题
2019/02/15 Javascript
微信小程序修改数组长度的问题的解决
2019/12/17 Javascript
关于vue3默认把所有onSomething当作v-on事件绑定的思考
2020/05/15 Javascript
浅谈vue生命周期共有几个阶段?分别是什么?
2020/08/07 Javascript
在Vue中使用mockjs代码实例
2020/11/25 Vue.js
[36:45]TNC vs VGJ.S 2018国际邀请赛小组赛BO2 第二场 8.18
2018/08/19 DOTA
[01:00:13]完美世界DOTA2联赛 LBZS vs Forest 第一场 11.07
2020/11/09 DOTA
用Python写的图片蜘蛛人代码
2012/08/27 Python
python flask实现分页的示例代码
2018/08/02 Python
详解Python数据分析--Pandas知识点
2019/03/23 Python
python打开windows应用程序的实例
2019/06/28 Python
Python中栈、队列与优先级队列的实现方法
2019/06/30 Python
解决Python列表字符不区分大小写的问题
2019/12/19 Python
优秀管理者获奖感言
2014/02/17 职场文书
银行金融服务方案
2014/06/11 职场文书
入党积极分子考察意见
2015/06/02 职场文书
捐书仪式主持词
2015/07/04 职场文书
运动会通讯稿300字
2015/07/20 职场文书
2016年教师节贺卡寄语
2015/12/04 职场文书
党风廉政建设心得体会(2016最新版)
2016/01/22 职场文书
《月光曲》教学反思
2016/02/16 职场文书
CSS 新特性 contain控制页面的重绘与重排问题
2021/04/30 HTML / CSS
go语言基础 seek光标位置os包的使用
2021/05/09 Golang
Python简易开发之制作计算器
2022/04/28 Python