Ask Your Question
0

Largest circle inside a contour - Python

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

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
camera=PiCamera()
rawCapture=PiRGBArray(camera)
camera.resolution=(800,608)

#allow the camera to sleep
time.sleep(0.1)

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

#convert from BGR to HSV for easier segmentation
hsvimage=cv2.cvtColor(image,cv2.COLOR_BGR2HSV)

#define the orange we want to filter out of the image
lower_orange=np.array([0,42,75])
upper_orange=np.array([40,203,220])

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

#Invert the HSV image
invbinimg=cv2.bitwise_not(imagebinary)

#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

area_min=3000
area_max=4000

#loop through the contours with certain conditions
for i in range(len(contours)):
    cnt=contours[i]
    area=cv2.contourArea(cnt)
    centres=[]

    #if area is the correct one
    if (area > area_min) and (area < area_max):
            moments=cv2.moments(contours[i])
            hu=cv2.HuMoments(moments)
            #Sort out the parts that are upside down with moments
            if (hu[6] < 0):
                #Calculate and draw center of mass
                centres.append((int(moments['m10']/moments['m00']),int(moments['m01']/moments['m00'])))
                cv2.circle(image, 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
                #cv2.pointPolygonTest...

            #show coordinates of the centers of mass    
            print(centres)


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

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

Comments

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

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 -0600 )edit

The links doesn't work for you.

supra56 gravatar imagesupra56 ( 2018-12-01 23:06:22 -0600 )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

circleXY=dist_transform.max
Hatmpatn gravatar imageHatmpatn ( 2018-12-02 04:41:11 -0600 )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 -0600 )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'])))
        cv2.circle(image, centres[-1],3,(0,0,255),-1)
        cv2.drawContours(image, contours, i, (0,255,0), 2)
        print(area)

        #What do I write instead of CNT to supply the distanceTransform with an image of the specific contour?
        dist_transform=cv2.distanceTransform(cnt,cv2.DIST_L2,5) 
        maxDT=np.unravel_index(dist_transform.argmax(), dist_transform.shape)
        cv2.circle(image, (maxDT[1], maxDT[0]),3,(0,0,255),-1)
Hatmpatn gravatar imageHatmpatn ( 2018-12-02 13:00:35 -0600 )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 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
1

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

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

Stats

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

Seen: 4,936 times

Last updated: Dec 01 '18