10

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]))
1
  • 2
    If you're facing such error, it's probably because something is wrong with your bounding boxes. In this case I suggest you to check in the exception what's going on instead of leaving the except clause with a single break
    – Maf
    Commented Jan 26, 2021 at 15:01

10 Answers 10

13

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)
2
  • 3
    If you're facing such error, it's probably because something is wrong with your bounding boxes. In this case I suggest you to check in the exception what's going on instead of leaving the except clause with a single break
    – Maf
    Commented Jan 26, 2021 at 15:01
  • this answer is completely nonsensical. the try-except hides the issue. the code is malformed. if img is None, then the lines of code after the try-except will also fail. I rarely see such low-quality answers. Commented Jun 26 at 15:14
7

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

5

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.

0
2

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

0

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")]
-1

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.

-1

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.

-1
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)
1
  • Remember that Stack Overflow isn't just intended to solve the immediate problem, but also to help future readers find solutions to similar problems, which requires understanding the underlying code. This is especially important for members of our community who are beginners, and not familiar with the syntax. Given that, can you edit your answer to include an explanation of what you're doing and why you believe it is the best approach? Commented Mar 8, 2023 at 0:04
-1

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/
1
  • 1
    Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
    – Algamest
    Commented Jan 21, 2022 at 17:04
-3

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.

3
  • I am using images, not a video file Commented Jul 30, 2019 at 6:13
  • image also same. you should check name of file and Image name in code.
    – KScript
    Commented Jul 30, 2019 at 6:18
  • If your directory contains the files other than the images, it could generate the error like yours. Check the directory to see if it has only the image files.
    – passion
    Commented Dec 17, 2019 at 1:01

Not the answer you're looking for? Browse other questions tagged or ask your own question.