Open cv error: (-215) scn == 3 || scn == 4 in the cvtColor function

I'm on Ubuntu 14.04 now using python 2.7 and cv2.

When I run this code:

import numpy as np import cv2 img = cv2.imread('2015-05-27-191152.jpg',0) gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) 

it returns:

  File "face_detection.py", line 11, in <module> gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) cv2.error: /home/arthurckl/Desktop/opencv-3.0.0-rc1/modules/imgproc/src/color.cpp:7564: error: (-215) scn == 3 || scn == 4 in function cvtColor 

I already searched here, and one answer said that I can upload my photo wrong, because it must have 3 dimensions: rows, columns and depth.

When I print img.shape it only returns two numbers, so I have to do it wrong. But I don’t know how to upload my photo correctly.

+43
python opencv photo
May 28 '15 at 12:06
source share
8 answers

Give the full path of the image with a slash. He solved the mistake for me.

eg.

 import numpy as np import cv2 img = cv2.imread('C:/Python34/images/2015-05-27-191152.jpg') gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) 

In addition, if you specify 0 in the second parameter when loading the image using cv2.imread , than there is no need to convert the image using cvtColor , it is already loaded as an image in grayscale, for example.

 import numpy as np import cv2 gray = cv2.imread('C:/Python34/images/2015-05-27-191152.jpg',0) 
+53
Sep 16 '15 at 2:42
source share

Set below

 img = cv2.imread('2015-05-27-191152.jpg',1) // Change Flag As 1 For Color Image //or O for Gray Image So It image is //already gray 
+9
Jul 26 '15 at 11:51
source share

Only pass the image name, no need 0 :

 img=cv2.imread('sample.jpg') 
+3
Mar 24 '17 at 15:55
source share
 img = cv2.imread('2015-05-27-191152.jpg',0) 

The above line of code reads your image in a grayscale color model due to the addition of 0 at the end. And if you try again to convert an already gray image to a gray image, it will show this error.

So use the style above or try using the following code:

 img = cv2.imread('2015-05-27-191152.jpg') gray_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) 
+3
Jul 20 '17 at 7:00
source share

First of all, you need to check if the image exists in the root directory or not. This is mainly due to the image with height = 0. This means that cv2.imread(imageName) not reading the image.

+2
Feb 26 '17 at 10:38 on
source share

This is what I noticed when I used my own .jpg image sets. In the sample script available in the Opencv doc , notice that it has the undistort and crop the image lines, as shown below:

 # undistort dst = cv2.undistort(img, mtx, dist, None, newcameramtx) # crop the image x,y,w,h = roi dst = dst[y:y+h, x:x+w] cv2.imwrite('calibresult.jpg',dst) 

So, when we run the code for the first time, it executes the line cv2.imwrite('calibresult.jpg',dst) , saving the image calibresult.jpg in the current directory. So, the next time I ran the code, along with my sets of sample images that I used to calibrate the camera in jpg format, the code also tried to examine this newly added image calibresult.jpg , which caused an error to calibresult.jpg up

 error: C:\builds\master_PackSlaveAddon-win64-vc12-static\opencv\modules\imgproc\src\color.cpp:7456: error: (-215) scn == 3 || scn == 4 in function cv::ipp_cvtColor 

What I did: I simply deleted this newly created image after each run or alternatively changed the image type to say png or tiff . This solved the problem. Make sure you enter and record calibresult the same type. If so, just change the type.

0
Mar 12 '17 at 12:45
source share

I think this is because cv2.imread cannot read the .jpg picture, you need to change the .jpg to .pgn, ma

0
Oct 28 '17 at 6:32
source share

Try this on line 11, working for me. I think you are missing the parameters of the detectMultiScale () function

 faces = face_cascade.detectMultiScale(gray, 1.3, 5) 
-3
Mar 03 '16 at 8:23
source share



All Articles