Visual Studio 2010 console window closes immediately using opencv - c++

I'm developing a project with visual studio 2010 and opencv. Here is my problem: i acquire a video from webcam, analize it, do some operation on it and then i show the result in another window (Object Tracking). The code is ok, no compiling errors but as soon as i start the program the console windows it closes immediately and i cannot see both the original and the modified video. If i debug the code i can see the webcam works and acquire images but obviously i ned to do this in real time. Any suggestion?

Can you give any code?
Are you write and compile any video player program like this?
#include "opencv2/opencv.hpp"
using namespace cv;
int main(int, char**)
{
VideoCapture cap(0); // open the default camera
//Video Capture cap(path_to_video); // open the video file
if(!cap.isOpened()) // check if we succeeded
return -1;
namedWindow("Video",1);
for(;;)
{
Mat frame;
cap >> frame; // get a new frame from camera
imshow("Video", frame);
if(waitKey(30) >= 0) break;
}
// the camera will be deinitialized automatically in VideoCapture destructor
return 0;
}

Try this:
#include <iostream>
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
using namespace cv;
using namespace std;
int main() {
VideoCapture cap(0);
while (true)
{
Mat imgOriginal;
Mat imgHSV;
bool bSuccess = cap.read(imgOriginal);
cvtColor(imgOriginal, imgHSV, COLOR_BGR2HSV); //Convert the captured frame from BGR to HSV
imshow("Thresholded Image", imgHSV);
imshow("Original", imgOriginal);
waitKey(33);
}
return 0;
}

Related

How to get frames from webcam using OpenCV C++

I'm trying to get frames form a Logitech C920 camera
and show them.
The camera seems to be working but nothing is being shown on the display window.
I tried to configure all the camera settings but nothing.
What am I missing???
#include "pch.h"
#include <iostream>
#include "opencv2\imgcodecs.hpp"
#include "opencv2\core.hpp"
#include "opencv2\highgui.hpp"
#include "opencv2\videoio.hpp"
using namespace std;
using namespace cv;
int main()
{
VideoCapture camera(CAP_ANY);
Mat frame;
namedWindow("x", WINDOW_AUTOSIZE);
camera.set(CAP_PROP_FOURCC, VideoWriter::fourcc('M', 'J', 'P', 'G'));
camera.set(CAP_PROP_FRAME_WIDTH, 1920);
camera.set(CAP_PROP_FRAME_HEIGHT, 1080);
while (1)
{
camera.read(frame);
imshow("x", frame);
}
waitKey(0);
return 0;
}
You have to put waitKey(int delay) after each frame. Your while loop should look like this:
while (1)
{
camera.read(frame);
imshow("x", frame);
waitKey(1);
}

opencv detectMultiScale

Im trying to learn opencv and object detection. I used objecdetection.cpp in opencv samples and when I run it I get this error
The cascade loads perfectly fine and also the camera the only problem is detectmultiscale because whenever i commented it out the program doesn't crash
here is the code of objectdecetion2.cpp
#include "opencv2/objdetect.hpp"
#include "opencv2/videoio.hpp"
#include "opencv2/highgui.hpp"
#include "opencv2/imgproc.hpp"
#include <iostream>
#include <stdio.h>
using namespace std;
using namespace cv;
/** Function Headers */
void detectAndDisplay(Mat frame);
/** Global variables */
String face_cascade_name = "..\\Debug\\haarcascade_frontalface_alt.xml";
String eyes_cascade_name = "..\\Debug\\haarcascade_eye_tree_eyeglasses.xml";
CascadeClassifier face_cascade;
CascadeClassifier eyes_cascade;
String window_name = "Capture - Face detection";
/**
* #function main
*/
int main(void)
{
VideoCapture capture;
Mat frame;
//-- 1. Load the cascade
if (!face_cascade.load(face_cascade_name)){ printf("--(!)Error loading face cascade\n"); return -1; };
if (!eyes_cascade.load(eyes_cascade_name)){ printf("--(!)Error loading eyes cascade\n"); return -1; };
//-- 2. Read the video stream
capture.open(0);
if (!capture.isOpened()) { printf("--(!)Error opening video capture\n"); return -1; }
while (capture.read(frame))
{
if (frame.empty())
{
printf(" --(!) No captured frame -- Break!");
break;
}
//-- 3. Apply the classifier to the frame
detectAndDisplay(frame);
//-- bail out if escape was pressed
int c = waitKey(10);
if ((char)c == 27) { break; }
}
return 0;
}
/**
* #function detectAndDisplay
*/
void detectAndDisplay(Mat frame)
{
std::vector<Rect> faces;
Mat frame_gray;
cvtColor(frame, frame_gray, COLOR_BGR2GRAY);
equalizeHist(frame_gray, frame_gray);
face_cascade.detectMultiScale(frame_gray, faces, 1.1, 2, 0, Size(80, 80));
imshow(window_name, frame);
}
You are likely experiencing an OpenCV bug, described here: http://code.opencv.org/issues/3710
The code you posted looks OK to me, otherwise.
The code you posted is right! But I doubt that your opencv configuration is not right! If you work on windows, Please check your .dll files and lib files!

Camera Interfacing with Opencv:

I have Windows 8 on "DELL Inspiron 5537" installed and i create a virtual machine from "Virtual box" and installed Ubuntu 13.04 in it. And then I installed Opencv Version=2.4.9 through commands in Ubuntu's terminal now I want to do camera interfacing in opencv which is very necessary for my project to move forward..my code contains no error still it is not working the way i want.
My code is given below:
#include <iostream>
#include <cv.h>
#include <highgui.h>
using namespace std;
char key;
int main()
{
cvNamedWindow("Camera_Output", 1); //Create window
CvCapture* capture = cvCaptureFromCAM(CV_CAP_ANY);
//Capture using any camera connected to your system
while(1){ //Create infinte loop for live streaming
IplImage* frame = cvQueryFrame(capture); //Create image
frames from capture
cvShowImage("Camera_Output", frame); //Show image
frames on created window
key = cvWaitKey(10); //Capture Keyboard stroke
if (char(key) == 27){
//cvSaveImage("webcam_frame.png",frame);
}
}
cvReleaseCapture(&capture); //Release capture.
cvDestroyWindow("Camera_Output"); //Destroy Window
return 0;
}
when I run this code in my terminal it is not capturing the image rather it shows a blank window. I have attached a photo of output of this code.

Unable to read video file in OpenCV

I am trying to capture a video and store it in a file and then read the same video file. I am able to write it but not able to read the same file. On pressing escape, the program is supposed to quit the webcam and play the recorded video but displays the following error instead:
mpeg1video # 0x2a16f40] ac-tex damaged at 14 28
[mpeg1video # 0x2a16f40] Warning MVs not available
OpenCV Error: Bad flag (parameter or structure field) (Unrecognized or unsupported array type) in cvGetMat, file /home/ujjwal/Downloads/OpenCV-2.4.0/modules/core/src/array.cpp, line 2482
terminate called after throwing an instance of 'cv::Exception'
what(): /home/ujjwal/Downloads/OpenCV-2.4.0/modules/core/src/array.cpp:2482: error: (-206) Unrecognized or unsupported array type in function cvGetMat
The code is:
#include <sstream>
#include <string>
#include <iostream>
#include <opencv/highgui.h>
#include <opencv/cv.h>
using namespace cv;
int main(int argc, char* argv[])
{
Mat inputVideo;
Mat frame;
Mat HSV;
Mat tracking;
char checkKey;
VideoCapture capture;
capture.open(0);
capture.set(CV_CAP_PROP_FRAME_WIDTH, 640);
capture.set(CV_CAP_PROP_FRAME_HEIGHT,480);
VideoWriter writer("OutputFile.mpeg", CV_FOURCC('P','I','M','1'), 50, Size(640, 480));
while(1){
capture.read(inputVideo);
imshow("Original Video",inputVideo);
writer.write(inputVideo);
checkKey = cvWaitKey(20);
if(checkKey == 27)
break;
}
capture.open("OutputFile.mpeg");
capture.set(CV_CAP_PROP_FRAME_WIDTH, 640);
capture.set(CV_CAP_PROP_FRAME_HEIGHT,480);
while(1){
capture.read(inputVideo);
imshow("Tracking Video", inputVideo);
}
return 0;
}
Can someone please help me? Thanks!
You need to correct several things to make it work:
You have to create the window before showing images in the window.
You have to close the writer to finish writing before open it later.
You need to add cvWaitKey(20) for the second image showing (check out here for why this is essential).
The whole fixed code is as follows:
#include <sstream>
#include <string>
#include <iostream>
#include <opencv/highgui.h>
#include <opencv/cv.h>
using namespace cv;
int main(int argc, char* argv[])
{
Mat inputVideo;
Mat frame;
Mat HSV;
Mat tracking;
char checkKey;
VideoCapture capture;
capture.open(0);
capture.set(CV_CAP_PROP_FRAME_WIDTH, 640);
capture.set(CV_CAP_PROP_FRAME_HEIGHT,480);
VideoWriter writer("OutputFile.mpeg", CV_FOURCC('P','I','M','1'), 50, Size(640, 480));
namedWindow("Original Video", WINDOW_AUTOSIZE );
while(1){
capture.read(inputVideo);
imshow("Original Video",inputVideo);
writer.write(inputVideo);
checkKey = cvWaitKey(20);
if(checkKey == 27)
break;
}
writer.release();
capture.open("OutputFile.mpeg");
capture.set(CV_CAP_PROP_FRAME_WIDTH, 640);
capture.set(CV_CAP_PROP_FRAME_HEIGHT,480);
namedWindow("Tracking Video", WINDOW_AUTOSIZE );
while(1){
capture.read(inputVideo);
if (!inputVideo.empty())
{
imshow("Tracking Video", inputVideo);
checkKey = cvWaitKey(20);
}
else
break;
}
return 0;
}

OpenCV unable to capture image from isight webcam

I can not capture image from my webcam using following OpenCV code.
The code can show images from a local AVI file or a video device. It works fine on a "test.avi" file.
When I make use my default webcam(CvCapture* capture =cvCreateCameraCapture(0)), the program can detected the size of the image from webcam,but just unable to display the image.
/I forgot to mention that I can see the iSight is working because the LED indicator is turn on/
Anyone encounter the same problem?
cvNamedWindow( "Example2", CV_WINDOW_AUTOSIZE );
CvCapture* capture =cvCreateFileCapture( "C:\\test.avi" ) ;// display images from avi file, works well
// CvCapture* capture =cvCreateCameraCapture(0); //display the frame(images) from default webcam not work
assert( capture );
IplImage* image;
while(1) {
image = cvQueryFrame( capture );
if( !image ) break;
cvShowImage( "Example2", image );
char c = cvWaitKey(33);
if( c == 27 ) break;
}
cvReleaseCapture( &capture );
cvDestroyWindow( "Example2" );
opencv 2.2
Debug library *d.lib
WebCam isight
Macbook OS win7 32
VS2008
I'm working on opencv 2.3 with Macbook pro Mid 2012 and I had that problem with the Isight cam. Somehow I managed to make it work on opencv by simply adjusting the parameters of the Cvcapture and adjusting the frame width and height:
CvCapture* capture = cvCaptureFromCAM(0);
cvSetCaptureProperty( capture, CV_CAP_PROP_FRAME_WIDTH, 500 );
cvSetCaptureProperty( capture, CV_CAP_PROP_FRAME_HEIGHT, 600 );
You can also change these numbers to the frame width and height you want.
Did you try the example from the opencv page?
namely,
#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(30) >= 0) break;
}
// the camera will be deinitialized automatically in VideoCapture destructor
return 0;
}
Works on a macbook pro for me (although on OS X). If it doesn't work, some kind of error message would be helpful.
Try this:
int main(int, char**) {
VideoCapture cap(0); // open the default camera
if (!cap.isOpened()) { // check if we succeeded
cout << "===couldn't open camera" << endl;
return -1;
}
Mat edges, frame;
frame = cv::Mat(10, 10, CV_8U);
namedWindow("edges", 1);
for (;;) {
cap >> frame; // get a new frame from camera
cout << "frame size: " << frame.cols << endl;
if (frame.cols > 0 && frame.rows > 0) {
imshow("edges", frame);
}
if (waitKey(30) >= 0)
break;
}
// the camera will be deinitialized automatically in VideoCapture destructor
return 0;
}
Latest update! Problem solved!
This happen to be one of OpenCV 2.2′s bug
Here is how to fix it:
http://dusijun.wordpress.com/2011/01/11/opencv-unable-to-capture-image-from-isight-webcam/
Why dont you try
capture=cvCaptureFromCam(0);
I think this may work.
Let me know about wheather its working or not.