'error: OpenCV(4.1.0) error: (-215:Assertion failed) !ssize.empty() in function 'cv::resize'

This is the error I get:

error: OpenCV(4.1.0) C:\projects\opencv-python\opencv\modules\imgproc\src\resize.cpp:3718: error: (-215:Assertion failed) !ssize.empty() in function 'cv::resize'

Already checked for corrupted files.

cat is an array of 1000 images (RGB).

I am trying to compress these images to (50,50) grayscale.

def greyscale_compress(img):
    new_img = cv2.resize(img, (50,50))
    img_gray = cv2.cvtColor(new_img, cv2.COLOR_BGR2GRAY)
    return img_gray

cat_bin = []
for i in range(0, 100):
    cat_bin.append(greyscale_compress(cat[i]))


Solution 1:[1]

Generally this problem occurs due to resizing of the image, I just apply try and catch statement for resizing the image so that any error handling. It is working fine and not getting error related to shape.

 img=cv2.imread(filename)
    print(img)
        try:
           img = cv2.resize(img, (1400, 1000), interpolation=cv2.INTER_AREA)
            print(img.shape)
        except:
        break
    height, width , layers = img.shape
    size=(width,height)
    print(size)

Solution 2:[2]

I had the same issue when I was importing images into a list through label containing the name and path of images.

My problem arose due to an empty space in the list of labels.

I fixed this problem by just removing the space from my label list:

enter image description here

Solution 3:[3]

Cat[i] in your code is doing what,

if there are 1000 images in folder, you have to list the image directory (os.listdir) and read them 1 by 1 through loop applying your function.

other than that, your function for compression is correct and working properly, other thing I will suggest to convert in grayscale first and then apply resize, may result a better interpolation.

Solution 4:[4]

That's because open cv can't find any image in the given directory

Check the path you might miss a / and to be on the safe side try

images = [img for img in os.listdir(image_path) if img.endswith(".jpg")]

or

images = [img for img in os.listdir(image_path) if img.endswith("anyformat")]

Solution 5:[5]

X = []
count = 0

path = TRAIN_PATH_X
for img in os.listdir(TRAIN_PATH_X):
    image = cv2.imread(os.path.join(path, img), cv2.IMREAD_GRAYSCALE)
    try:
        image = cv2.resize(image, (IMG_HEIGHT, IMG_WIDTH), interpolation=cv2.INTER_AREA)
        print(image.shape)
    except:
        break
    X.append([image])
    count = count +1
print(count)

Solution 6:[6]

Please make sure that your img variable doesn't equal to None

Solution 7:[7]

It is an error that can not be received from the camera or movie file. Make sure that the video file name is the same as the code and that it is in the same directory.

Solution 8:[8]

It occurs for the following two reasons:

  1. The image path you are using to read the image does not exist.

Solution: Re-check the image path, particularly the backslashes and dots.

  1. The image you are trying to read is a NoneType corrupted image.

Solution: Re-download the image or remove it.

Solution 9:[9]

The fix to the issue was to rectify the file and remove the 0 0 0 0 bounding box I had.

enter image description here

Where there is the highlighted line, there is a box 0 0 0 0. I amended that line from positive/person_0004.jpg 2 0 0 0 0 63 30 148 185 to positive/person_0004.jpg 1 63 30 148 185.

Note that I also changed the number of boxes. 2 -> 1.

Solution 10:[10]

I faced the same issue but I solved it. I had this path:

/content/drive/MyDrive/skincancer/data/train/benign

I solved the issue by adding / at the end of the path, like this:

/content/drive/MyDrive/skincancer/data/train/benign/

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 David Buck
Solution 2 mkrieger1
Solution 3
Solution 4
Solution 5 Tomerikoo
Solution 6 juleslasne
Solution 7 KScript
Solution 8 Mehedi Hasan Bijoy
Solution 9 Tomerikoo
Solution 10 joanis