I am new to opencv .I am trying to read video mp4 file but my code always says cannot open video device or file. I have placed video file in code folder .
VideoCapture capture;
capture.open("a.mp4"); // Open file
if (!capture.isOpened())
{
cout << "Cannot open video device or file!" << endl;
getch();
return -1;
}
Mat frame;
namedWindow("video", CV_WINDOW_AUTOSIZE);
while(true)
{
capture >> frame;
if (frame.empty())
break;
imshow("video", frame);
if (waitKey(30) == 'q')
break;
}
I am posting this answer in case someone came across this question as I did.
I was facing almost the same problem, trying to open a video file. My code worked with .avi but failed to open files with .mp4 files.
Turns out openCV needs ffmpeg decoder to be installed on the system. In case of windows OS, you will need to copy opencv_ffmpeg2411.dll file into C:\Windows\System32 directory. The DLL different systems can be found in OpenCV extracted directory.
Hope this helps someone.
this is correct code for video and image reading. I don't know what was the exact error. But using different code from orieally help me do my task in a different way.
// newproject.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "highgui.h"
#include <stdio.h>
#include <cv.h>
#include <highgui.h>
#include <stdio.h>
#include <conio.h>
#include <opencv2/imgproc/imgproc.hpp> // Gaussian Blur
#include <opencv2/core/core.hpp> // Basic OpenCV structures (cv::Mat, Scalar)
#include <opencv2/highgui/highgui.hpp>
#include <iostream>
#include <conio.h>
using namespace cv;
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
//Image Reading
IplImage* img = cvLoadImage( "b.jpg" );
cvNamedWindow( "Example1", CV_WINDOW_AUTOSIZE );
cvShowImage( "Example1", img );
cvWaitKey(0);
cvReleaseImage( &img );
cvDestroyWindow( "Example1" );
//Video Reading
// cvNamedWindow( "Example2", CV_WINDOW_AUTOSIZE );
// CvCapture* capture = cvCreateFileCapture( "video.avi" );
// IplImage* frame;
/* while(1) {
frame = cvQueryFrame( capture );
if( !frame ) break;
cvShowImage( "Example2", frame );
char c = cvWaitKey(33);
if( c == 27 ) break;//if user enter escape key then exit
}*/
//Summarize by sampling
CvCapture* capture = 0;
capture = cvCreateFileCapture( "video.avi" );
if(!capture){
return -1;
}
IplImage *bgr_frame=cvQueryFrame(capture);//Init the video read
double fps = cvGetCaptureProperty (capture,CV_CAP_PROP_FPS);
CvSize size = cvSize((int)cvGetCaptureProperty( capture, CV_CAP_PROP_FRAME_WIDTH),(int)cvGetCaptureProperty( capture, CV_CAP_PROP_FRAME_HEIGHT));
//double fps = cvGetCaptureProperty (capture,CV_CAP_PROP_FPS);
CvVideoWriter *writer = cvCreateVideoWriter("Ayesha",CV_FOURCC('M','J','P','G'),fps,size);
int x=0;
while(1){
x++;
bgr_frame = cvQueryFrame( capture );
if(!bgr_frame)
break;
if(x==19||x==21){
cvShowImage("Example2",bgr_frame);
cvWriteFrame( writer, bgr_frame );
}
if(x==20){
cvShowImage("Example2",bgr_frame);
cvWriteFrame( writer, bgr_frame );
x=0;
}
char c=cvWaitKey(30);
if (c==27) break;
}
cvReleaseVideoWriter( &writer );
cvReleaseCapture( &capture );
cvDestroyWindow( "Example2" );
}
Related
I'm trying to get the stream and defining the ROI and tracking an object within ROI using dlib correlation tracker but there is a error in compilation, not found any code or explanation which can describe any solution of this error. Thanks in advance.
cvcap.cpp
#include <stdio.h>
#include <opencv2/opencv.hpp>
#include "dlib/image_processing.h"
#include "dlib/gui_widgets.h"
#include "dlib/image_io.h"
#include "dlib/dir_nav.h"
using namespace dlib;
using namespace std;
int main(int argc, char * argv[]){
CvCapture* camera = cvCaptureFromFile(argv[1]);
if (camera==NULL)
printf("camera is null\n");
else
printf("camera is not null\n");
cvNamedWindow("CvCapture");
while (cvWaitKey(1000)!=atoi("q")){
double t1 = (double)cvGetTickCount();
IplImage *img = cvQueryFrame(camera);
cvSetImageROI(img, cvRect(140, 150, 340, 150));
IplImage *img1 = cvCreateImage(cvGetSize(img), img->depth, img->nChannels);
cvCopy(img, img1, NULL);
cvResetImageROI(img);
cvShowImage("ROI",img1);
correlation_tracker tracker;
tracker.start_track(img1, centered_rect(point(120,100), 80, 150));
image_window win;
tracker.update(img1);
win.set_image(img1);
win.clear_overlay();
win.add_overlay(tracker.get_position());
cout << "hit enter to process next frame" << endl;
cin.get();
double t2=(double)cvGetTickCount();
printf("time: %gms fps: %.2g\n",(t2-t1)/(cvGetTickFrequency()*1000.), 1000./((t2-t1)/(cvGetTickFrequency()*1000.)));
cvShowImage("CvCapture",img1);
}
cvReleaseCapture(&camera);
}
the error is :
In file included from dlib/image_processing.h:24:0,
from cvcap.cpp:3:
dlib/image_processing/correlation_tracker.h: In instantiation of ‘dlib::point_transform_affine dlib::correlation_tracker::make_chip(const image_type&,
dlib::drectangle, std::vector<dlib::matrix<std::complex<double> > >&) const [with image_type = _IplImage*]’:
dlib/image_processing/correlation_tracker.h:65:47: required from ‘void dlib::correlation_tracker::start_track(const image_type&, const dlib::drectang
le&) [with image_type = _IplImage*]’
cvcap.cpp:36:70: required from here
dlib/image_processing/correlation_tracker.h:301:67: error: invalid use of incomplete type ‘struct dlib::image_traits<_IplImage*>’
typedef typename image_traits<image_type>::pixel_type pixel_type;
Update
i edited my code and now it is compiling but when i'm running my code, tracking box appears but it is not tracking the objects.
for(;;) {
if(!vcap.read(image)) {
std::cout << "No frame" << std::endl;
break;
} else {
std::cout << "Starting" << std::endl;
array2d<rgb_pixel> img;
assign_image(img, cv_image<rgb_pixel>(image));
tracker.start_track(img, centered_rect(point(413,260), 98, 126));
for (unsigned long i = 0; i < img.size(); i++) {
tracker.update(img);
win.set_image(img);
win.clear_overlay();
win.add_overlay(tracker.get_position());
}
}
}
You're passing an IplImage to the dlib routine. But if you look at the docs it says you have to convert your opencv images to cv_images before passing them to dlib functions. And you can see this with the error pointing out IplImage doesn't have a pixel_type trait. A fix would be.
IplImage *img1 = cvCreateImage(cvGetSize(img), img->depth, img->nChannels);
...
correlation_tracker tracker;
// Not sure if the tracker wants a single channel image
// change rgb_alpha_pixel to uchar in that case.
tracker.start_track(dlib::cv_image<dlib::rgb_alpha_pixel>(img1), centered_rect(point(120,100), 80, 150));
Pixel types and the Correlation tracker
EDIT: Passed your stuff to opencv 3 and this is compiling for me. Do you really need to use opencv 2?
#include <stdio.h>
#include <opencv2/opencv.hpp>
#include "dlib/image_processing.h"
#include "dlib/gui_widgets.h"
#include "dlib/image_io.h"
#include "dlib/dir_nav.h"
#include <dlib/opencv/cv_image.h>
using namespace cv;
using namespace dlib;
using namespace std;
int main(int argc, char * argv[]){
VideoCapture cap(0);
Mat frame;
while (cap.read(frame)) {
cvtColor(frame, frame, CV_RGB2GRAY);
cv_image<unsigned char> img(frame);
correlation_tracker tracker;
tracker.start_track(img, centered_rect(point(120,100), 80, 150));
image_window win;
tracker.update(img);
win.set_image(img);
win.clear_overlay();
win.add_overlay(tracker.get_position());
cout << "hit enter to process next frame" << endl;
cv::waitKey(0);
}
}
and with an uglier opencv 2..
#include <stdio.h>
#include <opencv2/opencv.hpp>
#include "dlib/image_processing.h"
#include "dlib/gui_widgets.h"
#include "dlib/image_io.h"
#include "dlib/dir_nav.h"
#include <dlib/opencv/cv_image.h>
using namespace cv;
using namespace dlib;
using namespace std;
int main(int argc, char * argv[]){
CvCapture* camera = cvCaptureFromFile(argv[1]);
if (camera==NULL)
printf("camera is null\n");
else
printf("camera is not null\n");
cvNamedWindow("CvCapture");
while (cvWaitKey(1000)!=atoi("q")) {
double t1 = (double)cvGetTickCount();
IplImage *img = cvQueryFrame(camera);
cvSetImageROI(img, cvRect(140, 150, 340, 150));
IplImage *img1 = cvCreateImage(cvGetSize(img), img->depth, img->nChannels);
cvCopy(img, img1, NULL);
cvResetImageROI(img);
cvShowImage("ROI",img1);
cvCvtColor(img1, img1, CV_RGB2GRAY);
cv_image<unsigned char> img2(img1);
correlation_tracker tracker;
tracker.start_track(img2, centered_rect(point(120,100), 80, 150));
image_window win;
tracker.update(img2);
win.set_image(img2);
win.clear_overlay();
win.add_overlay(tracker.get_position());
cout << "hit enter to process next frame" << endl;
cin.get();
double t2=(double)cvGetTickCount();
printf("time: %gms fps: %.2g\n",(t2-t1)/(cvGetTickFrequency()*1000.), 1000./((t2-t1)/(cvGetTickFrequency()*1000.)));
cvShowImage("CvCapture",img1);
}
cvReleaseCapture(&camera);
}
As you see the error came exactly from what I told you before.
I was following a tutorial on web for an OpenCV library to detect eyes
when i compile it this error appears I tried to fix it but didn't find a
solution.
main.cpp:59:1: error: ‘capture’ was not declared in this scope
capture = cvCaptureFromCAM( 0 );
The code is long I put only the part that I think causing this error .
: Full code
#include <opencv2/objdetect/objdetect.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <iostream>
#include <queue>
#include <stdio.h>
#include <math.h>
#include "constants.h"
#include "findEyeCenter.h"
#include "findEyeCorner.h"
/** Function Headers */
void detectAndDisplay( cv::Mat frame );
int main( )
{
cv::Mat frame;
if( !face_cascade.load( face_cascade_name ) ){ printf("--(!)Error loading face cascade, please change face_cascade_name in source code.\n"); return -1; };
createCornerKernels();
ellipse(skinCrCbHist, cv::Point(113, 155.6), cv::Size(23.4, 15.2),
43.0, 0.0, 360.0, cv::Scalar(255, 255, 255), -1);
capture = cvCaptureFromCAM( 0 );
if( capture)
{
while( true )
{
frame = cvQueryFrame( capture );
// mirror it
imshow("Video",frame);
cv::flip(frame, frame, 1);
frame.copyTo(debugImage);
// Apply the classifier to the frame
if( !frame.empty() ) {
detectAndDisplay( frame );
}
else {
printf(" --(!) No captured frame -- Break!");
break;
}
imshow(main_window_name,debugImage);
int c = cv::waitKey(10);
if( (char)c == 'c' ) { break; }
if( (char)c == 'f' ) {
imwrite("frame.png",frame);
}
}
}
releaseCornerKernels();
return 0;
}
}
try to use VideoCapture capture(0);
I'm new to openCV, and I finally got it to open a window and show the webcam output.
I'm now trying to convert frames to greyscale before displaying them. Unfortunately, I get the following error every time I try to compile. error: ‘cvCvtColor’ was not declared in this scope I'm using the following line to compile
g++ main.cpp -lopencv_core -lopencv_imgproc -lopencv_highgui -lopencv_objdetect -o camera
Am I linking my code incorrectly? Here's the full code listing. Sorry for my formatting issues!
#include "opencv2/objdetect/objdetect.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/core/core.hpp"
#include <stdio.h>
#include <unistd.h>
IplImage* getCameraFrame(CvCapture* &camera)
{
IplImage *frame;
int w, h;
// If the camera hasn't been initialized, then open it.
if (!camera) {
printf("Acessing the camera ...\n");
camera = cvCreateCameraCapture( 0 );
if (!camera) {
printf("Couldn't access the camera.\n");
exit(1);
}
// Try to set the camera resolution to 320 x 240.
cvSetCaptureProperty(camera, CV_CAP_PROP_FRAME_WIDTH, 320);
cvSetCaptureProperty(camera, CV_CAP_PROP_FRAME_HEIGHT, 240);
// Get the first frame, to make sure the camera is initialized.
frame = cvQueryFrame( camera );
if (frame) {
w = frame->width;
h = frame->height;
printf("Got the camera at %dx%d resolution.\n", w, h);
}
// Wait a little, so that the camera can auto-adjust its brightness.
sleep(1); // (in milliseconds)
}
// Wait until the next camera frame is ready, then grab it.
frame = cvQueryFrame( camera );
if (!frame) {
printf("Couldn't grab a camera frame.\n");
exit(1);
}
return frame;
}
int main(int argc, char* argv[])
{
cvNamedWindow( "Example2", CV_WINDOW_AUTOSIZE );
CvCapture* capture = cvCreateCameraCapture( 0 );
IplImage* frame;
while(1) {
frame = cvQueryFrame( capture );
if( !frame ) break;
cvCvtColor(frame, frame, CV_RGB2GRAY);
cvShowImage( "Example2", frame );
char c = cvWaitKey(33);
if( c == 27 ) break;
}
cvReleaseCapture( &capture );
cvDestroyWindow( "Example2" );
return 0;
}
you're using the headers for the c++ api, but code from the deprecated c-api ( please don't !! ).
cv::VideoCapture cap(0);
while(cap.IsOpened()) {
cv::Mat f,g;
if ( ! cap.read(f) ) break;
cv::cvtColor(f, g, CV_RGB2GRAY);
cv::imshow("lalala",g);
if ( waitKey(10) == 27 ) break;
}
return 0;
I have configure an Openc CV include file path in Tool Setting, but it still gives warning in my code.
warning is :
Unresolved inclusion: <highgui.h>
my code is :
#include <cv.h>
#include <highgui.h>
using namespace cv;
int main()
{
Mat image;
image = imread( argv[1], 1 );
if( argc != 2 || !image.data )
{
printf( "No image data \n" );
return -1;
}
namedWindow( "Display Image", CV_WINDOW_AUTOSIZE );
imshow( "Display Image", image );
waitKey(0);
return 0;
}
You need to use a different include if you work with cv::Mat:
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
I cannot use webcam as input device for OpenCV 2.3.1 in Ubuntu 11.04, this code works fine on windows:
#include "cv.h"
#include "highgui.h"
#include <stdio.h>
// A Simple Camera Capture Framework
int main() {
CvCapture* capture = cvCaptureFromCAM(-1);
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;
}
It returns "ERROR: capture is NULL "
I found the solution, I need to install libv4l-0 and libv4l-dev then compile OpenCV with USE_V4L=ON.