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

Question:

This is the error I get:

error: OpenCV(4.1.0) C:projectsopencv-pythonopencvmodulesimgprocsrcresize.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]))
Asked By: palash behra

||

Answers:

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.

Answered By: KScript

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.

Answered By: Amit Gupta

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

Answered By: Muhammad Saad Najib

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)
Answered By: Viresh Saini
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)
Answered By: Iftikhar humayun

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.

Answered By: Mehedi Hasan Bijoy

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")]
Answered By: H Sa

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.

Answered By: Anthony Mifsud

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/
Answered By: user17993485

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

import PIL
import os
import os.path
from PIL import Image

f = r'//content/drive/MyDrive/data/'
for file in os.listdir(f):
    f_img = f+" "+file
    img = Image.open(f_img)
    img = img.resize((64,64))
    img.save(f_img)
Answered By: Adnan
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.