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和OpenCV库将URL转换为OpenCV格式的方法
Mar 27 Python
Python用Bottle轻量级框架进行Web开发
Jun 08 Python
Python实现中文数字转换为阿拉伯数字的方法示例
May 26 Python
Python环境搭建之OpenCV的步骤方法
Oct 20 Python
在Python中将函数作为另一个函数的参数传入并调用的方法
Jan 22 Python
Python 隐藏输入密码时屏幕回显的实例
Feb 19 Python
centos7之Python3.74安装教程
Aug 15 Python
安装2019Pycharm最新版本的教程详解
Oct 22 Python
提升python处理速度原理及方法实例
Dec 25 Python
Python栈的实现方法示例【列表、单链表】
Feb 22 Python
如何基于Python实现word文档重新排版
Sep 29 Python
写一个Python脚本自动爬取Bilibili小视频
Apr 24 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
用 PHP5 轻松解析 XML
2006/12/04 PHP
PHP 日期加减的类,很不错
2009/10/10 PHP
php字符串函数学习之substr()
2015/03/27 PHP
PHP获取文本框、密码域、按钮的值实例代码
2017/04/19 PHP
Laravel实现定时任务的示例代码
2017/08/10 PHP
了解jQuery技巧来提高你的代码(个人觉得那个jquery的手册很不错)
2012/02/10 Javascript
js常用系统函数用法实例分析
2015/01/12 Javascript
jQuery的事件委托实例分析
2015/07/15 Javascript
IE下JS保存图片的简单实例
2016/07/15 Javascript
jQuery实现鼠标经过购物车出现下拉框代码(推荐)
2016/07/21 Javascript
解决给dom元素绑定click等事件无效问题的方法
2017/02/17 Javascript
jQuery实现腾讯信用界面(自制刻度尺)样式
2017/08/15 jQuery
nodejs中art-template模板语法的引入及冲突解决方案
2017/11/07 NodeJs
js实现复制功能(多种方法集合)
2018/01/06 Javascript
Angular使用动态加载组件方法实现Dialog的示例
2018/05/11 Javascript
大转盘抽奖小程序版 转盘抽奖网页版
2020/04/16 Javascript
使用JavaScript获取扫码枪扫描得到的条形码的思路代码详解
2020/06/10 Javascript
JS实现小米轮播图
2020/09/21 Javascript
[02:21]2018完美盛典章节片——初心
2018/12/17 DOTA
使用Python脚本在Linux下实现部分Bash Shell的教程
2015/04/17 Python
Django中login_required装饰器的深入介绍
2017/11/24 Python
Python实现的根据文件名查找数据文件功能示例
2018/05/02 Python
python接口自动化测试之接口数据依赖的实现方法
2019/04/26 Python
关于Keras Dense层整理
2020/05/21 Python
浅析Python requests 模块
2020/10/09 Python
Python pip 常用命令汇总
2020/10/19 Python
python实现简单猜单词游戏
2020/12/24 Python
巴黎一票通:The Paris Pass
2018/02/10 全球购物
护理职业应聘自荐书
2013/09/29 职场文书
亲子拓展活动方案
2014/02/20 职场文书
小学生自我评价100字(15篇)
2014/09/18 职场文书
个人授权委托书范文
2014/09/21 职场文书
出差报告范文
2014/11/06 职场文书
2014年初中班主任工作总结
2014/11/08 职场文书
分析mysql中一条SQL查询语句是如何执行的
2021/06/21 MySQL
Vue鼠标滚轮滚动切换路由效果的实现方法
2021/08/04 Vue.js