OpenCV resize fails on large image with “error: (-215) ssize.area() > 0 in function cv::resize”

后端 未结 13 2106
醉酒成梦
醉酒成梦 2021-01-01 10:08

I\'m using OpenCV 3.0.0 and Python 3.4.3 to process a very large RGB image (107162,79553,3). While I\'m trying to resize it using the following code:

import         


        
相关标签:
13条回答
  • 2021-01-01 10:29

    In my case,

    image = cv2.imread(filepath)
    final_img = cv2.resize(image, size_img)
    

    filepath was incorrect, cv2.imshow didn't give any error in this case but due to wrong path cv2.resize was giving me error.

    0 讨论(0)
  • 2021-01-01 10:32

    In my case I did a wrong modification in the image.

    I was able to find the problem checking the image shape.

    print img.shape
    
    0 讨论(0)
  • 2021-01-01 10:33

    Also pay attention to the object type of your numpy array, converting it using .astype('uint8') resolved the issue for me.

    0 讨论(0)
  • 2021-01-01 10:37

    So it turns out that the problem comes from one line in modules\imgproc\src\imgwarp.cpp:

    CV_Assert( ssize.area() > 0 );
    

    When the product of rows and columns of the image to be resized is larger than 2^31, ssize.area() results in a negative number. This appears to be a bug in OpenCV and hopefully will be fixed in the future release. A temporary fix is to build OpenCV with this line commented out. While not ideal, it works for me.

    And I just recently found out that the above applies only to image whose width is larger than height. For images with height larger than width, it's the following line that causes error:

    CV_Assert( dsize.area() > 0 );
    

    So this has to be commented out as well.

    0 讨论(0)
  • 2021-01-01 10:37

    I had the same error. Resizing the images resolved the issue. However, I used online tools to resize the images because using pillow to resize them did not solve my problem.

    0 讨论(0)
  • 2021-01-01 10:39

    You can manually place a check in your code. Like this -

    
        if result != []:
            for face in result:
                bounding_box = face['box']
                x, y, w, h = bounding_box[0], bounding_box[1], bounding_box[2], bounding_box[3]
                rect_face = cv2.rectangle(frame, (x, y), (x+w, y+h), (46, 204, 113), 2)
                face = rgb[y:y+h, x:x+w]
    
                #CHECK FACE SIZE (EXIST OR NOT)
                if face.shape[0]*face.shape[1] > 0:
    
                    predicted_name, class_probability = face_recognition(face)
    
                    print("Result: ", predicted_name, class_probability)
    `
    
    0 讨论(0)
提交回复
热议问题