Film defect detection

# - * - Coding: UTF-. 8 - * - 
Import CV2 AS CV
Import numpy AS NP
from matplotlib Import pyplot AS PLT
Import CV2



#
# * pinhole zhenkong Min <1 gate> = 0.6 Min> 1 gate> = 0.8 12 Zhang
# * graining qili 5 Min <1 Max <50 gate <= 0.6 Min <1 Max> 50 gate> = 0.4 5 sheets Optional 0.4
# * hanging injury guashang Max <20 0.4 <gate < = 0.5 20 <Max <40 0.1 <Gate <Max = 0.4> 40 Gate <= 0.2
# * hanging flow gualiu Min <1 Max <50 0.2 <gate <= 0.5 is selected from 0.4





#C: / the Users / Administrator / Desktop / film sample data set / qimoshujuji / hard image segmentation / gualiu /
#guashang qili zhenkong



IMG = cv.imread ( 'C: /Users/Administrator/Desktop/qimoshujujiance/qimoshujuji/hard_images/00.jpg',0)
rows, cols = img.shape
Crow, CCOL = rows // 2, cols // 2



im_copy_max=np.zeros([rows,cols],dtype=float)
def max_filte(x,y,step,image_fiter_max3):
sum_s=[]
for k in range(-int(step/2),int(step/2)+1):
for m in range(-int(step/2),int(step/2)+1):
sum_s.append(image_fiter_max3[x+k][y+m])
sum_s.sort()

# if(max(sum_s)>140):
# value=1
# else:
# value=0
return max(sum_s)

#return sum_s[(int(step*step/2)+1)]
def test(Step,image_fiter_max3):
for i in range(int(Step/2),img.shape[0]-int(Step/2)):
for j in range(int(Step/2),img.shape[1]-int(Step/2)):
im_copy_max[i][j]=max_filte(i,j,Step,image_fiter_max3)
im_copy_max return


F = np.fft.fft2 (IMG)
#F (U, V) transform the frequency domain
Fshift = np.fft.fftshift (F)
# moves to the center of the low frequency image
fshift_ima = np.abs (Fshift)
#A (u, v) = | F (u, v) | seeking frequency amplitude
#magnitude_spectrum * = 20 is np.log (np.abs (Fshift))
magnitude_spectrum * = 20 is np.log (np.abs (Fshift))

row_n, = magnitude_spectrum.shape col_n
magnitude_spectrum_max3 = Test (2, magnitude_spectrum)
magnitude_spectrum_max3 [0] [:] = magnitude_spectrum [0] [:]
magnitude_spectrum_max3 [-rows. 1] [:] = magnitude_spectrum [-rows. 1] [:]
magnitude_spectrum_max3 [: , 0] = magnitude_spectrum [:, 0]
magnitude_spectrum_max3 [:,. 1-cols] = magnitude_spectrum [:,. 1-cols]

# small tensile logarithm L (u, v) = log (A (u, v))
# laplacian_image = cv2.Laplacian (magnitude_spectrum_max3, cv2.CV_64F )

#laplacian_image_max3=test(3,laplacian_image)

#大津阈值分割法

def OTSU_enhance(img_gray, th_begin=0, th_end=256, th_step=1):
assert img_gray.ndim == 2, "must input a gary_img"

max_g = 0
suitable_th = 0
for threshold in range(th_begin, th_end, th_step):
bin_img = img_gray > threshold
bin_img_inv = img_gray <= threshold
fore_pix = np.sum(bin_img)
back_pix = np.sum(bin_img_inv)
if 0 == fore_pix:
break
if 0 == back_pix:
continue

w0 = float(fore_pix) / img_gray.size
u0 = float(np.sum(img_gray * bin_img)) / fore_pix
w1 = float(back_pix) / img_gray.size
u1 = float(np.sum(img_gray * bin_img_inv)) / back_pix
# intra-class variance
g = w0 * w1 * (u0 - u1) * (u0 - u1)
if g > max_g:
max_g = g
suitable_th = threshold
return suitable_th
th2 = OTSU_enhance(magnitude_spectrum_max3, th_begin=0, th_end=256, th_step=1)



MASK=np.zeros([rows,cols],dtype=bool)



def max_filte1(x,y,step):
sum_s=[]
for k in range(-int(step/2),int(step/2)+1):
for m in range(-int(step/2),int(step/2)+1):
sum_s.append(magnitude_spectrum_max3[x+k][y+m])
sum_s.sort()

if(max(sum_s)>th2*2):
value=0
else:
value=1
return value

# return sum_s[(int(step*step/2)+1)]
def test1(Step):
for i in range(int(Step/2),img.shape[0]-int(Step/2)):
for j in range(int(Step/2),img.shape[1]-int(Step/2)):
MASK[i][j]=max_filte1(i,j,Step)



test1(2)


plt.subplot(231),plt.imshow(img, cmap = 'gray')
plt.title('img'), plt.xticks([]), plt.yticks([])
plt.subplot(232),plt.imshow(fshift_ima, cmap = 'gray')
plt.title('fshift_ima'), plt.xticks([]), plt.yticks([])
plt.subplot(233),plt.imshow(magnitude_spectrum, cmap = 'gray')
plt.title('magnitude_spectrum'), plt.xticks([]), plt.yticks([])

#
plt.subplot(234),plt.imshow(magnitude_spectrum_max3, cmap = 'gray')
plt.title('magnitude_spectrum_max3'), plt.xticks([]), plt.yticks([])


# plt.subplot(235),plt.imshow(laplacian_image, cmap = 'gray')
# plt.title('laplacian_image3'), plt.xticks([]), plt.yticks([])


plt.subplot(236),plt.imshow(MASK, cmap = 'gray')
plt.title('MASK'), plt.xticks([]), plt.yticks([])

plt.show()





img_bad = cv.imread('C:/Users/Administrator/Desktop/qimoshujujiance/qimoshujuji/hard_images/00.jpg',0)
rows, cols
img_bad = cv2.resize(img_bad,(cols,rows),interpolation=cv2.INTER_CUBIC)
#im_copy_max=im_copy_max.reshape((rows,cols))
#进行傅里叶变换
f = np.fft.FFT2 (img_bad) fshift1 = np.fft.fftshift (f)
# translation center

# Shrunk conversion
magnitude_spectrum1 * = 20 is np.log (np.abs (fshift1))

Fshift = fshift1 the MASK *

# inverse translation transform
f_ishift = np.fft.ifftshift (Fshift)
# inverse Fourier transform
img_back = np.fft. ifft2 (f_ishift)
# absolute value


img_back = np.abs (img_back)





fshift_image = np.abs (Fshift)



#% find the maximum tone value

bad_Max = max (np.max (img_back, Axis = 0))
#% for the minimum gradation value
bad_Min = min (np.min (img_back, Axis = 0))

RET, Thresh1 = cv2.threshold (img_back, bad_Max * 0.3,255, cv2.THRESH_BINARY)

plt.subplot (321), plt.imshow (img_bad , CMap = 'Gray')
plt.title ( 'the Input img_bad'), plt.xticks ([]), plt.yticks ([])

plt.subplot (322), plt.imshow (magnitude_spectrum1, CMap = 'Gray' )
plt.title ( 'magnitude_spectrum1'), plt.xticks ([]), PLT.yticks([])



plt.subplot (323), plt.imshow (np.abs (Fshift), CMap = 'Gray')
plt.title ( 'np.abs (Fshift)'), plt.xticks ([]), plt.yticks ( [])

plt.subplot (324), plt.imshow (img_back, CMap = 'Gray')
plt.title ( 'img_back)'), plt.xticks ([]), plt.yticks ([])

plt.subplot (325), plt.imshow (Thresh1, CMap = 'Gray')
plt.title ( 'Thresh1)'), plt.xticks ([]), plt.yticks ([])

plt.show ()








#### ################################################## ################### wavelet part
# wavelet denoising
Import pywt
# wavelet
## return (cA, (cH, cV, cD)), respectively, approaching the level of details, vertical details and diagonal details
img_back_Max = max (np.max (img_back, Axis = 0))
########################## ################################################## #### can be used directly in the
#hard data in absolute value than the threshold value 2 is replaced 6, does not replace more than 2
img_back_new=pywt.threshold(img_back,img_back_Max*0.5, "hard",0)
#ret,thresh1=cv2.threshold(img_bad,180,255,cv2.THRESH_BINARY)
plt.subplot(121),plt.imshow(img_back_new, cmap = 'gray')
plt.show()

#####################################################################################













# img_back1 = cv.imread('C:/Users/Administrator/Desktop/10.png',0)
coeffs = pywt.dwt2(img_back, "db4")
cA, (cH, cV, cD) = coeffs
#print (cA)

rows1, cols1 = cA.shape



cA_mean = np.mean(cA)
CH_mean = np.mean(cH)
cV_mean=np.mean(cV)
cD_mean=np.mean(cD)



cH_image=np.zeros([rows1,cols1],dtype=float)
cV_image=np.zeros([rows1,cols1],dtype=float)
cD_image=np.zeros([rows1,cols1],dtype=float)


tw=2

def Variance(Image,newImage,mean,var):
rows, cols = Image.shape

for i in range(0,rows-1):
for j in range(0,cols-1):
a=Image[i][j]
abs_a=np.abs(a)
T_value=np.sqrt(np.abs(a-mean))*tw
# T_value=var*tw
if(abs_a>T_value):
if(a>0):
newImage[i][j]=(abs_a-T_value)
elif(a==0):
newImage[i][j]=0
else:
newImage[i][j]=-(abs_a-T_value)
else:
newImage[i][j]=0

newimage return
cA

# seeking variance

cH_arr_var = np.var (cH)
cV_arr_var = np.var (cV in the)
cD_arr_var = np.var (the cD)
cH_image1 Variance = (cH, cH_image, CH_mean, cH_arr_var)
cV_image1 Variance = (cV in the, cV_image, cV_mean, cV_arr_var)
cD_image1 Variance = (the cD, the CD_image, cD_mean, cD_arr_var)

coeffs = (cA, (cH_image1, cV_image1, cD_image1))
after_idwt_image_ = pywt.idwt2 (coeffs, 'DB4')

######### ####### approximate image as a reconstructed image 0
# cA_NEW = np.zeros (cA.shape, DTYPE = a float)
# coeffs_new = (cA_NEW, (cH_image1, cV_image1, cD_image1))
# = pywt after_idwt_image_new. idwt2 (coeffs_new, 'DB4')
# = # new_images cV_image1 cH_image1 + + cD_image1




Variance
plt.subplot (521), plt.imshow (cA, CMap = 'Gray')
plt.title ( 'cA'), plt.xticks ([]), plt.yticks ([])

plt.subplot (522), plt.imshow (cH, CMAP = 'gray')
plt.title ( 'cH' ), plt.xticks ([]), plt.yticks ([])

plt.subplot (523), plt.imshow (CV, CMAP = 'gray')
plt.title ( 'CV'), plt.xticks ([ ]), plt.yticks ([])

plt.subplot (524), plt.imshow (CD, CMAP = 'gray')
plt.title ( 'CDs'), plt.xticks ([]), plt.yticks ( [])

plt.subplot (525), plt.imshow (after_idwt_image_, CMAP = 'gray')
plt.title ( 'after_idwt_image_'), plt.xticks ([]), plt.yticks ([])


plt.subplot ( 526), plt.imshow (cH_image1, CMAP = 'gray')
plt.title ( 'cH_image1'), plt.xticks ([]), plt.yticks ([])

plt.subplot (527), plt.imshow ( cV_image1, CMAP = 'gray')
plt.title ( 'cV_image1'), plt.xticks ([]), plt.yticks ([])


plt.subplot (528), plt.imshow (cD_image1, CMap = 'Gray')
plt.title ( 'cD_image1'), plt.xticks ([]), plt.yticks ([])


# plt.subplot (529) , plt.imshow (new_images, CMap = 'Gray')
# plt.title ( 'new_images'), plt.xticks ([]), plt.yticks ([])

#
# plt.subplot (529), plt.imshow (after_idwt_image_new, CMap = 'Gray')
# plt.title ( 'after_idwt_image_new'), plt.xticks ([]), plt.yticks ([])

plt.show ()

########### ################################### segmented image

after_idwt_image_












############ ######################################## Next is divided XX image
# Fourier denoised image
img_back
# wavelet transform the image reconstructed



# now binarizes


DEF mean_filte1 (X, Y, STEP, image):
sum_s = []
for K in Range (-int (STEP / 2),int(step/2)+1):
for m in range(-int(step/2),int(step/2)+1):
sum_s.append(image[x+k][y+m])
sum_s.sort()
#mean=np.sum(sum_s,axis=0)/step**2

return max(sum_s)
def test_mean(Step,image,new_iamge):
for i in range(int(Step/2),img.shape[0]-int(Step/2)):
for j in range(int(Step/2),img.shape[1]-int(Step/2)):
new_iamge[i][j]=mean_filte1(i,j,Step,image)
return new_iamge

after_idwt_image_new=np.zeros(after_idwt_image_.shape,dtype=float)
new_iamge=test_mean(5,after_idwt_image_,after_idwt_image_new)

# ret,new_iamge=cv2.threshold(new_iamge,0,255,cv2.THRESH_BINARY_INV+cv2.THRESH_OTSU)
# cv2.imshow('new_iamge',new_iamge)


#%求最大灰度值

Max = max(np.max(new_iamge,axis=0))
#%求最小灰度值
Min =min(np.min(new_iamge,axis=0))

after_idwt_image_new


ret,thresh1_image=cv2.threshold(new_iamge,Max*0.5,255,cv2.THRESH_BINARY)




img_back
##########################################################
new_after_res_mean = np.mean(after_idwt_image_new)*4

ret2,thresh1_image2=cv2.threshold(after_idwt_image_new,new_after_res_mean,255,cv2.THRESH_BINARY)
#########################################################################################
plt.subplot(321),plt.imshow(after_idwt_image_, cmap = 'gray')
plt.title('after_idwt_image_'), plt.xticks([]), plt.yticks([])

plt.subplot(322),plt.imshow(new_iamge, cmap = 'gray')
plt.title ( 'new_iamge'), plt.xticks ([]), plt.yticks ([])

plt.subplot (323), plt.imshow (thresh1_image, CMAP = 'gray')
plt.title ( 'thresh1_image' ), plt.xticks ([]), plt.yticks ([])


plt.subplot (325), plt.imshow (img_back, CMAP = 'gray')
plt.title ( 'img_back'), plt.xticks ([ ]), plt.yticks ([])

plt.subplot (324), plt.imshow (thresh1_image2, CMAP = 'gray')
plt.title ( 'thresh1_image2'), plt.xticks ([]), plt.yticks ( [])

plt.show ()

Guess you like

Origin www.cnblogs.com/shuimuqingyang/p/11605127.html