I have build OpenCV 2.4.1 on Ubuntu 12.04 32 bit platform with OpenGl,Qt and OpenNI but whenever I am running example programs listed in the Learning OpenCV Book.
For Example:
#include "highgui.h"
int main( int argc, char** argv ) {
cvNamedWindow( "Example2", CV_WINDOW_AUTOSIZE );
//CvCapture* capture = cvCaptureFromAVI( argv[1] );
CvCapture* capture = cvCreateFileCapture( argv[1] );
IplImage* frame;
while(1) {
frame = cvQueryFrame( capture );
if( !frame ) break;
cvShowImage( "Example2", frame );
char c = cvWaitKey(33);
if( c == 27 ) break;
}
cvReleaseCapture( &capture );
cvDestroyWindow( "Example2" );
}
I get this message in the console:
init done
opengl support available
I wonder where I am going wrong.I am not getting any errors in compilation.
This is not an error. I have a similar configuration on my machine and I see these statements every time I run something. These statements have nothing to do with what you have programmed. I have run your exact code and it displayed the video without any problems. Perhaps add this error check after you open the capture to make sure it found the video:
if (!capture) {
std::cout << "COULD NOT OPEN CAPTURE\n";
}
I was having the same problem and then I added waitKey(0) at the end and the image was displayed.
Related
Problem description:
With the simple code I can access my internal camera webcam and also change the resolution. So displaying the frame with default resolution and with a predefined resolution (e.g., from 640x480 to 320x240, with cap.set(CV_CAP_PROP_FRAME_WIDTH,320), and FRAME_HEIGHT, 240)) – both work fine.
Using the same code with slight adaption so that it works with a Ximea camera (VideoCapture cap(CV_CAP_XIAPI)), does work for the default resolution.
It is a MU9PC_MH with a default resolution of 2592x1944, so 648 x486 is the lower resolution which is required.
For a manually set resolution the displayed window/grabbed frame has the size of the default resolution (2592x1944) and shows the lower amount of pixels of the capture in the this huge display, so that the upper fifths is filled with puzzled pixels and the rest of the window is black.
This effect happens for both, C++ with VideoCapture and Mat and for C CvCaptureFromCAM and IplImage*.
When I’m using the set flag CV_CAP_PROP_XI_DOWNSAMPLING, then I can see the output image with pixels in correct order, but the display frame has still the default high resolution and the output image is shown multiple times (the factor depends on the factor I am using for downsampling).
I’m even not able to force the Mat or IplImage to a predefined size, because then an error of assertion or access violation occurs(Mat image(XRES, YRES, CV_8UC3, Scalar(0,0,255)) (frame = cvCreateImage(cvSize( XRES, YRES), IPL_DEPTH_8U,3);. I've checked through frame.type(), that the output is an CV_8UC3
What I want is a Ximea camera output of 648x486 shown in an (ideally) Mat of same size.
Did anyone experience the same problem?
Probably it is due to a lack in my knowledge about industrial camera configuration/development, but any help is appreciated.
Situation:
Windows 7(x64)
Visual Studio Professional 2012
OpenCV Version 2.4.10 compiled for x86 with following flags checked: WITH_XIMEA and WITH_OPENGL
Simple VS2012 Project in x86 (both release and debug) for camera streaming and displaying of camera frame in window.
Simple Code Snippets(not mine, copied from opencv tutorials and ximea):
C++-Style:
#include "opencv2/core/core.hpp"
#include "opencv2/highgui/highgui.hpp"
#include <iostream>
using namespace cv;
using namespace std;
int main(int argc, char **argv) {
VideoCapture cap(0); // open the default camera
//VideoCapture cap(CV_CAP_XIAPI);
if(!cap.isOpened()) // check if we succeeded
return -1;
cout<<" "<<cap.get(CV_CAP_PROP_FRAME_WIDTH)<<" "<<cap.get(CV_CAP_PROP_FRAME_HEIGHT)<<endl; //output: 640, 480
cap.set(CV_CAP_PROP_FRAME_WIDTH,XRES);
cap.set(CV_CAP_PROP_FRAME_HEIGHT,YRES);
cout<<" "<<cap.get(CV_CAP_PROP_FRAME_WIDTH)<<" "<<cap.get(CV_CAP_PROP_FRAME_HEIGHT)<<endl; //output: 320, 240
for(;;)
{
Mat frame;
cap >> frame; // get a new frame from camera
imshow("camera frame", frame);
if(waitKey(30) == 27) //wait for 'esc' key press
{
cout << "esc key is pressed by user" << endl;
break;
}
}
return 0;}
C-Style:
#include "cv.h"
#include "highgui.h"
#include <stdio.h>
#include <iostream>
using namespace cv;
using namespace std;
// A Simple Camera Capture Framework
int main()
{
CvCapture* capture = cvCaptureFromCAM( CV_CAP_XIAPI );
if ( !capture ) {
fprintf( stderr, "ERROR: capture is NULL \n" );
getchar();
return -1;
}
cvSetCaptureProperty( capture, CV_CAP_PROP_FRAME_WIDTH, 648 );
cvSetCaptureProperty( capture, CV_CAP_PROP_FRAME_HEIGHT, 486 );
// Create a window in which the captured images will be presented
cvNamedWindow( "mywindow", CV_WINDOW_AUTOSIZE );
// Show the image captured from the camera in the window and repeat
while ( 1 ) {
// Get one frame
IplImage* frame = cvQueryFrame( capture );
cout<<cvGetCaptureProperty(capture, CV_CAP_PROP_FRAME_WIDTH); //output: 648
cout<<cvGetCaptureProperty(capture, CV_CAP_PROP_FRAME_HEIGHT); //output: 486
cout<<frame->height<<frame->width<<endl; //output: 1944, 2592
if ( !frame ) {
fprintf( stderr, "ERROR: frame is null...\n" );
getchar();
break;
}
cvShowImage( "mywindow", frame );
// Do not release the frame!
//If ESC key pressed, Key=0x10001B under OpenCV 0.9.7(linux version),
//remove higher bits using AND operator
if ( (cvWaitKey(10) & 255) == 27 ) break;
}
// Release the capture device housekeeping
cvReleaseCapture( &capture );
cvDestroyWindow( "mywindow" );
return 0;
}
thank you karlphillip for your answer. Unfortunately, you are right that this was not an ideal way. So I take yesterday's snowy wheather and found the solution myself:
There's a mistake in the resetCvImage() method of the cap_ximea.cpp.
line 207 if( (int)image.width != width || (int)image.height != height
|| image.frm != (XI_IMG_FORMAT)format)
has to be:
if( (int)image.width != **frame->**width || (int)image.height != **frame->**height || image.frm != (XI_IMG_FORMAT)format)
VideoCapture::set() returns a bool to indicate the success of the method. You shouldn't let your application continue to run without checking the success/failure of this call and readjusting the size of the capture when necessary.
The fact is that some camera drivers don't accept arbitrary dimensions, and there's simply nothing you can do about it. However, you can retrieve the frame using the default size and then cv::resize() it to your needs.
It's not ideal, but it will get the job done.
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."
I am using CodeBlocks in my windows 7 64 bit and I use MinGw for my default c/c++ compiler.
Few days ago I need to use OpenCV, after I struggle a lot of error, I get unsolveable error like this :
The sample code:
#include "cv.h"
#include "highgui.h"
int main( int argc, char** argv ) {
IplImage* img = cvLoadImage( argv[1] );
cvNamedWindow( "Example1", CV_WINDOW_AUTOSIZE );
cvShowImage( "Example1", img );
cvWaitKey(0);
cvReleaseImage( &img );
cvDestroyWindow( "Example1" );
}
I believe my linked & directory setting is correct. So please help me :) I am about to give up :(
Assuming that you are doing everything correct in the code and the image, this can be a problem due to incompatible opencv binaries.
Please have a look at a similar installation to compile and see if it works. I had a similar problem in my installation, which was fixed by compiling the binaries again.
The problem is most probably a failure when loading the image. But you will only be certain if you check the return of cvLoadImage():
IplImage* img = cvLoadImage( argv[1] );
if (!img)
{
printf("!!! cvLoadImage failed\n");
}
The function fails if the image format is not supported, or if the image is not found in the specified location.
You application expects to load the file passed from the command line, so you better execute your application with: Main.exe C:\some_img.png
You can also hardcode the filename in your code:
IplImage* img = cvLoadImage("C:\\some_img.png");
if (!img)
{
printf("!!! cvLoadImage failed\n");
}
I have searched a lot about my simple problem but I didn't find solution. When I run my code black console shows me the camera frame size but in the window video is not showing, it shows a solid gray screen. But if I play a video from HDD then it works fine.
Please help me some one.
This is my code
#include <iostream>
#include <cv.h>
#include <cxcore.h>
#include <highgui.h>
using namespace std;
int main(int argc, char** argv){
CvCapture *capture;
IplImage* img=0;
cvNamedWindow("Window");
capture = cvCreateCameraCapture( -1);
//capture = cvCaptureFromAVI("1.mp4");
//capture = cvCaptureFromCAM(-1);
int ext=0;
assert( capture );
if(capture==NULL){
cout<<"Cam Not Found!!!"<<endl;
getchar();
return -5;
}
while ( true ){
img = cvQueryFrame( capture );
cvSaveImage("1.jpg",img);
if (!img){
printf("Image not Found\n");
break;
}
cvShowImage("Window", img);
cvWaitKey(50);
}
cvReleaseImage(&img);
cvDestroyWindow("Window");
cvReleaseCapture(&capture);
return 0;
}
I use opencv 2.2 and Visual studio 2010
One thing is obviouslly wrong, you need to change the order of the calls to:
cvShowImage("Window", img);
cv::waitKey(20);
Second, it's essential that you check the success of cvQueryFrame():
img = cvQueryFrame( capture );
if (!img)
{
// print something
break;
}
EDIT:
By the way, I just noticed you are mixing the C interface of OpenCV with the C++ interface. Don't do that! Replace cv::waitKey(50); by cvWaitKey(50);.
For debugging purposes, if cvQueryFrame() succeeds I suggest you store one frame to the disk with cvSaveImage(), and if that image is OK it means the capture procedure is actually working perfectly and the problem is somewhere else.
I jast switch the openCV version 2.2 to 2.1 and its work perfectly.......
I am using OpenCV version 3.1, I got the same problem, I re-built openCV 3.1 and re-checked Environment Variables, so my problem resolved. You can back-up built-opencv and extract if you need. Sorry for my bad english :)
I ran into a routine that converts from IplImage to QImages in Qt, i tried it and it works perfects, after that i tried to display a video in a label using also Iplframes, it also worked, but now im trying to display live video from my webcam and im running into some kind of trouble because it doesnt display anything, Opencv 2.3 , Ubuntu Linux C++
CvCapture* capture = cvCreateFileCapture( argv[1] );
//CvCapture* capture = cvCaptureFromCAM( 0 );
while(1) {
frame = cvQueryFrame( capture );
cvWaitKey(33);
if( !frame ) break;
cvCvtColor(frame,frame,CV_BGR2RGB);
myImage = QImage((unsigned char *)frame->imageDataOrigin,frame->width,frame->height,QImage::Format_RGB888);
myLabel.setPixmap(QPixmap::fromImage(myImage));
myLabel.show();
//sleep(1);
Sleeper::msleep(33);
}
There i have the 2 options , capturefromcam or capturefromavi, from an avi video it converts and displays converted frames perfectly, but when i try the same thing for my webcam's captured frames it doesnt display anything, also i dont get any error or something like that, any idea?
From the looks of it, cvCaptureFromCAM() failed to find a device at index 0. But you don't know this because you are not coding defensively: cvCaptureFromCAM() returns NULL when it fails to access a device:
CvCapture* capture = cvCaptureFromCAM( 0 );
if (!capture)
{
// print error
// quit application
}
Try passing CV_CAP_ANY or experiment with other indexes: 1, 2, 3, and if none of them work I suggest you check the compatibility list and verify is your camera is supported by OpenCV.
The same attention should be payed with cvQueryFrame():
frame = cvQueryFrame( capture );
if (!frame)
{
// print error
// quit application
}