OpenCV failing to open camera - c++

I have an OpenCV program that is supposed to use a webcam (in this case the built in laptop camera). The program compiles fine without error but when I run it the program seems to hang on the line:
img_scene = cvCaptureFromCAM(0);
I have a different program that access the camera in the same fashion so I don't understand as to why this program seems to be having trouble.
Full capture code from non-working program:
Mat captureThread() {
if(captureOpen == false){
img_scene = cvCaptureFromCAM(0);
cvSetCaptureProperty(img_scene, CV_CAP_PROP_FRAME_WIDTH, 640);
cvSetCaptureProperty(img_scene, CV_CAP_PROP_FRAME_HEIGHT, 480);
}
while(1) {
image = cvQueryFrame(img_scene);
if(image.empty()) {
continue;
}
cvtColor(image, gray, CV_BGR2GRAY);
return gray;
}
}
This is the code that is being used in a different program that is working as expected:
img_scene = cvCaptureFromCAM(0);
cvSetCaptureProperty(img_scene, CV_CAP_PROP_FRAME_WIDTH, 640);
cvSetCaptureProperty(img_scene, CV_CAP_PROP_FRAME_HEIGHT, 480);
while(1) {
imageFrame = cvQueryFrame(img_scene);
if(imageFrame.empty()) {
continue;
cout << "image frame is empty" << endl;
}
cvtColor(imageFrame, gray, CV_BGR2GRAY);
What is causing the non-working program to have trouble access the camera? I have also tried plugging in a USB camera and setting cvCaptureFromCam(-1) but it does not work either.
EDIT:
The non-working program uses multiple threads whereas the working program does not.

Related

Picture and text on video capture

I would like to put an image on video and i'm wondering if it's possible in opencv without multithreading.
I would like to avoid it because in my project i am operating on RPI 0W(that's whyi don't want multithreading) .
i can't find anything about it on internet. I got some basic code in c++ . I'm new to open cv.
int main(){
VideoCapture cap(0);
if (!cap.isOpened())
{
cout << "error"<<endl;
return -1;
}
Mat edges;
namedWindow("edges", 1);
Mat img = imread("logo.png");
for (;;)
{
Mat frame;
cap >> frame; // get a new frame from camera
imshow("edges", WINDOW_AUTOSIZE );
imshow("edges", img);
imshow("edges", frame);
if (waitKey(30) >= 0) break;
}
}
In OpenCV showing two things in the same window overwrites the previous one which I think is happening in your case.
You can use OpenCV addWeighted() function or bitwise operations.
OpenCV has good documentation on this. You can find it here

Why can I not see captured image?

I am facing a weird problem.
I am able to load and show image. Also, I am able to capture image but I cannot see image in display. The camera connected fine and capture image fine but cannot see image.
My system is window 10- 64 bit with opencv 3.3.0.
Code is below.
int main()
{
cv::VideoCapture cap(0);
if (!cap.isOpened()) {
std::cerr << "camera didn't connected." << std::endl;
return 0;
}
int nFrame = 0;
cv::Mat image = cv::imread("orgin102.jpg");
cv::imshow("image", image);
cvWaitKey(0);
while (true) {
cv::Mat origin;
cap >> origin;
//flip orign
flip(origin, origin, 1);
nFrame++;
cv::imshow("image", origin);
//if (cv::waitKey(27) >= 0) break;
cvWaitKey(0);
}
return 0;
}
I fixed this problem by changing parameter of cap.
here it is
cv::VideoCapture cap(1);
For some systems, 0 index shows as first camera. and for others index 1 shows first camera.
Hope this helps.
Cheers!

How to close camera (OpenCv Beaglebone)

Good Day,
I am trying to figure out how to close the camera on a beaglebone in openCV. I have tried numerous commands such as release(&camera) but none exist and the camera continues to stay on when I don't want it to.
VideoCapture capture(0);
capture.set(CV_CAP_PROP_FRAME_WIDTH,320);
capture.set(CV_CAP_PROP_FRAME_HEIGHT,240);
if(!capture.isOpened()){
cout << "Failed to connect to the camera." << endl;
}
Mat frame, edges, cont;
while(1){
cout<<sending<<endl;
if(sending){
for(int i=0; i<frames; i++){
capture >> frame;
if(frame.empty()){
cout << "Failed to capture an image" << endl;
return 0;
}
cvtColor(frame, edges, CV_BGR2GRAY);
Code is something like this, at the end of the for loop, I want to close the camera, but of course it still stays open
The camera will be deinitialized automatically in VideoCapture destructor.
Check this example from opencv docu:
int main(int, char**)
{
VideoCapture cap(0); // open the default camera
if(!cap.isOpened()) // check if we succeeded
return -1;
Mat edges;
namedWindow("edges",1);
for(;;)
{
Mat frame;
cap >> frame; // get a new frame from camera
cvtColor(frame, edges, CV_BGR2GRAY);
GaussianBlur(edges, edges, Size(7,7), 1.5, 1.5);
Canny(edges, edges, 0, 30, 3);
imshow("edges", edges);
if(waitKey(30) >= 0) break;
}
// the camera will be deinitialized automatically in VideoCapture destructor
return 0;
}
Also
cvQueryFrame
Grabs and returns a frame from camera or file
IplImage* cvQueryFrame( CvCapture* capture );
capture video capturing structure.
The function cvQueryFrame grabs a frame from camera or video file, decompresses and > returns it. This function is just a combination of cvGrabFrame and cvRetrieveFrame in one > call. The returned image should not be released or modified by user.
Also check: http://derekmolloy.ie/beaglebone/beaglebone-video-capture-and-image-processing-on-embedded-linux-using-opencv/
I hope this works for you. Best of luck.

Gray screen being displayed during video capture - OpenCV

I am trying to run a program for video capture from the webcam in OpenCV. Everytime I run the program, a gray screen is being displayed. I initially tried programming in C API using the CvCapture Function and it worked perfectly fine. But now in the C++ API when I try running the following code which uses VideoCapture, a gray screen is getting displayed.
How do I resolve this problem? Please help. My OpenCV version is 2.4.6 and I am running the code in MS Visual Studio 2010 Professional.
#include "opencv2/core/core.hpp"
#include "opencv2/highgui/highgui.hpp"
using namespace cv;
int main(int argc, char** argv)
{
VideoCapture capture(0);
Mat frame;
if( !capture.isOpened() )
throw "Error when reading steam_avi";
namedWindow( "w", 1);
for( ; ; )
{
capture.read(frame);
if(frame.empty())
break;
imshow("w", frame);
waitKey(1);
}
waitKey(0);
}
Your code is running fine on my laptop. Make sure that your camera device is not blocked by another application, or you can try to comment out the namedWindow call (but it should not be a problem), actually you can use following loop to grab video frames from camera:
VideoCapture capture(0);
Mat frame;
if( !capture.isOpened() )
throw "Error when reading steam_avi";
namedWindow( "w", 1);
while(capture.read(frame))
{
imshow("w", frame);
waitKey(1);
}
waitKey(0);
According to documentation: "If no frames has been grabbed (camera has been disconnected, or there are no more frames in video file), the methods return false and the functions return NULL pointer."

OpenCV C++ Video Capture does not seem to work

I am using a Mac OS X 10.6 machine. I have OpenCV 2.1 x64 compiled from source using Xcode and its GCC compiler.
I am having trouble using the C++ video reading features of OpenCV. Here is the simple test code I am using (came straight from OpenCV documentation):
#include "cv.h"
#include "highgui.h"
using namespace cv;
int main(int, char**)
{
VideoCapture cap(0); // open the default camera
if(!cap.isOpened()) // check if we succeeded
return -1;
Mat edges;
namedWindow("edges",1);
for(;;)
{
Mat frame;
cap >> frame; // get a new frame from camera
cvtColor(frame, edges, CV_BGR2GRAY);
GaussianBlur(edges, edges, Size(7,7), 1.5, 1.5);
Canny(edges, edges, 0, 30, 3);
imshow("edges", edges);
if(waitKey(200) >= 0) break;
}
// the camera will be deinitialized automatically in VideoCapture destructor
return 0;
}
The program compiles fine, but when I try to run it, I see the green light on my webcam come on for a few seconds, then the program exits with the error message:
OpenCV Error: Bad flag (parameter or structure field) (Unrecognized or unsupported array type) in cvGetMat, file /Users/mark/Downloads/OpenCV-2.1.0/src/cxcore/cxarray.cpp, line 2476
terminate called after throwing an instance of 'cv::Exception'
what(): /Users/mark/Downloads/OpenCV-2.1.0/src/cxcore/cxarray.cpp:2476: error: (-206) Unrecognized or unsupported array type in function cvGetMat
Under debug mode, the matrix still seems to be empty after the cap >> frame line.
I get similar behavior when I try to capture from a video file or an image, so it's not the camera. What is wrong, do you think? Anything I can do to make this work?
EDIT: I'd like to add that if I use the C features, everything works fine. But I would like to stick with C++ if I can.
Thanks
I've seen the same problem. When I use the C features, sometimes the similar question also comes up. From the error message of the C code, I think it happened because the camera got a NULL frame. So I think it can be solved in this way:
do
{
capture>>frame;
}while(frame.empty());
That way it works on my machine.
I encountered the same problem, it seems that the first two attempts to get the video wont return any signal, so if you try to use it you'll get an error, here is how I got around this, simply by adding a counter and checking the size of the video.
int cameraNumber = 0;
if ( argc > 1 )
cameraNumber = atoi(argv[1]);
cv::VideoCapture camera;
camera.open(cameraNumber);
if ( !camera.isOpened() ) {
cerr << "ERROR: Could not access the camera or video!" << endl;
exit(1);
}
//give the camera 40 frames attempt to get the camera object,
//if it fails after X (40) attemts the app will terminatet,
//till then it will display 'Accessing camera' note;
int CAMERA_CHECK_ITERATIONS = 40;
while (true) {
Mat cameraFrame;
camera >> cameraFrame;
if ( cameraFrame.total() > 0 ) {
Mat displayFrame( cameraFrame.size(), CV_8UC3 );
doSomething( cameraFrame, displayFrame );
imshow("Image", displayFrame );
} else {
cout << "::: Accessing camera :::" << endl;
if ( CAMERA_CHECK_ITERATIONS > 0 ) CAMERA_CHECK_ITERATIONS--;
if ( CAMERA_CHECK_ITERATIONS < 0 ) break;
}
int key = waitKey(200);
if (key == 27) break;
}
Try simplifying the program so that you can identify the exact location of the problem, e.g. change your loop so that it looks like this:
for(;;)
{
Mat frame;
cap >> frame; // get a new frame from camera
// cvtColor(frame, edges, CV_BGR2GRAY);
// GaussianBlur(edges, edges, Size(7,7), 1.5, 1.5);
// Canny(edges, edges, 0, 30, 3);
// imshow("edges", edges);
imshow("edges", frame);
if(waitKey(200) >= 0) break;
}
If that works OK then try adding the processing calls back in, one at a time, e.g
for(;;)
{
Mat frame;
cap >> frame; // get a new frame from camera
cvtColor(frame, edges, CV_BGR2GRAY);
// GaussianBlur(edges, edges, Size(7,7), 1.5, 1.5);
// Canny(edges, edges, 0, 30, 3);
imshow("edges", edges);
if(waitKey(200) >= 0) break;
}
and so on...
Once you've identified the problematic line you can then focus on that and investigate further.
Go to project->project properties->configuration properties->linker->input
In the additional dependencies paste cv210.lib cvaux210.lib cxcore210.lib highgui210.lib
Hi I got the solution for you :)
VideoCapture san_cap(0);
if (san_cap.isOpened()) {
while (1) {
san_cap.read(san);
imshow("Video", san);
Mat frame;
san_cap.read(frame); // get a new frame from camera
cvtColor(frame, edges, CV_BGR2GRAY);
imshow("Video2", edges);
int key = cv::waitKey(waitKeyValue);
if (key == 27 ) {
break;
}
}
}