1

I have a final list like this, notice the "None" at the end and I am trying to reformat it:

[array([[[18, 10, 10],
    [44, 38, 10]]], dtype=uint16), array([[[24, 32,  9]]], dtype=uint16), array([[[18, 12,  9]]], dtype=uint16), array([[[20, 34,  9]]], dtype=uint16), array([[[16, 12, 10]]], dtype=uint16), array([[[16, 12, 10]]], dtype=uint16), None]

This is what comes directly out of the function cv.HoughCircles():

[[[18.5 10.5 10.3]]]

If it failed to read it will return

"None"    

This is the output from Hough circles in OpenCV. Occasionally Hough Circles will output a "None", I have a check in case it fails but it doesn't stop None being read.

I cannot get a check to work properly to stop the None from being appended or removed.

This is the Hough Circle Code:

    def Hough_Circles(image,dp,minDist,param1,param2,minRadius,maxRadius):
    try:
        circles = cv.HoughCircles(image, cv.HOUGH_GRADIENT, dp, minDist,
                                param1=param1,
                                param2=param2,
                                minRadius=minRadius,
                                maxRadius=maxRadius)
        if(circles == None):
             #this is a keyword to keep it from appending later
             circles = "REMOVE ME"
        else:
            circles = np.uint16(np.around(circles))
    except:
        print("no circle found")
    return circles

I have tried the following methods to check and remove this:

this was tried after np.uint16()

if(circles[index] != None):
    # this is a good item


if(any(circles == None))
    # This is a bad item

this was tried before np.uint()

if(isinstance(circles,list):
    # This is a good item

if(circles == None):
    # this is a bad item

if(isinstance(circles,list)):
    # it says [[[18.5 10.5 10.3]]] is not a list
    circles = np.uint16(np.around(circles))
else:
    # this is all it reads
    circles = "BAD READ"

I've tried to remove it before the np.uint is added but does not help.

My issue is almost always "NoneType is not subscriptable" or "the truthvalue of a subscribable is ambiguous" which makes sense. But also the "isinstance" always says that the list is not a list, however, the same test on a python console says it is a list.

Community
  • 1
  • 1
  • 1
    your circles are not `list`s, they are `np.array`s. So that's why your `isinstance` checks fail. Checking for `None` should always be done using the `is` operator, see [here](https://stackoverflow.com/questions/3257919/what-is-the-difference-between-is-none-and-none), not using `==`. – amdex Oct 07 '19 at 17:59
  • 2
    `[circle for circle in circles if circles]` should remove Nones if they are in the format of you provided example. – Brian Oct 07 '19 at 18:02
  • 1
    @BrianJoseph should be `[circle for circle in circles if circle]` (`if circle` no **s**) – r.ook Oct 07 '19 at 18:40
  • @amdex Perhaps how I word this is confusing. There are two ways I have tried to check. First, I check at the Hough Circle function which is just the simple list (before I use np.array). Second way I was checking was after I did np.array. – Charles Curt Oct 07 '19 at 18:52
  • @Krrr could you give more detail of how to do that? do you mean something like this? circles = [circle for circle in circles if (circles no s)] – Charles Curt Oct 07 '19 at 18:53
  • @Krrr, ahh thanks. Python's great but sometimes you get confused when writing expressions like that haha. – Brian Oct 07 '19 at 19:03
  • @CharlesCurt, part of Krrr's response wasn't syntactic. It was for documentation purposes. What you want is: `circles = [circle for circle in circles if circle]` – Brian Oct 07 '19 at 19:04
  • He meant literally: `[circle for circle in circles if circle]`, but I am not sure you have a list? What does `cv.HoughCircles` return? Don't you have an `np.array` or what exactly does it return? If that was the case: `circles = np.array([c for c in circles if c is not None])`.Essentially, it's the step in that direction, may need to adjust. Note1: if value could be `0` or other resulting to evaluate to `False`, you may want to check explicitly for `None`. Note2: As in your code, there is only one `None` and it's usually tested for identity (`is`/`is not`), not equality (`==`/`!=`). – Ondrej K. Oct 07 '19 at 19:07
  • @OndrejK. thanks for the response. Yes, I just did a type check and it output a np.ndarray. Quick fix solved it, thanks for all of your help! – Charles Curt Oct 07 '19 at 19:12

1 Answers1

0

Solved

I did not know that HoughCircles actually returns as a "numpy.ndarray"

Note to self, always check the type.

    def Hough_Circles(image,dp,minDist,param1,param2,minRadius,maxRadius):
    #circles = []
    try:
        circles = cv.HoughCircles(image, cv.HOUGH_GRADIENT, dp, minDist,
                                param1=param1,
                                param2=param2,
                                minRadius=minRadius,
                                maxRadius=maxRadius)
        if(isinstance(circles,np.ndarray)):
            circles = np.uint16(np.around(circles))
        else:
            circles = [[["BAD READ"]]]
    except:
        print("no circle found")
    return circles