1

So basically I'm converting images from BGR (used by OpenCV) to RGB (used by dlib) for some image vision stuff.

Here's some code:

for (i, imagePath) in enumerate(imagePaths):
    # load the input image and convert it from RGB (OpenCV ordering)
    # to dlib ordering (RGB)
    image = cv2.imread(imagePath)
    rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)`

I'm getting the error OpenCV Error: Assertion failed (scn == 3 || scn == 4) in cvtColor on rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)

The full error is cv2.error: /home/Ben/opencv-3.2.0/modules/imgproc/src/color.cpp:9716: error: (-215) scn == 3 || scn == 4 in function cvtColor

Usually I know these errors are caused by incorrect image paths, but I've run the same code with different pictures, so it must be something with the pictures.

Looking at the picture properties, the only difference I can see is that the pics that don't work are 1080x1920 while the pictures that do work are a variety of sizes, such as 512x440 or 291x512.

What am I doing wrong?

Thanks, Ben

Julia Fasick
  • 121
  • 7
  • Does the problem occur when you enter the path directly, without the for-loop? Is the path made with forward slashes? ([suggested here](https://stackoverflow.com/a/32598876/10699171)) – J.D. Mar 19 '19 at 23:02
  • @J.D. Yeah I was actually looking at that post when I originally was searching for an answer. The problem does occur when the image is put in manually, with this code: `import cv2 image = cv2.imread('/home/deeplearning/PycharmProjects/AltTSAStates/xc_pics/7516545584_IMG_4245.jpg', 0) rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)` The path is entered with forward slashes. – Julia Fasick Mar 19 '19 at 23:25
  • @J.D. Also I think it might be an indicator how the error traces to the converting line, so it appears to read in correctly. – Julia Fasick Mar 19 '19 at 23:31
  • Can you try `cv2.imread([path], *1*)`? When loading an image, the code `0` means grayscale, which can't be used in a bgr-conversion. [documentation](https://docs.opencv.org/3.4.5/d4/da8/group__imgcodecs.html) – J.D. Mar 19 '19 at 23:40
  • @J.D. I tried it with 1 (color), -1 (unchanged), and 4 (anycolor). I did, however, exclude the asterisks. Should they be included? – Julia Fasick Mar 20 '19 at 00:02
  • No, it was mean for emphasis. Can you try it with the image in the same folder as the script? Can you add `print(image.shape)` after `imread` and post the result? – J.D. Mar 20 '19 at 00:34
  • @J.D. the pics are already in the same folder as the script. All of my results printing the shape are tuples, such as `(886, 590, 3)`, `(590, 886, 3)`, `(1280, 1920, 3)`, and `(1920, 1280, 3)` – Julia Fasick Mar 20 '19 at 02:06
  • Well, I see nothing out of the ordinary. Can you display the image without color conversion? If not, can you upload one of the problematic images? – J.D. Mar 20 '19 at 12:18
  • @J.D. https://drive.google.com/open?id=1PpvvT6cvhZkmHlflEK42ZbPkOF2pWlOq – Julia Fasick Mar 21 '19 at 16:59
  • It works just fine for me. But you mentioned that the problematic images are 1080x1920, these are smaller. – J.D. Mar 21 '19 at 22:04

0 Answers0