Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 cv2
image = cv2.resize(img, (0,0), fx=0.5, fy=0.5, interpolation=cv2.INTER_AREA)

I had this error message coming up:

cv2.error: C:\opencv-3.0.0\source\modules\imgproc\src\imgwarp.cpp:3208: error: (-215) ssize.area() > 0 in function cv::resize

I'm certain there is image content in the image array because I can save them into small tiles in jpg format. When I try to resize just a small part of the image, there is no problem and I end up with correctly resized image. (Taking a rather big chunk (50000,50000,3) still won't work, but it will work on a (10000,10000,3) chunk)

What could cause this problem and how can I solve this?

like image 509
user3667217 Avatar asked Aug 13 '15 19:08

user3667217


4 Answers

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.

like image 197
user3667217 Avatar answered Nov 19 '22 12:11

user3667217


Turns out for me this error was actually telling the truth - I was trying to resize a Null image, which was usually the 'last' frame of a video file, so the assertion was valid.

Now I have an extra step before attempting the resize operation, which is to do the assertion myself:

def getSizedFrame(width, height):
"""Function to return an image with the size I want"""    
    s, img = self.cam.read()

    # Only process valid image frames
    if s:
            img = cv2.resize(img, (width, height), interpolation = cv2.INTER_AREA)
    return s, img

Now I don't see the error.

like image 29
Kelton Temby Avatar answered Nov 19 '22 14:11

Kelton Temby


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

like image 11
Attila Avatar answered Nov 19 '22 13:11

Attila


I know this is a very old thread but I had the same problem which was due spaces in the images names.

e.g.

Image name: "hello o.jpg"

weirdly, by removing the spaces the function worked just fine.

Image name: "hello_o.jpg"

like image 3
Wanderer Avatar answered Nov 19 '22 13:11

Wanderer