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 True
if 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
@Csrf_Exempt Does Not Work on Generic View Based Class
Rotate Image and Crop Out Black Borders
How to Read a Column of CSV as Dtype List Using Pandas
Check If Value Already Exists Within List of Dictionaries
How to Fix Character Constantly Accelerating in Both Directions After Deceleration Pygame
How to Install the Yaml Package for Python
Populate a Pandas Sparsedataframe from a Scipy Sparse Matrix
Sqlalchemy Unique Across Multiple Columns
How to Display a 3D Plot of a 3D Array Isosurface in Matplotlib Mplot3D or Similar
Matplotlib: Adding an Axes Using the Same Arguments as a Previous Axes
How to Efficiently Process a Numpy Array in Blocks Similar to Matlab's Blkproc (Blockproc) Function
Login to Website Using Urllib2 - Python 2.7
How to Initialize the Base (Super) Class
Pandas: Change Data Type of Series to String
How to Read Contents of an Table in Ms-Word File Using Python
Using Psycopg2 with Lambda to Update Redshift (Python)
How to Add Default Parameters to Functions When Using Type Hinting