Ask Your Question

Largest circle inside a contour - Python

asked 2018-12-01 11:30:00 -0500

Hatmpatn gravatar image

I want to find the largest circle(or rectangle) possible inside the contours generated in this code. This is the binary image that the contours are generated from:

image description

I have this code:

import cv2
import numpy as np
import time
import math
from picamera.array import PiRGBArray
from picamera import PiCamera

#initialize the camera and grab a reference to the raw camera capture
#and set the resolution

#allow the camera to sleep

#grab an image
camera.capture(rawCapture, format="bgr")

#convert from BGR to HSV for easier segmentation

#define the orange we want to filter out of the image

#Threshold the HSV image
imagebinary=cv2.inRange(hsvimage, lower_orange, upper_orange)

#Invert the HSV image

#apply findContours
modimg, contours, hierarchy=cv2.findContours(invbinimg,cv2.RETR_LIST,cv2.CHAIN_APPROX_NONE)

#filter out the contours that doesn't have the correct area size and draw the into original image


#loop through the contours with certain conditions
for i in range(len(contours)):

    #if area is the correct one
    if (area > area_min) and (area < area_max):
            #Sort out the parts that are upside down with moments
            if (hu[6] < 0):
                #Calculate and draw center of mass
      , centres[-1],3,(0,0,255),-1)
                cv2.drawContours(image, contours, i, (0,255,0), 2)
                #THIS IS WHERE I NEED HELP, draw the largest circle(or rectangle) possible in the 
                #i-th contour

            #show coordinates of the centers of mass    

#display image on screen and wait for a keypress
cv2.imshow("show", image)
#save imaginebinary
cv2.imwrite("/home/pi/springs/imagebinary.png", imagebinary)

This is the result I want How can I implement this in my code? As far as I've understood, pointPolygonTest should be a suiting function, but I have no clue how to use it in my for-loop. Please help!

image description

edit retag flag offensive close merge delete


LBerger gravatar imageLBerger ( 2018-12-01 12:51:19 -0500 )edit

That is genious LBerger! Lets go with that solution. I found something promising that would return the X and Y position of the pixel that is furthest away from a black pixel, but it is written in C++. Im quite bad at translating this code into Python. Care to help me how I would go about writing this in Python?

link text

Hatmpatn gravatar imageHatmpatn ( 2018-12-01 16:38:57 -0500 )edit

The links doesn't work for you.

supra56 gravatar imagesupra56 ( 2018-12-01 23:06:22 -0500 )edit

Do you mean that the code in my link will not work for my code? Ok, in that case too bad.

Could this work?

dist_transform=cv2.distanceTransform(contours, DIST_L2, 5)

#and to get center x, y for the blue circle

Hatmpatn gravatar imageHatmpatn ( 2018-12-02 04:41:11 -0500 )edit

Okay, I have gotten the distanceTransform to work perfectly with the original image. But when I try to supply it with the specific contours in the for-loop it doesnt seem to work. It seems to want a numpy array... What is a contour?

Hatmpatn gravatar imageHatmpatn ( 2018-12-02 13:00:04 -0500 )edit

What to write instead of cnt in dist_transform?

  if (hu[6] < 0):
        #Calculate and draw center of mass
        centres.append((int(moments['m10']/moments['m00']),int(moments['m01']/moments['m00']))), centres[-1],3,(0,0,255),-1)
        cv2.drawContours(image, contours, i, (0,255,0), 2)

        #What do I write instead of CNT to supply the distanceTransform with an image of the specific contour?
        maxDT=np.unravel_index(dist_transform.argmax(), dist_transform.shape), (maxDT[1], maxDT[0]),3,(0,0,255),-1)
Hatmpatn gravatar imageHatmpatn ( 2018-12-02 13:00:35 -0500 )edit

use the doc distance transform use a binary image you don't need the contour

LBerger gravatar imageLBerger ( 2018-12-02 13:22:46 -0500 )edit

1 answer

Sort by ยป oldest newest most voted

answered 2018-12-02 16:51:55 -0500

Hatmpatn gravatar image

Yes, solved it by using a singe channel image instead! So everything is good now, thx for the help!

edit flag offensive delete link more

Question Tools

1 follower


Asked: 2018-12-01 11:30:00 -0500

Seen: 2,206 times

Last updated: Dec 01 '18