OpenCV Error: Assertion Failed (scn == 3 || scn == 4) in cvtColor errors occur while working with OpenCV in Raspberry Pi for several reasons. This often happens because your Raspberry Pi does not know that the Raspberry Pi camera is connected to it.
While working on a computer vision project, I came across this error. After surfing the web for a long time, I found a solution to this. In this post, I am going to share a single-line command that can solve this error.
Here is a screenshot of the error that I encountered and the command that I used to solve this error.
OpenCV Error:Assertion Failed
Copy, paste, and run the following command:
#sudo modprobe bcm2835-v4l2
In the command note that, it is V4L2 all in small case. I personally made mistake by typing L as 1.
What does the Command Do?
This command makes the Raspberry Pi camera appear in the /dev directory as a standard USB video device by loading the “Video for Linux 2” abbreviated as V4L2.
The above command does not persist after rebooting the Raspberry Pi. To solve the error, you will have to run the command once again.
To make the command you will have to add the command to the startup script such as rc-local.