2

I am using a opencv to manipulate an image. The exact error is:

OpenCV(4.1.0) /io/opencv/modules/imgproc/src/color.cpp:182: error: (-215:Assertion failed) !_src.empty() in function 'cvtColor'

I have searched all over the internet but I cannot find a solution to this.

The same image is called multiple times in this process and most part of it works fine. I get an error in this function:

def percentage(image, box_points):  #box point is touple here. ((x1,y1),(x2,y2))
    x1=box_points[0][0]
    x2=box_points[1][0]
    y1=box_points[0][1]
    y2=box_points[1][1]  
    rows,cols,c =image.shape
    M = cv2.getRotationMatrix2D((cols/2,rows/2),90,1)
    image = cv2.warpAffine(image,M,(cols,rows))
    image=image[int(x1):int(x2),int(y1):int(y2)]
    image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    # ret, bw_img = cv2.threshold(image,160,255,cv2.THRESH_BINARY)
    # bw_img = cv2.adaptiveThreshold(image, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 5, 7)
    bw_img = cv2.adaptiveThreshold(image,255,cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY,11,2)
    # image=image[int(x1):int(x2),int(y1):int(y2)]
    # bw_img is our binary image here.
    count = cv2.countNonZero(bw_img)
    total = bw_img.shape[1] * bw_img.shape[0]
    ratio = count/total
    ratio =1-ratio
    return ratio, bw_img

I get an error in the line where I call cv2.cvtColor. The function gives ratio of black pixels and total pixels. The same image is called several times in this whole project therefore the error should not be related to absence of image.

Also, while testing, I rotated image by 90 degrees counter-clockwise. And this works fine, but just for one specific image. Help me here. enter image description here

I am attaching original image here. It works when I rotate it by 90 degrees.

Himanshu Suthar
  • 437
  • 2
  • 9
  • 22
  • 1
    check if the image is empty after you crop it – Miki Jul 01 '19 at 13:25
  • Possible duplicate of [Open Cv Error: (-215:Assertion failed) !\_src.empty() in function 'cv::cvtColor'](https://stackoverflow.com/questions/54121013/open-cv-error-215assertion-failed-src-empty-in-function-cvcvtcolor) – zteffi Jul 01 '19 at 13:28
  • please [edit] your post to avoid posting real private data on the internet – Jean-François Fabre Mar 22 '20 at 20:13

1 Answers1

0

I tried your code. It's working fine. Although I made a slight modification at a line where you are cropping the image. What you are doing is roi = im[x1:x2, y1:y2] but it should be roi = im[y1:y2, x1:x2]. Check this stackoverflow post for more information on how to crop an image using numpy slicing. It might be the cause of the above error you are getting. You would have to check in order to verify that.

This slight change can be very important. Check the below comparison:

Function call for both the cases: percentage(img, ((10, 10), (300, 600)))

Case 1: image=image[int(x1):int(x2),int(y1):int(y2)]

enter image description here

Case 2: image=image[int(y1):int(y2), int(x1):int(x2)]

enter image description here

I don't know if you are doing it intensionally or not, but please verify it once.

Anubhav Singh
  • 8,321
  • 4
  • 25
  • 43
  • I was cropping horizontally and then vertically. It could be a mistake but according to me in [x:x+h,y:y+w], here x represents x-coordinates (distance from y-axis). Please tell me if I am wrong here. – Himanshu Suthar Jul 02 '19 at 11:18
  • We should first supply the startY and endY coordinates, followed by the startX and endX coordinates to the slice. check here: https://stackoverflow.com/questions/15589517/how-to-crop-an-image-in-opencv-using-python – Anubhav Singh Jul 02 '19 at 11:55
  • See comments in this link's answer. It says that he accidentally flipped it. – Himanshu Suthar Jul 03 '19 at 10:01