-2

So here is my problem, I need to be able to identify and count shapes for a project and am having one small difficulty. I have just started messing around with OpenCV and have not been able to find a better method on how to identify circles without just using an else command. For example, if there is anything else in the frame that is not one clearly one of the other shapes it will detect it as a circle (and ideally I would like to be able to ignore those objects in the frame.) I am not sure if this affects anything but before this portion of the code, I am using adaptive thresholding and gaussian blur to assist with better processing ability.

TLDR: I am trying to find a way to identify circles without using an else statement.

def detect(self, c):
    # initialize the shape name and approximate the contour
    shape = "unidentified"
    peri = cv2.arcLength(c, True)
    approx = cv2.approxPolyDP(c, 0.04 * peri, True)

    # if the shape is a triangle, it will have 3 vertices
    if len(approx) == 3:
        shape = "triangle"
        self.s2 = self.s2 + 1

    # if the shape has 4 vertices, it is either a square or
    # a rectangle
    elif len(approx) == 4:
        # compute the bounding box of the contour and use the
        # bounding box to compute the aspect ratio
        (x, y, w, h) = cv2.boundingRect(approx)
        ar = w / float(h)

        # a square will have an aspect ratio that is approximately
        # equal to one, otherwise, the shape is a rectangle
        if 0.75 <= ar <= 1.35:
            shape = "square"
            self.s3 = self.s3 + 1
        else:
            shape = "rectangle"
            self.s4 = self.s4 + 1

    # otherwise, we assume the shape is a circle
    else:
        shape = "circle"
        self.s5 += 1
  • 1
    Hello, what have you tried ? I checked google really quick and the first result is an article that describes how to do this with code examples. What was the problem ? [see here](https://www.pyimagesearch.com/2014/07/21/detecting-circles-images-using-opencv-hough-circles/) –  Mar 11 '19 at 08:25
  • _" without using an else statement"_ why? – Miki Mar 11 '19 at 08:26
  • can you show sample images? One common approach is to test whether some edge-like structures really are there, where you are expecting a circle-contour. – Micka Mar 11 '19 at 09:30
  • Reportgunner, the problem is that I do not know how I would compare contours generated form HoughCircles to ones found in my image. I have basic knowledge of OpenCV and can only understand so much documentation as of right now. Miki, The reason why I need this is so if there is something that is not a predefined shape in my code it will not be detected as a circle. Micka, here is a perfect environment for the code: https://cdn.discordapp.com/attachments/365641247236554753/554857790292623360/unknown.png – Adam Graham Mar 12 '19 at 02:47
  • @Micka Something that could cross-check the shapes so that a circle is checked with something else. That would be ideal. – Adam Graham Mar 12 '19 at 04:05
  • try chamfer matching. Or have a look at both of my answers here: https://stackoverflow.com/questions/20698613/detect-semi-circle-in-opencv/20706100#20706100 – Micka Mar 12 '19 at 05:38

1 Answers1

0

I solve my problem by re-organizing my code significantly and adding

if shape == "circle":
    circles = cv2.HoughCircles(thresh,cv2.HOUGH_GRADIENT,1,20,param1=50,param2=30,minRadius=0,maxRadius=0)
    if circles is not None:
        s5 += 1

To the new area of the code. Thank you all for your help.