【问题标题】:Opencv: detecting brightest line on FFTOpencv:在FFT上检测最亮的线
【发布时间】:2020-08-11 17:17:28
【问题描述】:

我的图像有一个快速傅立叶变换,如下所示:

我想使用边缘检测和霍夫变换来获得这条明亮的水平线(我正在测量图像的旋转角度)。但是 Canny 算子效果不好,因为有这么小的颜色变化。我怎样才能检测到那条线?

我这样生成 FFT:

dft = cv2.dft(frame, flags=cv2.DFT_COMPLEX_OUTPUT)
dft = np.fft.fftshift(dft)
spectrum = 20 * np.log(cv2.magnitude(dft[:, :, 0], dft[:, :, 1]))
# Converting to uint8 for Canny and HoughLinesP
spectrum = spectrum / np.max(spectrum) * 255
spectrum = spectrum.astype(np.uint8)

【问题讨论】:

    标签: python opencv edge-detection


    【解决方案1】:

    这是 Python/OpenCV 中的一种方式。

    • 读取输入
    • 转换为灰色并反转
    • 应用自适应阈值和反转
    • 应用形态学清理阈值并填写线条
    • 应用 Canny 边缘检测
    • 应用霍夫线检测
    • 画出最大的线
    • 保存结果

    输入:

    import cv2
    import numpy as np
    
    # read image
    img = cv2.imread('fft.png')
    
    # convert to grayscale
    gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
    gray = 255 - gray
    
    # threshold
    thresh = cv2.adaptiveThreshold(gray, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY, 13, 3)
    thresh = 255 - thresh
    
    # apply close to connect the white areas
    kernel = np.ones((3,3), np.uint8)
    morph = cv2.morphologyEx(thresh, cv2.MORPH_OPEN, kernel)
    kernel = np.ones((1,9), np.uint8)
    morph = cv2.morphologyEx(morph, cv2.MORPH_CLOSE, kernel)
    
    # apply canny edge detection
    edges = cv2.Canny(morph, 150, 200)
    
    # get hough lines
    result = img.copy()
    lines = cv2.HoughLines(edges, 1, np.pi/180, 50)
    # Draw line on the image
    for rho,theta in lines[0]:
        a = np.cos(theta)
        b = np.sin(theta)
        x0 = a*rho
        y0 = b*rho
        x1 = int(x0 + 1000*(-b))
        y1 = int(y0 + 1000*(a))
        x2 = int(x0 - 1000*(-b))
        y2 = int(y0 - 1000*(a))
        cv2.line(result, (x1, y1), (x2, y2), (0, 0, 255), 1)
    
    # save resulting images
    cv2.imwrite('fft_thresh.jpg',thresh)
    cv2.imwrite('fft_morph.jpg',morph)
    cv2.imwrite('fft_edges.jpg',edges)
    cv2.imwrite('fft_line.jpg',result)
    
    # show thresh and result    
    cv2.imshow("thresh", thresh)
    cv2.imshow("morph", morph)
    cv2.imshow("edges", edges)
    cv2.imshow("result", result)
    cv2.waitKey(0)
    cv2.destroyAllWindows()
    


    阈值图像:

    形态清洁图像:

    边缘图像:

    在输入图像上绘制的结果霍夫线:

    【讨论】:

      猜你喜欢
      • 2016-08-21
      • 2021-05-21
      • 2015-05-25
      • 2023-03-10
      • 1970-01-01
      • 2012-11-11
      • 2017-10-31
      • 2017-06-28
      • 1970-01-01
      相关资源
      最近更新 更多