# -*- coding: utf-8 -*-
import cv2 as cv
import numpy as np
from matplotlib import pyplot as plt
import cv2
#
# *针孔 zhenkong Min<1 gate>=0.6 Min>1 gate>=0.8 12张
# *起粒 qili 5 Min<1 Max<50 gate<=0.6 Min<1 Max>50 gate>=0.4 5张 选 0.4
# *挂伤 guashang Max<20 0.4<gate<=0.5 20<Max<40 0.1<gate<=0.4 Max>40 gate<=0.2
# *挂流 gualiu Min<1 Max<50 0.2<gate<=0.5 选0.4
#C:/Users/Administrator/Desktop/漆膜数据集样本/qimoshujuji/困难的分割图片/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)
return im_copy_max
f = np.fft.fft2(img)
#F(u,v)频域变换
fshift = np.fft.fftshift(f)
#将低频移动到图像的中心
fshift_ima=np.abs(fshift)
#A(u,v)=|F(u,v)|求频幅
#magnitude_spectrum = 20*np.log(np.abs(fshift))
magnitude_spectrum = 20*np.log(np.abs(fshift))
row_n,col_n=magnitude_spectrum.shape
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[:,cols-1]=magnitude_spectrum[:,cols-1]
#拉伸小取对数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)
#进行收缩变换
magnitude_spectrum1 = 20*np.log(np.abs(fshift1))
fshift=fshift1*MASK
#平移逆变换
f_ishift = np.fft.ifftshift(fshift)
#傅里叶反变换
img_back = np.fft.ifft2(f_ishift)
# 取绝对值
img_back = np.abs(img_back)
fshift_image=np.abs(fshift)
#%求最大灰度值
bad_Max = max(np.max(img_back,axis=0))
#%求最小灰度值
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('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()
#求最小外接矩形image = np.array(image,np.uint8)
img_back2 = np.array(img_back,np.uint8)
ret2,thresh2=cv2.threshold(img_back2 ,bad_Max*0.3,255,cv2.THRESH_BINARY )
image2,contours2,hier2=cv2.findContours(thresh2,2,1)
for c in contours2:
# find bounding box coordinates
x,y,w,h = cv2.boundingRect(c)
cv2.rectangle(img, (x,y), (x+w, y+h), (0, 255, 0), 2)
# find minimum area
rect = cv2.minAreaRect(c)
# calculate coordinates of the minimum area rectangle
box = cv2.boxPoints(rect)
# normalize coordinates to integers
box = np.int0(box)
# draw contours
cv2.drawContours(img, [box], 0, (0,0, 255), 3)
# calculate center and radius of minimum enclosing circle
(x,y),radius = cv2.minEnclosingCircle(c)
# cast to integers
center = (int(x),int(y))
radius = int(radius)*2
# draw the circle
img = cv2.circle(img,center,radius,(0,255,0),2)
cv2.drawContours(img, contours2, -1, (255, 0, 0), 1)
cv2.imshow("contours", img)
cv2.waitKey()