Cannot open videos in Opencv - opencv

I am trying to open .avi file in opencv. When I run the code I do not get any error but the video does not play. I searched around and I guess it requires ffmpeg to be installed. So I installed it using
sudo apt-get install libavformat-dev libavcodec-dev libavfilter-dev libswscale-dev
It still does not work. Maybe the compiler needs to know where to find ffmpeg. I am using gcc compiler.
Thanks.
#Paul R: I have just copied the code from the Learning Opencv textbook, so wasn't including it. The code:
#include<stdio.h>
#include<highgui.h>
int main( int argc, char** argv ) {
cvNamedWindow( "Example2", CV_WINDOW_AUTOSIZE );
CvCapture* capture = cvCreateFileCapture( argv[1] );
IplImage* frame;
while(1) {
printf("Inside");
frame = cvQueryFrame( capture );
if( !frame ) break;
cvShowImage( "Example2", frame );
char c = cvWaitKey(33);
if( c == 27 ) break;
}
cvReleaseCapture( &capture );
cvDestroyWindow( "Example2" );
}
#praks411:
printf("%s",argv[1]);
CvCapture* capture = cvCreateFileCapture( argv[1] );
if(!capture)
{
printf("failed\n");
return -1;
}
It prints the argument correctly and the capture fails.

I think it could be the also the problem of your installation as you have installed ffmpeg after installing opencv, why don't you follow this link for proper installation on ubuntu.
https://help.ubuntu.com/community/OpenCV
http://opencvstart.blogspot.it/2012/12/install-opencv-in-ubuntu-1204.html
I would also recommend to put check on cvCapture if you are able to create correct capture pointer and also try to print argv[1] to see if you are getting proper file name or not.

Related

error using MIL tracker and tracking header

Hi guys below is my code and i am getting error in create(const String& TrackerMIL), i am getting error as in this link exactly https://pastebin.com/0x52tJL6,, please help, for you all to know i have added extra module opencv_contrib in opencv3. please help guys. Thanks
#include <opencv2/opencv.hpp>
#include <opencv2/tracking/tracking.hpp>
using namespace cv;
using namespace std;
int main(int argc, char **argv)
{
// Set up tracker.
// Instead of MIL, you can also use
// BOOSTING, KCF, TLD, MEDIANFLOW or GOTURN
Ptr<Tracker> Tracker::create( const String& TrackerMIL );
// Read video
VideoCapture video("videos/chaplin.mp4");
// Check video is open
if(!video.isOpened())
{
cout << "Could not read video file" << endl;
return 1;
}
// Read first frame.
Mat frame;
video.read(frame);
// Define an initial bounding box
Rect2d bbox(287, 23, 86, 320);
// Uncomment the line below if you
// want to choose the bounding box
// bbox = selectROI(frame, false);
// Initialize tracker with first frame and bounding box
tracker->init(frame, bbox);
while(video.read(frame))
{
// Update tracking results
tracker->update(frame, bbox);
// Draw bounding box
rectangle(frame, bbox, Scalar( 255, 0, 0 ), 2, 1 );
// Display result
imshow("Tracking", frame);
int k = waitKey(1);
if(k == 27) break;
}
return 0;
}
this is solved after including headers properly specially for the above error where it was not able to find my libopencv_tracking.so.3.2,, i had to sudo apt-get update and sudo apt-get upgrade .. and now its working all fine ,,
thanks

Error In OpenCv

I installed opencv and configure it from tutorials, but when I am trying a code implementing it, all what is related to opencv is shown as an error as shown in the figure. Can you please help me? I really need it
#include "stdafx.h"
#include <opencv2/highgui/highgui.hpp>
#include <iostream>
using namespace std;
using namespace cv;
int Main( void)
{
CvCapture* capture = 0;
// Start Capture From WebCam
capture = cvCaptureFromCAM( CV_CAP_ANY );
if( !capture )
{
cout << "No camera Detected" << endl;
}
// Create New Window
cvNamedWindow( "My OpenCV WebCam", CV_WINDOW_AUTOSIZE );
if( capture )
{
cout << "WebCam Is In capture" << endl;
for(;;)
{
// Get Captured Image And Show It In The New Window
// You Can Do Save It Or Filter It
IplImage* iplImg = cvQueryFrame( capture );
// Use This To Filter Image
//cvNot(iplImg, iplImg);
cvShowImage( "My OpenCV WebCam", iplImg );
if( waitKey( 10 ) >= 0 )
break;
}
}
cvReleaseCapture( &capture );
cvDestroyWindow( "My OpenCV WebCam" );
return 0;
}
From the comments to the question it turned out that the OP was linking to OpenCV 2.4.8, instead of OpenCV 3.0.0.
Correcting the linked libraries name, as well as the library path, solved the issue.

Does cvQueryFrame advance the current frame?

I'm trying to learn OpenCV using an O'Reilley book and am finding the sample programs raise as many questions as they answer. In a very basic program to show a video:
#include <highgui.h>
#include <string>
int main( int argc, char** argv){
std::string name = "Example 2";
cvNamedWindow(name.c_str(),CV_WINDOW_AUTOSIZE);
CvCapture* capture = cvCreateFileCapture( argv[1]);
IplImage* frame;
while(1) {
frame = cvQueryFrame( capture );
if ( !frame ) break;
cvShowImage (name.c_str(), frame);
char c = cvWaitKey(33);
if (c == 27) break; //User hits ESC key - ASCII value 27
}
cvReleaseCapture( &capture );
cvDestroyWindow(name.c_str());
}
I find myself wondering why I ever see more than a single frame. Everything I read online about cvQueryFrame says it retrieves the current frame. No where have I seen anything about how/when/where the "current" frame advances.
Does cvQueryFrame act more like reading from a file or stream, in that it reads data and then prepares to read the next piece of data, or does the current frame advance in some other way?

Opencv cannot access my webcam

I have trouble with accessing webcam using opencv 2.4.3.
My System:
Hp Probook 4530s - HP Fixed HD Webcam
Ubuntu 12.10
OpenCV 2.4.3
İf I want to capture my built-in camera i get ERROR: capture is NULL
I'm using http://opencv.willowgarage.com/wiki/CameraCapture sample code.
Sample code is:
#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;
}
I also tried with xawtv -hwscan using typing terminal. I get this output:
looking for available devices
port 129-144
type : Xvideo, image scaler
name : Intel(R) Textured Video`
/dev/video0: OK
[ -device /dev/video0 ]
type : libv4l
name : HP HD Webcam [Fixed]
flags: capture
then I can access my webcam typing xawtv video0. I think I have no trouble with my webcam.
I have trouble with opencv.
I solved my problem few minutes ago. And I decided share my solution for people who handling similar error.
First I didnt install some of below packets ( I dont remember which of them, so I paste all of them)
libjpeg62-dev
libtiff4-dev
zlib1g-dev
libjasper-dev
libavcodec-dev
libdc1394-22-dev
libgstreamer0.10-dev
libgstreamer-plugins-base0.10-dev
libavformat-dev
libv4l-dev
libswscale-dev
Then You should configure your cmake process with this code
cmake -D CMAKE_BULD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local -D BUILD_PYTHON_SUPPORT=ON USE_V4L=ON WITH_TBB=ON -D BUILD_NEW_PYTHON_SUPPORT=ON USE_GStreamer=ON ..
Please notice USE_V4L=ON this code..
I hope you solve after reading my solution.
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
using namespace cv;
using namespace std;
int main()
{
VideoCapture webcam;
webcam.open(0);
if(!webcam.isOpened())//**EDITED**
{
std::cout<<"CANNOT OPEN CAM"<<std::endl;
return -1;
}
Mat frame;
while(true)
{
webcam >> frame;
imshow("TEST",frame);
waitKey(20);
}
return 0;
}
Try the above code...
In some cases it is down to the inbuilt cameras response time (as it was in my case). I discovered that the in-built webcam on my HP G62 only "wakes up" after the first opencv cap.read(frame) call. Therefore to get a positive read from the camera (and hence no error later in the code) I made the call several times before proceeding:
if (!cap.read(frame))
{
if(!cap.read(frame))
{
if(!cap.read(frame))
{
if(!cap.read(frame))
{
printf("Cam read error");
}
}
}
}
For me the optimum was 4 read calls, which ensured my camera was awake and on before running through the main block of code. It is possible that a simple "waitKey" call would work and only two read calls, although I haven't tried this.

Opencv: image, retrieved from camera capture is always gray

I am always getting a grey screen when showing image using opencv, the image is captured from camera.
capture = cvCaptureFromCAM(-1);
cvGrabFrame(capture);
image = cvRetrieveFrame(capture);
cvShowImage("name", image);
after this I see the grey screen, even if I do in cycle. But the same code works well on another computer. What is the problem? The same opencv library version is used on both computers. Working in the Visual Studio 2010, C++
Opencv version 2.2.0
EDIT 1: The camera used in both computers is the same.
And I have tried to rebuild the opencv on the computer where the problem happens, it didn't help.
I had the same problem in OpenCvSharp. I don't know what's the cause, but for some reason, calling the "WaitKey" method after displaying the image solved it.
CvCapture mov = new CvCapture(filepath);
IplImage frame = mov.QueryFrame();
CvWindow win1 = new CvWindow(window name);
win1.ShowImage(frame);
CvWindow.WaitKey(1);
Your problem sounds very weird.
Try to use cvQueryFrame instead of cvRetrieveFrame() and let me know if it does make a difference.
Here is a code of using opencv for face detection, the concept of capturing an image is very similar:
int main( int argc, const char** argv )
{
CvCapture* capture;
Mat frame;
//-- 1. Load the cascades
if( !face_cascade.load( face_cascade_name ) ){ printf("--(!)Error loading\n"); return -1; };
if( !eyes_cascade.load( eyes_cascade_name ) ){ printf("--(!)Error loading\n"); return -1; };
//-- 2. Read the video stream
capture = cvCaptureFromCAM( -1 );
if( capture )
{
while( true )
{
frame = cvQueryFrame( capture );
//-- 3. Apply the classifier to the frame
if( !frame.empty() )
{ detectAndDisplay( frame ); }
else
{ printf(" --(!) No captured frame -- Break!"); break; }
int c = waitKey(10);
if( (char)c == 'c' ) { break; }
}
}return 0;
}

Resources