Open Cv Error: (-215) Scn == 3 || Scn == 4 in Function Cvtcolor

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

Give the full path of image with forward slash. It solved the error for me.

E.g.

import numpy as np
import cv2

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

Also, if you give 0 in second parameter while loading image using cv2.imread than no need to convert image using cvtColor, it is already loaded as grayscale image eg.

import numpy as np
import cv2

gray = cv2.imread('C:/Python34/images/2015-05-27-191152.jpg',0)

OpenCV Error: Assertion failed (scn == 3 || scn == 4) in cvtColor

It is a good practice always that you load a resource, like read an image, check if the resource was actually loaded. In that case, you should to check:

img_bgr = cv2.imread('opencv-template-matching-python-tutorial.jpg')
if img_bgr is not None
img_gray = cv2.cvtColor(img_bgr, cv2.COLOR_BGR2GRAY)

Note: Since in OpenCV by default the color space model is BGR (not RGB) I just renamed your img_rgb variable to img_bgr.

OpenCV Assertion Failed error: (-215) scn == 3 || scn == 4 in function cv::cvtColor works ALTERNATE times

At least I do not find any major problem in your code, i.e. "should work". The problem seems to be in the camera driver. Cameras are different, and camera drivers are different (a.k.a. buggy).

Unfortunately, debugging the camera driver is not a very easy mission. The odd behaviour is most likely bound to the specific camera, operating system, OpenCV, and camera driver version. It is not very likely the driver can be fixed. Just try to keep everything up to date.

However, as your camera can capture every second image, there are things to do.

First, verify that the problem really is in the camera driver by replacing:

cam = create_capture(video_src, fallback='synth:bg=../cpp/lena.jpg:noise=0.05')

by

cam = create_capture('synth:bg=../cpp/lena.jpg:noise=0.05')

As is probably evident form the code, this forces the camera to be simulated. Function create_capture is only a wrapper around VideoCapture to provide this functionality. If your code runs fine with this, the problem is in the video driver.

After verifying that, you could try to run the following code:

import cv2

cam = cv2.VideoCapture(0)
cam.open(0)
results = [ cam.read()[0] for i in range(100) ]
print results

This should create a list of 100 Trues, and the process should take a few seconds, as the camera should capture 100 consecutive images.

In your code you do not seem to use the first value in the return tuple of cam.read (ret in your code). It is Trueif the image is really captured. Also, cam.read should block until there is an image available, no need to add any delays.

Most probably you will get a list [True, False, True, False, ...] because the video driver does something odd. There is no beautiful way to fix this, but there is an ugly one. Replace your capture line by:

# max. 10 retries
for i in range (10):
ret, img = cam.read()
if ret:
break
else:
# capture failed even after 10 tries
raise MyExceptiom("Video driver does not like me.")

Of course, the driver may be so broken that you have to release and reopen it once in a while. Even uglier but doable, as well.

Summary: "Most probably it cannot be cured, it does not kill you, and there are medicines to alleviate the symptoms."



Related Topics



Leave a reply



Submit