def main(): filterInstance = filter.SimpleMedianFilter() img = mytools.read("moon.tif") cv2filter = filterInstance.apply(img) myapply = filterInstance.myapply(img) diff = numpy.abs(myapply - cv2filter) mytools.drawlocalImage([img, cv2filter, myapply, diff], locals()) pass
print("row" + str(counters + 1) + "of" + str(image.shape[0]) + "...") horizonDFT[counters] = singleRowDft(row) counters += 1 counters = 0 for col in horizonDFT.T: print("\t" + "row" + str(counters + 1) + "of" + str(image.shape[0]) + "...") verticalAndHorizonDft.T[counters] = singleRowDft(col) counters += 1 return verticalAndHorizonDft if __name__ == "__main__": srcImg = tool.read("car.jpg") srcImg = cv2.resize(srcImg, (100, 100)) # my频率化图像 mydft = myDFT(srcImg) #格式化显示 mydft_Draw = numpy.log(numpy.abs(numpy.fft.fftshift(mydft))) #numpy 频率化图像 npDft = numpy.fft.fft2(srcImg) npdft_Draw = numpy.log(numpy.abs(numpy.fft.fftshift(npDft))) diff = (npdft_Draw - mydft_Draw) plt.subplot(231), plt.imshow(srcImg, 'gray'),
for i in range(rows): for j in range(cols): dis = math.sqrt((i - rows / 2)**2 + (j - cols / 2)**2) if (dis <= d0 or dis >= d1): h = 0 else: h = 1 dstfftl[i, j] = h * sfftl[i, j] maskwidth = 10 crow, ccol = int(rows / 2), int(cols / 2) mask = numpy.ones((rows, cols), numpy.uint8) mask[0:rows, ccol - maskwidth:ccol + maskwidth] = 0 mask[crow - maskwidth:crow + maskwidth, 0:cols] = 0 cv2.imshow("sddd", mask * 244) return dstfftl * mask # 带通滤波 高频和低频不通过 中频通过 if __name__ == "__main__": filterInstance = BandPassFilter() img = mytools.read("moon.tif") img = cv2.resize(img, (400, 400)) # 三个图像分别是 归一化的 源图像傅里叶变换, 叠加滤波器的傅里叶变化, 和目标图像 sfftl, dstsfftl, dst = filterInstance.apply(img) mytools.drawlocalImage([img, sfftl, dstsfftl, dst, img - dst], locals()) pass
def histogramEqualize(image, maxIntensity): rows, cols = image.shape # get cdf from image cdf, binCenters = exposure.cumulative_distribution(image, maxIntensity) binCenters = binCenters.tolist() for i in range(rows): for j in range(cols): try: probability = cdf[binCenters.index(image[i][j])] except: probability = 1 image[i][j] = int(probability * maxIntensity) return image, binCenters, cdf if __name__ == "__main__": image = tool.read("sportscar.jpg") cv2.imshow("image", image) maxIntensity = 255 # 8 bits trans, binCenters, cdf = histogramEqualize(image, maxIntensity) cv2.imshow("trans", trans) cv2.waitKey(0)