【问题标题】:plot hough space in python with opencv?用opencv在python中绘制霍夫空间?
【发布时间】:2014-08-08 12:25:10
【问题描述】:

我正在使用 Python 在 OpenCV 中工作。

我在图像上使用了霍夫线变换,结果成功得到了一些线。

现在我想看看霍夫空间中的结果,以便更好地了解这些行有多少票以及它们在哪里。谁能帮我解决这个问题?

【问题讨论】:

    标签: python opencv hough-transform


    【解决方案1】:

    其实在 ximgproc 模块中有一个函数。您需要使用此 contrib 模块重新编译 OpenCV,请参阅 https://github.com/opencv/opencv_contrib

    那么你可以使用的函数是cv::ximgproc::FastHoughTransformhttps://docs.opencv.org/3.4.1/d9/d29/namespacecv_1_1ximgproc.html#a401697bbdcf6c4a4c7d385beda75e849

    【讨论】:

      【解决方案2】:

      我在 Github 中找到了一个计算它的存储库,我对其进行了调整以仅显示霍夫空间。

      Github 参考:https://github.com/alyssaq/hough_transform

      改编代码如下:

      import numpy as np
      import imageio
      import math
      import matplotlib.pyplot as plt
      
      def rgb2gray(rgb):
          return np.dot(rgb[..., :3], [0.299, 0.587, 0.114]).astype(np.uint8)
      
      
      def hough_line(img, angle_step=1, lines_are_white=True, value_threshold=5):
          """
          Hough transform for lines
          Input:
          img - 2D binary image with nonzeros representing edges
          angle_step - Spacing between angles to use every n-th angle
                       between -90 and 90 degrees. Default step is 1.
          lines_are_white - boolean indicating whether lines to be detected are white
          value_threshold - Pixel values above or below the value_threshold are edges
          Returns:
          accumulator - 2D array of the hough transform accumulator
          theta - array of angles used in computation, in radians.
          rhos - array of rho values. Max size is 2 times the diagonal
                 distance of the input image.
          """
          # Rho and Theta ranges
          thetas = np.deg2rad(np.arange(-90.0, 90.0, angle_step))
          width, height = img.shape
          diag_len = int(round(math.sqrt(width * width + height * height)))
          rhos = np.linspace(-diag_len, diag_len, diag_len * 2)
      
          # Cache some resuable values
          cos_t = np.cos(thetas)
          sin_t = np.sin(thetas)
          num_thetas = len(thetas)
      
          # Hough accumulator array of theta vs rho
          accumulator = np.zeros((2 * diag_len, num_thetas), dtype=np.uint8)
          # (row, col) indexes to edges
          are_edges = img > value_threshold if lines_are_white else img < value_threshold
          y_idxs, x_idxs = np.nonzero(are_edges)
      
          # Vote in the hough accumulator
          for i in range(len(x_idxs)):
              x = x_idxs[i]
              y = y_idxs[i]
      
              for t_idx in range(num_thetas):
                  # Calculate rho. diag_len is added for a positive index
                  rho = diag_len + int(round(x * cos_t[t_idx] + y * sin_t[t_idx]))
                  accumulator[rho, t_idx] += 1
      
          return accumulator, thetas, rhos
      
      
      def show_hough_line(img, accumulator, thetas, rhos, save_path=None):
          plt.imshow(accumulator, aspect='auto', cmap='jet', extent=[np.rad2deg(thetas[-1]), np.rad2deg(thetas[0]), rhos[-1], rhos[0]])
          if save_path is not None:
              plt.savefig(save_path, bbox_inches='tight')
          plt.show()
      
      
      if __name__ == '__main__':
          imgpath = 'path_img.tif'
          img = imageio.imread(imgpath)
          if img.ndim == 3:
              img = rgb2gray(img)
          accumulator, thetas, rhos = hough_line(img)
          show_hough_line(img,
                          accumulator,
                          thetas, rhos,
                          save_path='output.png')
      

      我得到了正确的结果:

      对于这张图片:

      【讨论】:

        【解决方案3】:

        OpenCV API 没有这种能力。 OpenCV 的霍夫线变换函数都不会将该图像返回给调用者。

        http://docs.opencv.org/modules/imgproc/doc/feature_detection.html#houghlines

        如果您想为一行收到的票数设置一个最小阈值,您可以在调用 Hough Lines 方法时设置threshold 参数。

        如果您想可视化霍夫线投票空间的图像以用于学习目的,您将不得不使用 MATLAB 的radon 命令。或者你可以实现你自己的。

        【讨论】:

          猜你喜欢
          • 2011-12-17
          • 2016-02-06
          • 1970-01-01
          • 2011-12-25
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-05-12
          • 2016-09-26
          相关资源
          最近更新 更多