Search code examples
pythonopencvimage-processingedge-detectioncanny-operator

How do I find the lowest point of a binary image?


After applying deep learning object detection and several image processing filters, at the end I have got the following image. Here is the image

enter image description here

I want to find the redpoint coordinate frame.

enter image description here

In order to get closer to the answer and remove those unwanted lines. I used a mask and here is the final result.

My question is how can I find one point in the lowest point of this image?

enter image description here


Solution

  • An alternative solution is to use cv2.HoughCircles to detect round shapes in your image.

    I used an example from pyimagesearch as base for my code, and added the low part of the circle with the output from the detected circle in HoughCircles. The result is in the image below:

    lowpointcircle

    Since this function returns the [x, y, r] (center point and radius of the circle) you can easily find the lowest part of the circle doing:

    low_point = [x, y + r]
    

    Remember that you can play around with the parameters from the cv2.HoughCircles() function.

    You can see the Jupyter Notebook I used in this Github page..

    The code I used:

    # 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]));