OpenCV Camera Calibration - c++

i'm using Camera calibration With OpenCV tutorial (http://docs.opencv.org/doc/tutorials/calib3d/camera_calibration/camera_calibration.html). When I use .mp4 video file as input, my program fails and gives this error:
Parsing error (): Valid XML should start with '') in icvXMLParse, file ........\opencv\modules\core\src\persistence.cpp, line 2252
Could anyone please tell me what i'm doing wrong?

In the example (camera_calibration.cpp), make this change to the readStringList method:
was:
static bool readStringList( const string& filename, vector<string>& l )
{
l.clear();
FileStorage fs(filename, FileStorage::READ);
if( !fs.isOpened() )
return false;
should be:
static bool readStringList( const string& filename, vector<string>& l )
{
l.clear();
FileStorage fs;
try {
fs.open(filename, FileStorage::READ);
}
catch (...) {
return false;
}
if( !fs.isOpened() )
return false;
Then you can use a video filename instead of an xml input file, and the program won't crash. The FileStorage constructor and open method throw an exception if the input file isn't xml/yml, and the exception needs to be caught.

Well done. I had the same problem and I fixed following your point applying the try..catch...
C:\OpenCVProjects\ConsoleApplication1\x64\Release>ConsoleApplication1.exe -w 9 -
h 6 -s 2 -o camera.yml -op -oe video.mp4
When the live video from camera is used as input, the following hot-keys may be
used:
, 'q' - quit the program
'g' - start capturing images
'u' - switch undistortion on/off
Calibration succeeded. avg reprojection error = 0.49

Related

I build opencv 3.4 with Gstreamer MSVC 1.16.1, and now imread and VideoCapture not working

I build OpenCV 3.4 with Gstreamer MSVC 1.16.1, using Cmake and Visual Studio 10.
I have include bin directory to the system path variable, added all additional include and library to Visual Studio.
Now when I am trying to read an Image to test if OpenCV is correctly installed it throws an error as:
OpenCV Error: Assertion failed (size.width>0 && size.height>0) in imshow, file .../opencv/modules/highgui/src/window.cpp
The code was:
Mat image1;
image1 = imread("‪D:\\Capture2.JPG");
if(! image1.data ) // Check for invalid input
{
cout << "Could not open or find the image" << std::endl ;
}
imshow("Image",image1);
cvWaitKey(0);
return 0;
Now I tried to play a video using demo code from openCV site:
// opencv_3.4_test.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "opencv2/opencv.hpp"
#include <iostream>
using namespace std;
using namespace cv;
int _tmain(int argc, _TCHAR* argv[])
{
// Create a VideoCapture object and open the input file
// If the input is the web camera, pass 0 instead of the video file name
VideoCapture cap("‪Wildlife.mp4");
// Check if camera opened successfully
if(!cap.isOpened()){
cout << "Error opening video stream or file" << endl;
return -1;
}
while(1){
Mat frame;
// Capture frame-by-frame
cap >> frame;
// If the frame is empty, break immediately
if (frame.empty())
break;
// Display the resulting frame
imshow( "Frame", frame );
// Press ESC on keyboard to exit
char c=(char)waitKey(25);
if(c==27)
break;
}
// When everything done, release the video capture object
cap.release();
// Closes all the frames
destroyAllWindows();
return 0;
}
The program is building correctly but I am getting following error while running it:
warning: Error opening file (/build/opencv/modules/videoio/src/cap_ffmpeg_impl.hpp:808)
warning: ?Wildlife.mp4 (/build/opencv/modules/videoio/src/cap_ffmpeg_impl.hpp:809)
GStreamer: error opening bin syntax error
Where can be the error as they both are simplest OpenCV program.
So the error was, there was an invisible character ('\u202A') present just after " of the filename. Once I deleted it, everything runs fine.
I found this from the warning C4566: character represented by universal-character-name '\u202A' cannot be represented in the current code page (1252)

OpenCV VideoCapture can't open file, what am I doing wrong?

I am compiling on Ubuntu 14.04 using OpenCV 3.1. When trying to open a video file it gives this error:
"Cannot open the video file"
I installed everything i could install : ffmpeg etc. Haven't found a solution checking out similar questions on StackOF.
What do ?
cv::VideoCapture cap(argv[1]);
Where argv[1] is the file name in the same directory as the executable.
In case your constructor is failing, you may want to use the .open() method. So, if you want to open a file that is called "myVideo.mp4" that is in the folder of your project, you would do the following:
cv::VideoCapture cap;
cap.open("myVideo.mp4");
For more detailed informations about this method, check this documentation link
Also, the book Learning OpenCV 3, from the O'Rilley media, on page 26 gives you a good example. Here is a Gist that I made to give you as an example.
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <iostream>
int main() {
cv::VideoCapture cap;
cap.open("myVideo.mp4" );
cv::namedWindow( "myVideo", cv::WINDOW_AUTOSIZE );
cv::Mat frame;
while(true) {
cap >> frame;
if( frame.empty() ){
std::cout << "Could not load the video frames. \n";
break;
}
cv::imshow( "myVideo", frame );
if( cv::waitKey(27) >= 0 ){
std::cout << "Escape pressed \n";
break;
}
}
return 0;
}

VideoCapture not working C++ windows

So I have played around in OpenCV a bunch before and never run into this problem. I am implementing a MeanShift algorithm and trying to do it on video devices, images, and videos. Devices and images work; however, no matter what I try, when I run VideoCapture on my filename (whether setting it in the Constructor or using the VideoCapture::open() method, and whether local or with a full path) I always get stuck in my error check.
Thoughts? Ideas? code below. running in Visual Studio 2012
#include "opencv2\highgui\highgui.hpp"
#include "opencv2\core\core.hpp"
#include "opencv2\opencv.hpp"
#include "opencv2\video\video.hpp"
#include <string>
using cv::Mat;
using std::string;
enum Filetype{Image, Video};
int main(int argc, char* argv[])
{
string filename = "short_front.avi";// "C:\\Users\\Jonathan\\Videos\\short_front.mp4"; //"hallways.jpg";
Mat cv_image; //convert to unsigned char * with data
Mat filtImage_;
Mat segmImage_;
Mat whiteImage_;
cv::VideoCapture vid;
vid.open("C:/Users/Jonathan/Desktop/TestMeanShift/TestMeanShift/short_front.avi");
cv::waitKey(1000);
if ( !vid.isOpened() ){
throw "Error when reading vid";
cv::waitKey(0);
return -1;
}
// cv_image = cv::imread(filename);//, CV_LOAD_IMAGE_COLOR);
// if(! cv_image.data){
// std::cerr << "Image Failure: " << std::endl;
// system("pause");
// return -1;
// }
//Mat cv_image_gray;
//cv::cvtColor(cv_image,cv_image_gray,CV_RGB2GRAY);
for (;;)
{
vid >> cv_image;
if ( !cv_image.data)
continue;
cv::imshow("Input",cv_image); //add a normal window here to resizable
}
EDIT: This is a distinct problem from the one listed here because it deals with a specific corner case: VideoCapture and ImageCapture both work, only not VideoCapture with a file. When it doesn't work, the code runs properly, except that the "video" it creates is incomplete as it didn't open properly. Therefore, as the code above does not crash in compile time or run time, the only indicator is bad output (6KB video output file). If you are having issues not with the corner case I am describing but general issues with the above functions in OpenCV, the aforementioned link could help you.

OpenCV FileStorage Reading Error

I'm trying to solve a problem related with the OpenCV FileStorage.
I can successfully write and read to/from yml/xml files.
This only works when writing and reading functions are in the same program though.
When I create and write a yml file successfully in one program and tried to access that file from another program, I'm having the following problem.
Writing part:
d_img = imread(filename, 1);
FileStorage fs_source("tmp.yaml", FileStorage::WRITE);
if (fs_source.isOpened()){
fs_source << "source" << d_img;
fs_source.releaseAndGetString();
cout<<"Source : image written to temporary file"<<endl;
}
else
{
fs_source.open("tmp.yaml", FileStorage::WRITE);
fs_source << "source" << d_img;
fs_source.releaseAndGetString();
cout<<"Source : image written to temporary file 2"<<endl;
}
Reading part:
FileStorage fs_sink("tmp.yaml", FileStorage::READ);
if (fs_sink.isOpened()){
fs_sink["source"] >> d_img;
fs_sink.releaseAndGetString();
cout<<"Sink : image read from file 1"<<endl;
}
else
{
fs_sink.open("tmp.yaml", FileStorage::READ);
fs_sink["source"] >> d_img;
fs_sink.releaseAndGetString();
cout<<"Sink : image read from file 2"<<endl;
}
imwrite( "output.jpg", d_img );
First code creates the yml file without any problem.
But the second code throws the following error when it comes to the first line here which is FileStorage fs_sink("tmp.yaml", FileStorage::READ);
OpenCV Error: Parsing error (tmp.yaml(0): Valid XML should start with '<?xml ...?>') in icvXMLParse, file /var/tmp/portage/media-libs/opencv-2.4.7/work/opencv-2.4.7/modules/core/src/persistence.cpp, line 2257
thread[thread-per-block[1]: <block image_sink (2)>]: /var/tmp/portage/media-libs/opencv-2.4.7/work/opencv-2.4.7/modules/core/src/persistence.cpp:2257: error: (-212) tmp.yaml(0): Valid XML should start with '<?xml ...?>' in function icvXMLParse
This is odd because the file is yml, not an xml. It still gives the same error when I change the file type to xml.
What could cause this error?
PS: Both releaseAndGetString(); and release(); methods are tried to close the file. Same results.
xml, yml, and yaml extensions are all tried too.
Edit: Can be replicated if file is empty.

Retrieving video frames using opencv - unhandled exception

I am getting video frames using cvQueryframes, but in few videos of avi file I get :
Unhandled exception at 0x715c14f0 0xC0000005:
Access violation reading location 0x02f509f0.
I am using visual studio 2010 with OpenCV 2.4.5 and Qt5
CvCapture* cap= cvCaptureFromFile(file);
frame = cvQueryFrame(capture);
There could be multiple reasons for this, link wrong file name, codec not found etc. Try putting debug printf before opening file to see if file name is proper also check for cap if it is not NULL. You can try something like this
int main(int argc, char*argv[])
{
char *my_file = "C:\\vid_an2\\desp_me.avi";
std::cout<<"Video File "<<my_file<<std::endl;
cv::VideoCapture input_video;
if(input_video.open(my_file))
{
std::cout<<"Video file open "<<std::endl;
}
else
{
std::cout<<"Not able to Video file open "<<std::endl;
}
namedWindow("My_Win",1);
namedWindow("Segemented", 1);
Mat cap_img;
for(;;)
{
input_video >> cap_img;
imshow("My_Win", cap_img);
waitKey(0);
}
return 0;
}