gpt4 book ai didi

python - 使用openCV2去除水平条纹

转载 作者:行者123 更新时间:2023-12-02 16:16:35 25 4
gpt4 key购买 nike

sample image

我是openCV的新手,我想知道是否有办法消除此图像下半部分的周期性条纹。

我看了这篇文章,但不知道发生了什么:Removing periodic noise from an image using the Fourier Transform

最佳答案

这是使用傅立叶变换和使用Python / OpenCV / Numpy进行陷波过滤处理来减轻(减少但不能完全消除)线条的方法。由于输入中的水平线非常接近,因此在傅立叶变换频谱中将有水平的线性结构相距很远。所以我做的是:

  • 读取输入的
  • 将平均值填充为2的幂次方(以尝试减轻填充不连续性引起的振铃)
  • 执行DFT
  • 根据幅值
  • 计算频谱图像
  • 阈值图像,并在中心绘制一条黑色水平线,以消除明亮的直流分量
  • 查找亮点(线条)显示的位置。
  • 获取亮点的坐标并在阈值图像上绘制白色水平线以形成蒙版
  • 将蒙版应用于幅值图像
  • 执行IDFT
  • 裁切大小并标准化为与原始图像相同的动态范围

  • 输入:

    enter image description here
    import numpy as np
    import cv2
    import math

    # read input as grayscale
    img = cv2.imread('pattern_lines.png', 0)
    hh, ww = img.shape

    # get min and max and mean values of img
    img_min = np.amin(img)
    img_max = np.amax(img)
    img_mean = int(np.mean(img))

    # pad the image to dimension a power of 2
    hhh = math.ceil(math.log2(hh))
    hhh = int(math.pow(2,hhh))
    www = math.ceil(math.log2(ww))
    www = int(math.pow(2,www))
    imgp = np.full((hhh,www), img_mean, dtype=np.uint8)
    imgp[0:hh, 0:ww] = img

    # convert image to floats and do dft saving as complex output
    dft = cv2.dft(np.float32(imgp), flags = cv2.DFT_COMPLEX_OUTPUT)

    # apply shift of origin from upper left corner to center of image
    dft_shift = np.fft.fftshift(dft)

    # extract magnitude and phase images
    mag, phase = cv2.cartToPolar(dft_shift[:,:,0], dft_shift[:,:,1])

    # get spectrum
    spec = np.log(mag) / 20
    min, max = np.amin(spec, (0,1)), np.amax(spec, (0,1))

    # threshold the spectrum to find bright spots
    thresh = (255*spec).astype(np.uint8)
    thresh = cv2.threshold(thresh, 155, 255, cv2.THRESH_BINARY)[1]

    # cover the center rows of thresh with black
    yc = hhh // 2
    cv2.line(thresh, (0,yc), (www-1,yc), 0, 5)

    # get the y coordinates of the bright spots
    points = np.column_stack(np.nonzero(thresh))
    print(points)

    # create mask from spectrum drawing horizontal lines at bright spots
    mask = thresh.copy()
    for p in points:
    y = p[0]
    cv2.line(mask, (0,y), (www-1,y), 255, 5)

    # apply mask to magnitude such that magnitude is made black where mask is white
    mag[mask!=0] = 0

    # convert new magnitude and old phase into cartesian real and imaginary components
    real, imag = cv2.polarToCart(mag, phase)

    # combine cartesian components into one complex image
    back = cv2.merge([real, imag])

    # shift origin from center to upper left corner
    back_ishift = np.fft.ifftshift(back)

    # do idft saving as complex output
    img_back = cv2.idft(back_ishift)

    # combine complex components into original image again
    img_back = cv2.magnitude(img_back[:,:,0], img_back[:,:,1])

    # crop to original size
    img_back = img_back[0:hh, 0:ww]

    # re-normalize to 8-bits in range of original
    min, max = np.amin(img_back, (0,1)), np.amax(img_back, (0,1))
    notched = cv2.normalize(img_back, None, alpha=img_min, beta=img_max, norm_type=cv2.NORM_MINMAX, dtype=cv2.CV_8U)

    cv2.imshow("ORIGINAL", img)
    cv2.imshow("PADDED", imgp)
    cv2.imshow("MAG", mag)
    cv2.imshow("PHASE", phase)
    cv2.imshow("SPECTRUM", spec)
    cv2.imshow("THRESH", thresh)
    cv2.imshow("MASK", mask)
    cv2.imshow("NOTCHED", notched)
    cv2.waitKey(0)
    cv2.destroyAllWindows()

    # write result to disk
    cv2.imwrite("pattern_lines_spectrum.png", (255*spec).clip(0,255).astype(np.uint8))
    cv2.imwrite("pattern_lines_thresh.png", thresh)
    cv2.imwrite("pattern_lines_mask.png", mask)
    cv2.imwrite("pattern_lines_notched.png", notched)

    光谱(注意中间的亮点,y = 64和192):

    enter image description here

    阈值图片:

    enter image description here

    亮点位置:
    [[   0 1023]
    [ 0 1024]
    [ 0 1025]
    [ 1 1024]
    [ 64 1024]
    [ 65 1024]
    [ 191 1024]
    [ 192 1024]
    [ 255 1024]]

    面具:

    enter image description here

    结果:

    enter image description here

    关于python - 使用openCV2去除水平条纹,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60506244/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com