How to open webcam in opencv2.4.6.1 on ubuntu12.04? - c++

I am using opencv2.4.6.1 on Ubuntu 12.04 LTS. I am new to opencv and have been trying to understand the sample programs in the opencv docs. I am trying to work on a project which takes a picture from a USB webcam (Kinamax Night Vision Camera) and do some image processing on it. I came across a sample code that is shown below:
#include "cv.h"
#include "highgui.h"
#include <stdio.h>
// A Simple Camera Capture Framework
int main()
{
CvCapture* capture = cvCaptureFromCAM( CV_CAP_ANY );
if ( !capture ) {
fprintf( stderr, "ERROR: capture is NULL \n" );
getchar();
return -1;
}
// 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 );
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;
}
On compiling using:
g++ trycam.c -o trycam `--cflags --libs opencv`
It gives no errors.
When I try to run it using : ./trycam Nothing shows up! Literally Nothing.
On searching google and some other posts in the stackoverflow community, I tried updating the libraries and install other dependencies like ffmpeg,GTK, Gstreamer,etc. I understand that the webcam I have connected via USB is not supported as per the list of webcams supported by linux opencv in the link here. Even my default webcam that is in my HP Pavilion dv6000 is not opening.
Is there a way I could get around this? Kindly help me out.

Related

OpenCV in Ubuntu does not show window

I installed a fresh Ubuntu. Downloaded Eclipse via the Shop, installed the CDT plugin via the Plugin Manager in Eclipse (Kepler). I used the Shop to download the OpenCV dev package. After adding the paths in eclipse I wrote a short program.
#include <iostream>
#include "opencv2/opencv.hpp"
int main(int argc, const char * argv[])
{
cvNamedWindow( "result", CV_WINDOW_AUTOSIZE );
CvCapture* capture = cvCaptureFromCAM(-1);
IplImage *newImg;
while(true)
{
newImg = cvQueryFrame( capture );
if( newImg==0 )
break;
cvShowImage( "result", newImg );
}
return 0;
}
The program compiles and the debugger shows some values in newImg. But there is no window coming up and shows the result. The camera LED lights, a step through the loop seem to work perfect. Only the output window is missing. The same program runs perfect in XCode on OS X.
Just add small wait between execution of subsequent loops. Use cv::waitKey for this purpose.
#include <iostream>
#include "opencv2/opencv.hpp"
int main(int argc, const char * argv[])
{
cvNamedWindow( "result", CV_WINDOW_AUTOSIZE );
CvCapture* capture = cvCaptureFromCAM(-1);
IplImage *newImg;
while(true)
{
newImg = cvQueryFrame( capture );
if( newImg==0 )
break;
cvShowImage( "result", newImg );
cv::waitKey(100); //Wait of 100 ms
}
return 0;
}

opencv qt display Video

This code to display a video using opencv with Visual studio
i have been looking everywhere for a tutorial how to use Qt with opencv to display video
but i couldn't find any :/
is there anyone here knows how to do that?
#include <opencv\highgui.h>
#include <opencv\cv.h>
int main(int argc, char** argv)
{
CvCapture* capture1 = cvCreateFileCapture("c:\\VideoSamples\\song.avi");
IplImage* frame1;
cvNamedWindow( "display video1", CV_WINDOW_AUTOSIZE );
while(1)
{
frame1 = cvQueryFrame( capture1 );
cvSmooth( frame1, out, CV_GAUSSIAN, 17, 17 );
if( !frame1 ) break;
cvShowImage( "display video1", frame1 );
char c = cvWaitKey(33);
if( c == 27 ) break;
}
cvReleaseCapture( &capture1 );
cvDestroyWindow( "display video1" );
}
You can easily display a cv::Mat in a QLabel:
Assuming frame is your current RGB-videoframe with 8bit depth as cv::Mat-object and label is a pointer to your QLabel:
//convert to QPixmap:
QPixmap pixmap = QPixmap::fromImage(QImage((uchar*)frame.data, frame.cols, frame.rows, frame.step, QImage::Format_RGB888));
//set scaled pixmap as content:
label->setPixmap(pixmap.scaled(frame.cols, frame.rows, Qt::KeepAspectRatio));
For starters, you've got to make sure that the OpenCV libraries you are using have been built with Qt support.
You will probably need to download the source code (available on Github), configure the build using CMake, and re-build them yourself. Here is the link to the guide on how to build the OpenCV libraries from source.
Once that is done, this is an example of how to capture frames from a camera (just swap camera with file for your case) and display the frames to a window, making use of the Qt framework.
Hope this helps you.

opencv - cvCaptureFromCAM(CV_CAP_DSHOW) capture blank frame

I am using OpenCV 2.1 and Visual Studio 2008 in Windows. I am trying to grab the frames from CCD camera and want to display on Windows. Camera is with PAL format. Camera is detecting but showing the blank grey screen.
I found many post related to blank screen but no one is work in my case. So post I post this question.
Below is my code:
#include "stdafx.h"
#include "cv.h"
#include "cxcore.h"
#include "highgui.h"
#include <iostream>
int main(int argc, char* argv[])
{
cvNamedWindow( "mywindow", CV_WINDOW_AUTOSIZE );
CvCapture* capture = cvCaptureFromCAM(CV_CAP_DSHOW);
if ( !capture ) {
fprintf( stderr, "ERROR: capture is NULL \n" );
getchar();
return -1;
}
while ( 1 ) {
IplImage* frame = cvQueryFrame( capture );
if ( !frame ) {
fprintf( stderr, "ERROR: frame is null...\n" );
getchar();
break;
}
else
{
fprintf( stderr, "Size of camera frame %d X %d\n",frame->width,frame->height );
}
cvShowImage( "mywindow", frame );
if ( (cvWaitKey(10) & 255) == 27 ) break;
}
// Release the capture device housekeeping
cvReleaseCapture( &capture );
cvDestroyWindow("mywindow");
return 0;
}
Above code return frame size 320 X 240 but blank screen.
Code is working fine for usb webcam with code CvCapture* capture = cvCaptureFromCAM(1);
I am using Avermedia Gold Camera Card on my board. So Do I need SDK to use this camera or is there any option to use CCD camera??
Driver is installed correctly and check with EzCaptureVC application.
OpenCV needs to support your camera else there's no guarantee its going to work: check the compatibility list.
Also 2.1 it's very outdated. I suggest you try again with the 2.3.1 since there has been some improvements in this area.

Wanting to live-mirror video in OpenCV on OSX, not sure where to start

If it isn't already obvious, this is my first day playing around with OpenCV.
What I am hoping to do is mirror frame2, and then upsample it.
I am not sure how to use a matrix operation on these frames which are of type IplImage. How could I mirror my frame2, and then upsample it to the Webcam2 window? Below is my code:
#include "cv.h"
#include "highgui.h"
#include <stdio.h>
// A Simple Camera Capture Framework
int main() {
CvCapture* capture = cvCaptureFromCAM( CV_CAP_ANY );
if ( !capture ) {
fprintf( stderr, "ERROR: capture is NULL \n" );
getchar();
return -1;
}
// Create a window in which the captured images will be presented
cvNamedWindow( "Webcam", CV_WINDOW_AUTOSIZE );
cvNamedWindow( "Webcam2", CV_WINDOW_AUTOSIZE );
// Show the image captured from the camera in the window and repeat
while ( 1 ) {
// Get one frame
IplImage* frame = cvQueryFrame( capture );
IplImage* frame2 = cvCreateImage(cvSize (frame->width*2, frame->height*2),
frame->depth, frame->nChannels);
cvPyrUp (frame, frame2);
if ( !frame ) {
fprintf( stderr, "ERROR: frame is null...\n" );
getchar();
break;
}
cvShowImage( "Webcam", frame );
cvShowImage( "Webcam2", frame2 );
// 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( "Webcam" );
cvDestroyWindow( "Webcam2" );
return 0;
}
There is a neat function in OpenCV, called flip(). The C counterpart is named cvFlip(). And I am sure it will help you.
And I will also give you the advice I always give: Move from the C interface to C++! Much cleaner, much safer, much easier!
You can check this answer to see the differences between the two.

OpenCv Visual C++ 2010 Express problem

Hi I am trying to run the code below but having problems with the cvCreateFileCapture function.
#include "stdafx.h"
#include <iostream>
#include <cv.h>
#include <cxcore.h>
#include <highgui.h>
using namespace std;
int main( int argc, char** argv ) {
cvNamedWindow( "Example2", CV_WINDOW_AUTOSIZE );
// CvCapture* capture = cvCaptureFromAVI( argv[1] ); // either one will work
CvCapture* capture = cvCreateFileCapture( "test.avi");
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" );
}
Initially I was getting an error about a missing msvcr90d.dll file. I had to download vs 2008 and change the platform toolset configuration settings. After I did this I got the error below. Any help would be much appreciated.
I bet cvCreateFileCapture() is failing because it didn't found the file. You just don't know because you are not checking the return of the function.
It returns NULL if it can't load the video file.
CvCapture* capture = cvCreateFileCapture("test.avi");
if (capture == NULL)
{
std::cout << "!!! cvCreateFileCapture failed !!!" << std::endl;
exit(0);
}
I have no idea what openCv is, but if memory serves me, msvcr90d.dll is the debug dll for VC++2008.
Have you checked your project's configuration and properties to see what mode and what dlls it is using/importing ?