I am trying to read live feed using OpenCV, I previously used read videos which were already converted to .avi , but how does it work IF I try to read a live feed which is in raw format ?
oh, easy then:
VideoCapture cap;
cap.open(0); // open camera 0, or /dev/video0
VideoCapture cap;
cap.open("/home/me/my.avi"); // a video file
VideoCapture cap;
cap.open("http://dummy.url?stream=mpeg"); // a mjpeg , ipcam stream
// whatever, from here on you get a 'raw' 24bit bgr stream:
if ( cap.isOpened() ) {
Mat frame;
if ( ! cap.read(frame) ) // end of stream
return -1;
}
Related
I am using new logitech camera c920 for my project to do object recognition .
My camera can support H264 codec and can display H264 HD output.
But How I can set CODEC type as H264 in my below code to get out put as H264 DECODED STREAM
by using OpenCV instruction .
I am capturing video by using below logic : ref:this link
#include "opencv2/opencv.hpp"
using namespace cv;
int main(int, char**)
{
VideoCapture cap(0); // open the default camera
if(!cap.isOpened()) // check if we succeeded
return -1;
for(;;)
{
Mat frame;
cap >> frame; // get a new frame from camera
imshow("display", frame);
if(waitKey(30) >= 0) break;
}
// the camera will be deinitialized automatically in VideoCapture destructor
return 0;
}
By setting the fourCC property, you should be telling VideoCapture that your source is h.264. All the docs for openCV say that you will get decoded BGR data out though.
cap.set(CV_CAP_PROP_FOURCC, CV_FOURCC('H', '2', '6', '4'));
I wrote a simple OpenCV program that recovers my webcam video stream and display it on a simple window. I wante to resize this window to the resolution 256x256 but it changed it to 320x240.
Here's my source code :
#include <iostream>
#include <opencv/cv.h>
#include <opencv/highgui.h>
using namespace std;
int main(int argc, char** argv)
{
char key;
cvNamedWindow("Camera_Output", cv::WINDOW_NORMAL);
CvCapture *capture = cvCaptureFromCAM(CV_CAP_ANY);
cvSetCaptureProperty(capture, CV_CAP_PROP_FRAME_WIDTH, 256);
cvSetCaptureProperty(capture, CV_CAP_PROP_FRAME_HEIGHT, 256);
while(1){
IplImage *frame = cvQueryFrame(capture);
cvShowImage("Camera_Output", frame);
key = cvWaitKey(10);
if (key == 27){
break;
}
}
cvReleaseCapture(&capture);
cvDestroyWindow("Camera_Output");
return 0;
}
The output resolution is 320x240 and I want a 256x256 resolution. I think it's not possible because the camera manages its output video stream buffer and it has to keep the same ratio (width/height). What do you think about this idea ?
Is there a function which can force the resolution as a square resolution using OpenCV ?
Thanks a lot in advance for your help.
Seems like you video source does not handle 256x256 resolution. If you want to display it as such, you will have to crop the image yourself before displaying it.
Simple, you can do this by:
VideoCapture cap;
cap.open(0); // open your web-camera
cap.set(CV_CAP_PROP_FRAME_WIDTH, 256);
cap.set(CV_CAP_PROP_FRAME_HEIGHT, 256);
If this doesn't work, you need to resize it manually by calling cv::resize().
The below is used to get the live video feed from the 4 webcam. But all the code i have written is limited only for four camera. I want to make this code dynamic that is. If two camera is connected it should only display two windows, if 3 are connected it should show 3 window. I am getting the number of camera attached to the System. I want to implement the code for that number of camera. Can someone please point me in right direction.
using namespace cv;
int main(){
//Create matrix to store image
Mat image;
Mat image1;
Mat image2;
Mat image3;
//Mat image1;
//initailize capture
videoInput VI;
int numDevices = VI.listDevices(); //getting number of camera attached to system dynamically.
VideoCapture cap;
VideoCapture cap1;
VideoCapture cap2;
VideoCapture cap3;
bool x = false;
//VideoCapture cap1;
cap.open(0);
cap1.open(1);
cap2.open(2);
cap3.open(3);
namedWindow("Camera 1",1);
namedWindow("Camera 2",1);
namedWindow("Camera 3",1);
namedWindow("Camera 4",1);
while(1){
//copy webcam stream to image
cap >> image;
cap1 >> image1;
cap2 >> image2;
cap3 >> image3;
cap4 >> image4;
//cap1 >> image1;
imshow("Camera 1",image);
imshow("Camera 2",image1);
imshow("Camera 3",image2);
imshow("Camera 4",image3);
//imshow("Camera 2",image1);
//delay 33ms
waitKey(33);
}
}
The link to a webcam is usually a direct http link. So, just create a static html file that has individual frame for each webcam link, arrange these frames the way that you want to view by using html code. Launch this static html page in your embedded browser of your application.
I've just written a first program for videocaptur and videowriter. I copied the source from the wiki and changed the only video file name, but it made error.
Here is the source from the wiki.
The opencv is 2.1 and the compiler is visual c++ 2008 express.
#include "cv.h"
#include "highgui.h"
using namespace cv;
int main(int, char**)
{
VideoCapture capture(1); // open the default camera
if( !capture.isOpened() ) {
printf("Camera failed to open!\n");
return -1;
}
Mat frame;
capture >> frame; // get first frame for size
// record video
VideoWriter record("RobotVideo.avi", CV_FOURCC('D','I','V','X'), 30, frame.size(), true);
if( !record.isOpened() ) {
printf("VideoWriter failed to open!\n");
return -1;
}
namedWindow("video",1);
for(;;)
{
// get a new frame from camera
capture >> frame;
// show frame on screen
imshow("video", frame);
// add frame to recorded video
record << frame;
if(waitKey(30) >= 0) break;
}
// the camera will be deinitialized automatically in VideoCapture destructor
// the recorded video will be closed automatically in the VideoWriter destructor
return 0;
}
With the source, I changed 2 parts. One is for VideoCapture. (I don't have tunercard or camera.) The source is
VideoCapture capture(1); // open the default camera
and changed to
VideoCapture capture("C:/Users/Public/Videos/Sample Videos/WildlifeTest.wmv");
And the other is for VideoWriter:
// record video
VideoWriter record("RobotVideo.avi", CV_FOURCC('D','I','V','X'), 30, frame.size(), true);
and changed to
VideoWriter record("C:/Users/Public/Videos/Sample Videos/WildlifeRec.wmv",
CV_FOURCC('W','M','V','1'), 30,frame.size(), true);
and the part of error is:
// add frame to recorded video
record << frame;
Please show me what is my mistake!
P.S.
when I delete the line record << frame;, it works well. I think the error caused at the line.
And I found that even if without change, the wiki source program make same error.
The first error that i see is the file paths. You have to give them like this : C:\\Users\\....
please make sure you opencv_ffmpegXXX.dll work right
I am trying to create an OpenCV application on my MacBook with built-in iSight camera. I grabbed some very simple code off the internet and ran it with no trouble. OpenCV automatically discovered the built-in webcam and ran properly but I can't get it to work with my USB webcam.
#include <stdio.h>
#include <opencv.hpp>
int main( int argc, char **argv )
{
CvCapture *capture = 0;
IplImage *frame = 0;
int key = 0;
/* initialize camera */
capture = cvCaptureFromCAM(0);
/* always check */
if ( !capture ) {
fprintf( stderr, "Cannot open initialize webcam!\n" );
return 1;
}
/* create a window for the video */
cvNamedWindow( "Test", CV_WINDOW_AUTOSIZE );
while( key != 'q' ) {
/* get a frame */
frame = cvQueryFrame( capture );
/* always check */
if( !frame ) break;
/* display current frame */
cvShowImage( "Test", frame );
/* exit if user press 'q' */
key = cvWaitKey( 1 );
}
/* free memory */
cvDestroyWindow( "Test" );
cvReleaseCapture( &capture );
return 0;
}
I compiled this with:
g++ webcam.c -o webcam -I/opt/local/include/opencv2 -I/opt/local/include -L/opt/local/lib -lopencv_core -lopencv_highgui
According to the documentation, by changing the line capture = cvCaptureFromCAM(0); to
capture = cvCaptureFromCAM(1); I should be able to access the other webcam that I have plugged in but running the program gives me the error message: Warning: Max Camera Num is 0; Using camera 0
What steps can I take to get OpenCV to recognize that I have another camera connected to my USB drive?
This is based on windows experience, but I believe the main issue to be the same. (To get input from a logitech USB-camera on my laptop.)
As far as I recall; OpenCV does not support multiple cameras, and thereby not the choice of camera.
I am guessing that you can easily run your build-in camera with the code you've shown.
My solution to a similar problem was deactivating the build-in camera. Giving the USB camera the only "availavle" slot for your cvCaptureFromCAM(0) function.
I hope this can solve your problem, even though the solution is a little 'clunky'.