I'm a beginner to opencv, and I've been trying out some basic things using the library. I've added all the dependencies in VS, so generally the image processing related codes work. However, I am getting the following error on the console for one video-related program:
warning: Error opening file
(/build/opencv/modules/videoio/src/cap_ffmpeg_impl.hpp:578) Press any
key to continue.
My code is as follows:
#include "opencv2\highgui\highgui.hpp"
#include "opencv2\imgproc\imgproc.hpp"
using namespace cv;
int main(int argc, char** argv) {
namedWindow("Video", WINDOW_AUTOSIZE);
VideoCapture cap;
cap.open(argv[1]);
Mat frame;
for (;;) {
cap >> frame;
if (frame.empty()) break;
imshow("Video", frame);
if (waitKey(33) >= 0) break;
}
return 0;
}
I downloaded ffmpeg, and added it my Path environment variable as well, it did not work.
I've searched quite extensively online for this, but to no avail. Any help is appreciated.
Related
I trying use OpenCV with visual studio 2017 and running this example code is giving me build time errors
I have provided the right directories to the libraries. I have gone through many tutorials and questions/answers on stack overflow so many times but haven't succeeded in fixing the problem.
#include "opencv2\opencv.hpp"
#include "opencv2\core.hpp"
#include "pch.h"
#include "opencv2\core\core.hpp"
using namespace cv;
int main(int argv, char** argc)
{
Mat test = imread("lena.jpg", IMREAD_UNCHANGED);
imshow("tst", test);
waitKey();
}
I get the following errors
C2065 'IMREAD_UNCHANGED':undeclared identifier
C3861 'imread': identifier not found
C3861 'imshow': identifier not found
C3861 'waitkey': identifier not found
Intellisense gives me all the library suggestions when I type the code but it throws errors after build.
I had installed CUDA 9.1 previously and tested my OpenCV with the code below, all worked fine but later on I had to remove it and install 8.0. Now the below code gives errors since the previous dlls are searched.
Here is my test code:
#include <iostream>
#include "opencv2/opencv.hpp"
#include "opencv2/core.hpp"
#include "opencv2/highgui.hpp"
#include "opencv2/cudaarithm.hpp"
using namespace cv;
int main(int argc, char* argv[])
{
try
{
cv::Mat src_host = cv::imread("hdd.png", cv::IMREAD_GRAYSCALE);
cv::cuda::GpuMat dst, src;
src.upload(src_host);
cv::cuda::threshold(src, dst, 128.0, 255.0, cv::THRESH_BINARY);
cv::Mat result_host(dst);
cv::namedWindow("Result", cv::WINDOW_NORMAL);
cv::imshow("Result", result_host);
cv::waitKey();
}
catch (const cv::Exception& ex)
{
std::cout << "Error: " << ex.what() << std::endl;
}
return 0;
}
Here is the error:
The code execution cannot proceed because cudart64_91.dll was not found. Reinstalling the program may fix this problem.
It asks for 2-3 more dlls when I click on OK button, but I am not gonna write them here as I suspect the problem arises from the same source.
The problem is rather obvious. The dlls which are tried to be loaded, belong to the uninstalled version of CUDA (9.1), whereas I have now 8.0. I do not know why my Visual Studio 2013 tries to load the previous ones still.
Before anyone asks, yes I do have my newer installation (8.0) in my PATH. I am using Windows 10 x64, if it matters.
The real problem behind your error:
OpenCV was built with CUDA 9.1, this will not change even if you change the CUDA installation, thus it will the DLLs from CUDA 9.1 will always be required for every program that is compiled with OpenCV. Maybe a module does not have this linked and you can use it... but I am almost sure the main ones do and you won't be able to use them.
Possible solutions:
Build OpenCV with CUDA 8.0 then it will require is CUDA 8.0 DLLs and not the 9.1 ones.
Install CUDA 9.1. Both CUDA can be installed in the same computer, that is why they have this _80 or _91, this way you can have both paths and the computer decide which one is needed... I think it is not possible to have BOTH in the same program though, so be careful with this option. If it is only used by OpenCV then it will be ok.
I would recommend the first option, it is safer to stick to one library version...
Hardware:
Raspberry Pi 2 B
OS: Raspbian (Debian Wheezy, released 05.05.2015)
OpenCV 3.0.0 (released 04.06.2015)
I am working with a camera on the CSI Port of the Raspberry Pi. The command
raspistill -o test.jpg
works fine: it takes a picture and saves it on my Raspberry. So the camera works well.
In June, I displayed the camera and could apply some other functions like detecting edges or lines. A few days ago, I wanted to go further but suddenly I had problems with imshow.
For example the following code worked in June but now returns the error
OpenCV Error: Assertion failed (size.width>0 && size.height>0) in imshow, file /home/pi/opencv-3.0.0/modules/highgui/src/window.cpp, line 271
terminate called after throwing an instance of 'cv::Exception'
what(): /home/pi/opencv-3.0.0/modules/highgui/src/window.cpp:271: error: (-215) size.width>0 && size.height>0 in function imshow
Here is the code that worked in June:
#include <cv.hpp>
#include <cxcore.hpp>
#include <stdlib.h>
#include <highgui.h>
#include <iostream>
using namespace cv;
using namespace std;
int main()
{
int c;
Mat image;
VideoCapture video;
video.open(0);
while(1)
{
video >> image;
imshow("test", image);
c=waitKey(10);
if (c==27)
break;
}
video.release();
return 0;
}
I tried to solve my problem by adding code to mine (like delay, resizing, check if the video opened, check if the image to display is not empty) or by deleting some (like vid.open). However, it still does not work. What I get is always "opening video... video did not open". That means that the test vid.isOpened returns always false. I tried to open another video (even if my camera is not the problem as it works with raspistill, as explained below) but the error is the same, isOpened is false.
Here is my modified code:
#include <stdlib.h>
#include <stdio.h>
#include <iostream>
#include "opencv2/opencv.hpp"
using namespace cv;
using namespace std;
int main()
{
Mat image;
VideoCapture vid(0); // open the default camera
//VideoCapture vid("/home/pi/video.mp4"); // open a video file
cout << "opening video... ";
waitKey(1000); // add delay if the camera did not have time to open correctly
//vid.set(CV_CAP_PROP_FRAME_WIDTH, 640); // resize
//vid.set(CV_CAP_PROP_FRAME_HEIGHT, 480);
//vid.open(0); // do not open twice, already done in VideoCapture vid(0)
if(!vid.isOpened()) // check if video is successfully opened
{
cout << "video did not open ";
return -1;
}
cout << "video opened correctly ";
namedWindow( "test", CV_WINDOW_AUTOSIZE ); // prepare the window
waitKey(1000); // add delay if the camera did not have time to open correctly
cout << "before while loop ";
while(1)
{
cout << "inside while loop ";
vid >> image; // get a frame from camera
if(!image.empty()) // wait for the image to be taken
{
cout << "displaying image ";
imshow("test", image); // display it
} else {
cout << "image empty ";
}
if (waitKey(10)==27) // waiting for esc key to be pressed for 10ms
break;
}
vid.release();
return 0;
}
Can someone help me fix this? I really do not see why I have this problem now, as it worked perfectly in June, and I did not change anything on my Raspberry.
Thanks in advance for your help!
EDIT: I think the last thing I can do is reinstalling everything (OS Raspbian and OpenCV), as it worked perfectly in June without installing any of the additional libraries you proposed in the answers. I really don't know what changed between June and now, as nobody touched anything on the Raspberry, my code just doesn't work anymore :(
EDIT2: Code used with PiCapture library:
#include <cv.hpp>
#include <stdlib.h>
#include <stdio.h>
#include <iostream>
#include "opencv2/opencv.hpp"
#include "/home/pi/PiCapture/src/PiCapture.cpp"
using namespace cv;
using namespace std;
int main()
{
PiCapture cap;
namedWindow("PiCapture");
cap.open(320, 240, true); // 320 width, 240 height, color true
while(1)
{
Mat img;
img = cap.grab(); // get a frame from camera
if(!image.empty()) // wait for the image to be taken
{
imshow("PiCapture", img); // display it
}
if (waitKey(10)==27) // waiting for esc key to be pressed for 10ms
break;
}
return 0;
}
sudo modprobe bcm2835-v4l2
load the pre-installed video for linux drivers for the cv video capture method
I had a same issue a while back and put together a nice little wrapper for the Raspberry Pi Camera Module so you can easily retrieve images as cv::Mat.
There's a plain c++ version: PiCapture and an OpenFrameworks addon version: ofxCvPiCam
OpenCV does not see raspiCam as a camera. I prefer to use system() command to get frame from raspiCam and save that somewhere in the filesystem. After that you can load it in your program as a image. Or you can try this BlogPost.
I hope it helps.
My small program is for read camera feed and it is working fine. But when camera connection lost during running application at that time application is terminated.Actually application control can't come out from cvCaptureFromFile() function and after some time it gives me error. When camera connection lost during running application at that time I want to control of cvCaptureFromFile() function means I want to put my application in waiting mode for next frame from camera and when got camera connection back then my application should start read frame automatically from camera. I want to do like this. I tried a lot but can't get any solution.
I am using opencv 2.4.4 version.
My cpp file is
#include "stdafx.h"
#include "cv.h"
#include "cxcore.h"
#include "highgui.h"
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <iostream>
#include <opencv2/imgproc/imgproc.hpp>
#include "stdio.h"
using namespace cv;
using namespace std;
char key;
IplImage* frame = cvCreateImage(cvSize(640,360),8,3);
int main()
{
IplImage *frame1;
back:
CvCapture* input = cvCaptureFromFile("rtsp://admin:12345#10.101.16.112:554/ch1-s1?tcp");
if(!input)
{
printf("\nWaiting for camera connection.");
goto back;
}
while(1)
{
frame1 = cvQueryFrame(input);
/*if(frame1 == NULL)
printf("\nCamera is disconnected.");*/
cvResize(frame1,frame,1);
cvShowImage("frame", frame);
key = cvWaitKey(10);
if (char(key) == 27)
break;
}
cvReleaseCapture(&input);
return 0;
}
Thanks in advance.
I don't know whether it will help you out but try this once...
1) If you are connecting camera to one of the ports, then you can continuously poll that, is it active ? and if is not active then camera got disconnected and accordingly wait for it to become active.
I think the issue might not be with the cvCaptureFromFile() function but with the frame1 = cvQueryFrame(input) command. Can you uncomment out the if statements below this command and clarify if camera disconnected is been printed out.
Sorry but can't do this myself as i currently don't have access to the opencv library
I have got a problem with running my opencv app on windows 7. I used visual studio C++ 2012 on Windows 8.1 to compile it.
After compiling, everything is ok on Windows 8.1 but when i try to launch it on a laptop with Windows 7 it initializes the camera(blue led next to the camera lights up) then crashes. I havent got any idea what am i doing wrong.
Here is the code:
#include <opencv2\highgui\highgui.hpp>
#include <opencv2\imgproc\imgproc.hpp>
int main( void )
{
cvNamedWindow("TEST" ,cv::WINDOW_NORMAL);
cv::VideoCapture capture;
cv::Mat frame;
capture.open( -1 );
if ( ! capture.isOpened() )
{
printf("--(!)Error initializing the camera\n");
system("pause");
}
while(capture.read(frame))
{
cv::imshow( "TEST", frame );
}
return 0;
}
Thank You for help.
Ok guys. I figured out it may be the problem with the camera being not compatible with opencv249. Thats why it crashes while trying to initialize it. I replaced capture from camera with capture from file and everything went great.
I just wasnt smart enough to figure it out before spamming on forum, sorry.