【问题标题】:How do I find the lowest point of a binary image?如何找到二值图像的最低点?
【发布时间】:2019-12-14 05:17:39
【问题描述】:

在应用深度学习对象检测和几个图像处理过滤器后,最后我得到了以下图像。 这是图片

我要找红点坐标系。

为了更接近答案并删除那些不需要的行。我使用了一个面具,这是最终的结果。

我的问题是如何在这张图片的最低点找到一个点?

【问题讨论】:

  • 红点似乎不属于“圆”。
  • 致@YvesDaoust,它属于圆圈,轮胎是瘪了,这就是你看到的原因
  • 它周围的正方形是什么?
  • 它来自对象检测算法。
  • 您的尝试/代码在哪里?

标签: python opencv image-processing edge-detection canny-operator


【解决方案1】:

如果我理解正确,您想找到图像中非黑色的最低行的行号。所以你可以这样做:

import cv2
import numpy as np

# Load image
img = cv2.imread('tyre.png',cv2.IMREAD_GRAYSCALE)                                           

# Get X, Y coordinates of non-black pixels
y, x = np.nonzero(img)                                                                            

如果您现在查看y,您会发现第 246 行是任何非黑色像素的最后一行。

# Look at y, specifically y[-1]                                                                                     
array([ 80,  80,  80, ..., 246, 246, 246])

【讨论】:

    【解决方案2】:

    另一种解决方案是使用cv2.HoughCircles 来检测图像中的圆形。

    我使用来自pyimagesearch 的示例作为我的代码的基础,并在HoughCircles 中将检测到的圆圈的输出添加到圆圈的下部。结果如下图:

    由于此函数返回[x, y, r](圆的中心点和半径),您可以轻松找到圆的最低部分:

    low_point = [x, y + r]
    

    请记住,您可以玩弄来自cv2.HoughCircles() 函数的参数。

    你可以看到我在this Github page. 中使用的 Jupyter Notebook。

    我使用的代码:

    # import the necessary packages
        import numpy as np
        import cv2
        import matplotlib.pyplot as plt
        # load the image, clone it for output, and then convert it to grayscale
        image = cv2.imread('img_circle.png')
        output = image.copy()
        gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
        #cv2.imshow("test", image)
        cv2.waitKey(0)
        cv2.destroyAllWindows()
    
    
        plt.imshow(image)
        plt.title('my picture')
        plt.show()
    
        # detect circles in the image
        circles = cv2.HoughCircles(gray, cv2.HOUGH_GRADIENT, 1, 100,
                                   param1=100, param2=40,
                                   minRadius = 130, maxRadius = 0)
    
        # ensure at least some circles were found
        if circles is not None:
            # convert the (x, y) coordinates and radius of the circles to integers
            circles = np.round(circles[0, :]).astype("int")
    
            # loop over the (x, y) coordinates and radius of the circles
            for (x, y, r) in circles:
                # draw the circle in the output image
                cv2.circle(output, (x, y), r, (0, 255, 0), 4)
    
                # Draw low point of the circle 
                cv2.rectangle(output, (x - 10, y - 10 + r), (x + 10, y + 10 + r), (0, 0, 255), -1)
    
            # show the output image
            plt.imshow(np.hstack([image, output]))
            plt.title('my picture')
            plt.show()
    
            cv2.imwrite( "lowPoint.jpg", np.hstack([image, output]));
    

    【讨论】:

    • 嘿,@Leonardo Mariga 请检查 GitHub 的问题部分,我问了一个问题 :)
    • 嗯...我在 GitHub 上找不到任何未解决的问题...你在哪里问的问题?你能把链接发给我吗?我很乐意回答=)
    猜你喜欢
    • 2021-11-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-03
    相关资源
    最近更新 更多