1
votes

2nd post here (I Think) I got an Traceback (most recent call last): File "c:\Users\bala006\OneDrive - St John's Anglican College\Desktop\Personal\Torch\lick.py", line 30, in circles = cv.HoughCircles(img, cv.HOUGH_GRADIENT, 1.2, 100) cv2.error: OpenCV(4.5.1) C:\Users\appveyor\AppData\Local\Temp\1\pip-req-build-kh7iq4w7\opencv\modules\imgproc\src\hough.cpp:2253: error: (-215:Assertion failed) !_image.empty() && _image.type() == CV_8UC1 && (_image.isMat() || _image.isUMat()) in function 'cv::HoughCircles'

error. Hopefully everything is in order... Thanks. I created a cascade using Cascade Trainer and am using it to detect Ping Pong balls.

import cv2 as cv
import numpy as np 

camera = cv.VideoCapture(0, cv.CAP_DSHOW)

cascade = cv.CascadeClassifier('cascade.xml')



while(1):
    _,img = camera.read()
    img = cv.flip(img,1)

    hsv = cv.cvtColor(img, cv.COLOR_BGR2HSV)

    detector = cv.SimpleBlobDetector()

    LowerRegion = np.array([4,142,149],np.uint8)
    upperRegion = np.array([33,255,255],np.uint8)

    redObject = cv.inRange(hsv,LowerRegion,upperRegion)

    kernal = np.ones((1,1),"uint8")

    red = cv.morphologyEx(redObject,cv.MORPH_OPEN,kernal)
    red = cv.dilate(red,kernal,iterations=1)

    res1=cv.bitwise_and(img, img, mask = red)
    
    circles = cv.HoughCircles(img, cv.HOUGH_GRADIENT, 1.2, 100)
    if circles is not None:
        circles = np.round(circles[0, :]).astype("int")

        for (x, y, r) in circles:
            cv.circle(img, (x, y), r, (0, 255, 0), 4)



    cv.imshow("Masking ",res1)
    cv.imshow("please work", img)

    if cv.waitKey(10) & 0xFF == ord('q'):
        camera.release()
        cv.destroyAllWindows()
        break
1

1 Answers

1
votes

HoughCircles does not support colored image.

You need to convert img to Grayscale before executing HoughCircles:

gray_frame = cv.cvtColor(frame, cv.COLOR_BGR2GRAY)
circles = cv.HoughCircles(gray_frame, cv.HOUGH_GRADIENT, 1.2, 100)

The exception message includes a C++ statement that raises by OpenCV code (the code is implemented in C++).


The exception message looks like gibberish but looking carefully implies the reason:

(-215:Assertion failed)

Means that the "asserted" (C++) expression is false.

!_image.empty() && _image.type() == CV_8UC1 && (_image.isMat() || _image.isUMat())

The above statement applies a logical and between 3 terms.
If one of the three is false, the expression is evaluated to false (and false raises and exception):

  • !_image.empty() is false is image is empty, but we know image is not empty.
  • _image.type() == CV_8UC1 is false in case image type is not CV_8UC1.
  • (_image.isMat() || _image.isUMat()) is not going to be false when executing from Python (ignore it).

We may conclude that _image.type() == CV_8UC1 is false.


What is the meaning of CV_8UC1?

The OpenCV images types has a naming convention.

  • 8U applies 8 bits per component (each element is uint8 type).
  • C1 applies 1 color channel.

For colored image in BGR color format, the type is CV_8UC3 (C3 applies 3 color channels).

You can verify that the type of img is a uint8 NumPy array.
Verify that img.dtype is dtype('uint8') (it probably is)...

You may check the number of color channels, by inspecting img.shape.
You are going to see that img.shape is (cols, rows, 3) (3 applies 3 color channels).
For Grayscale images (one color channel) we need the shape to be (cols, rows).