我在频域进行高通滤波之后,进行傅里叶逆变换获得轮廓,再进行了闭运算的操作,将输出图像作为查找轮廓的输入,结果提示报错,网上查阅说是因为输入图像没有进行灰度处理,但是读取的时候我已经灰度处理了。代码如下
import cv2 import cv2 as cv import numpy as np import matplotlib.pyplot as plt img = cv.imread("1.png",0) #傅里叶变换 ft = np.fft.fft2(img) cft = np.fft.fftshift(ft) res = 20*np.log(np.abs(cft)) plt.subplot(111) plt.imshow(res,'gray') plt.title('center') plt.show() r, c = img.shape cr, cc = int(r/2), int(c/2) cft[cr-30:cr+30,cc-30:cc+30] = 1 high = 20*np.log(np.abs(cft)) plt.subplot(111) plt.imshow(high,"gray") plt.title('high') plt.show() #反变换 fshift = np.fft.ifftshift(cft) backimg = np.fft.ifft2(fshift) backimg = np.abs(backimg) plt.imshow(backimg) plt.title("backimg") plt.show() plt.hist(backimg.ravel(),256,[0,256]) plt.show() ret,th1 = cv.threshold(backimg,15,255,cv.THRESH_BINARY) cv.imshow("win",th1) kernel = cv.getStructuringElement(cv.MORPH_ELLIPSE, (20, 20)) opening = cv.morphologyEx(th1, cv.MORPH_CLOSE, kernel) cv.imshow("openning",opening) contours, hierarchy = cv.findContours(opening,cv.RETR_TREE,cv.CHAIN_APPROX_SIMPLE) cv.waitKey(0) l =len(contours) print(l)