35

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?

mkrieger1
  • 19,194
  • 5
  • 54
  • 65
user3667217
  • 2,172
  • 2
  • 17
  • 29
  • 6
    guess: integer range is +/- 2.147.483.647 while cols x rows ist 8.525.058.586 in your example.So PROBABLY there is an integer overflow within cv::resize. You could test this by testing image sizes around cols x rows == 2.147.483.647 – Micka Aug 13 '15 at 19:23
  • 1
    Hey Micka, you're right !! It works on (46340,46340,3) but not (46341,46341,3). Does that mean changing int to int64 should solve the problem? – user3667217 Aug 13 '15 at 19:59
  • I dont know where inside of cv::resize the area is computed. I think you have to have a look at opencv resize source code... – Micka Aug 13 '15 at 20:43
  • Where can I find this file in macos, I didn't find any file which has similar name ? – Iem-Prog Mar 25 '18 at 13:06

17 Answers17

32

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.

user3667217
  • 2,172
  • 2
  • 17
  • 29
12

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.

Kelton Temby
  • 825
  • 10
  • 20
  • I don't think this is the general case. This error does occur with valid images because of the OpenCV bug. – Hack-R Feb 09 '17 at 18:01
  • 3
    This is the first hit on Google and this should be upvoted!! Check your imgs before starting commenting out the openCV code, I had exactly same problem while parsing dozens of images in preprocessing. – Jan Sila Oct 17 '17 at 14:01
12

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

Attila
  • 311
  • 3
  • 7
  • I have stumbled across this stackoverflow answer multiple times. And this has helped me every time. I am not sure why there aren't enough upvotes for this answer. – Prasad Raghavendra Jun 17 '20 at 22:21
  • worked for me. In my case my image was int16. I converted it to uint16 and it worked. – Rajat Oct 12 '20 at 10:09
4

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"

Wanderer
  • 1,065
  • 5
  • 18
  • 40
2

I am having OpenCV version 3.4.3 on MacOS. I was getting the same error as above.

I changed my code from

frame = cv2.resize(frame, (0,0), fx=0.5, fy=0.5)   

to

frame = cv2.resize(frame, None, fx=0.5, fy=0.5)    

Now its working fine for me.

Biranchi
  • 16,120
  • 23
  • 124
  • 161
2

This type of error also takes place because the resize is unable to get the image in simple the directory of the image may be wrong.In my case I left the forward slash during providing the location of file and this error took place after I put the slash problem was solved.

1

For me the following work-around worked:

  • split the array up into smaller sub arrays
  • resize the sub arrays
  • merge the sub arrays again

Here the code:

def split_up_resize(arr, res):
    """
    function which resizes large array (direct resize yields error (addedtypo))
    """

    # compute destination resolution for subarrays
    res_1 = (res[0], res[1]/2)
    res_2 = (res[0], res[1] - res[1]/2)

    # get sub-arrays
    arr_1 = arr[0 : len(arr)/2]
    arr_2 = arr[len(arr)/2 :]

    # resize sub arrays
    arr_1 = cv2.resize(arr_1, res_1, interpolation = cv2.INTER_LINEAR)
    arr_2 = cv2.resize(arr_2, res_2, interpolation = cv2.INTER_LINEAR)

    # init resized array
    arr = np.zeros((res[1], res[0]))

    # merge resized sub arrays
    arr[0 : len(arr)/2] = arr_1
    arr[len(arr)/2 :] = arr_2

    return arr
Community
  • 1
  • 1
Oliver Wilken
  • 2,654
  • 1
  • 24
  • 34
  • It didn't work for me, I'm getting the same error! "error: (-215) ssize.area() > 0 in function resize". Note: I had to modify ur code since indexing value must be an integer. "Use // instead of / ". – Iem-Prog Mar 25 '18 at 13:26
1

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)
mkrieger1
  • 19,194
  • 5
  • 54
  • 65
0

Turns out I had a .csv file at the end of the folder from which I was reading all the images. Once I deleted that it worked alright

Make sure that it's all images and that you don't have any other type of file

0

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
Thomio
  • 1,403
  • 15
  • 19
0

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

I came across the same error message while I was trying to enlarge the image size. Assigning the image type as uint8 did the work for me and I was able to resize the image 30 times of its original size. Here is an example as a reference for anyone else who has such issue.

scale_percent = 3000
width = int(img.shape[1] * scale_percent / 100)
height = int(img.shape[0] * scale_percent /100)

dim = (width, height)
image = cv2.resize(img.astype('uint8'), dim, interpolation=cv2.INTER_AREA)
0

Same error message for me but issue was different:

The interpolation method 'INTER_AREA' was NOT compatible with int8 !

enter image description here

Ludo Schmidt
  • 1,283
  • 11
  • 16
0
cv2.resize(frame_rgb, tuple([None, None]))

gives similar error. Notice the None values in the resizing tuple.

Mihai.Mehe
  • 448
  • 8
  • 13
0

In my case there were some corrupt or not supported images. What i simple did is just check if it is not None than process it as shown below.

cv2.imread(image_path)
if img is not None:
    cv2.resize(img,(150,150)) # You can give your own desired image size
-1

I was working with 3 files: The python script, the image, and the trained model.

Everything worked when I moved these 3 files into their own folder instead of in the directory with the other python scripts.

Shane Rooney
  • 99
  • 1
  • 2
-1

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.