首頁>技術>

這篇文章主要介紹了python 如何檢測圖片是否有馬賽克,幫助大家更好的理解和使用python處理圖片,感興趣的朋友可以瞭解下

首先是Canny邊緣檢測,將圖片的邊緣檢測出來,參考部落格https://www.cnblogs.com/techyan1990/p/7291771.html

原理講的很清晰,給原博主一個贊

邊緣檢測之後按照正方形檢索來判定是否是馬賽克內容

原理知曉了之後就很好做了

話說MATLAB轉化為python的過程還是很有趣的。

from PIL import Image

import numpy as np

import math

import warnings

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]))

結果圖可以上一下了

可以看出在一定程度上能夠檢測出馬賽克內容

原圖

邊緣圖案

正方形數量

24
  • BSA-TRITC(10mg/ml) TRITC-BSA 牛血清白蛋白改性標記羅丹明
  • 太陽-地球-月亮運動:python-tkinter來實現