OpenCV FileStorage Reading Error - c++

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.

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)

Am getting an unexpected in opencv 3.4.3

The following the the code to load the image tree
#include<opencv2/opencv.hpp>
using namespace cv;
int main()
{
Mat testobject = imread("tree",IMREAD_UNCHANGED);
if (testobject.empty())
std::cout << "failed to open img.jpg"
<<std::endl;
else
std::cout << "img.jpg loaded OK" << std::endl;
imshow("color", testobject);
waitKey();
return(0);
}
but am getting the following output
failed to open img.jpg
and error as
OpenCV(3.4.3) Error: Assertion failed (size.width>0 && size.height>0) in cv::imshow, file C:\build\3_4_winpack-build-win64-vc14\opencv\modules\highgui\src\window.cpp, line 356
Am using visual studio 2017
can anybody please help me out this my first open cv program
In code you should have
Mat testobject = imread("tree.jpg",IMREAD_UNCHANGED);
Image should be in file where You have cpp files, not where sln file is.

Opening and reading a file in Qt

I am trying to use the menu bar to open a file (hardcoded) when 'File|Open' is clicked. The file should append to and display each line. My function is not finding the file. So after I click open I am getting back 'trace.txt cannot be found'. I have the file saved in the same directory as the rest of the project files. I am wondering if I haven't opened the file properly? Can anyone have a look at my code and see if you're catching an error that I am not?
void MainWindow::readFile(){
infoLabel->setText(tr("Invoked <b>File|Open</b>"));
QString filename="trace.txt";
QFile file(filename);
if(!file.exists()){
qDebug() << "File <i>cannot</i> be found "<<filename;
}else{
qDebug() << filename<<" Opening...";
}
QString line;
textEdit->clear();
if (file.open(QIODevice::ReadOnly | QIODevice::Text)){
QTextStream stream(&file);
while (!stream.atEnd()){
line = stream.readLine();
textEdit->setText(textEdit->toPlainText()+"0x"+line+"\n");
qDebug() << "line: "<<line;
}
}
file.close();
}
UPDATE:
I changed the QFile object to the direct path and that found the file. On the other hand, I am reading it in an infinite loop, which never makes it to the textEdit and continually outputs to the debugger. Any ideas?
Use current or currentPath() to see with which directory you are working.
See this example to understand the current directory:
QFile file;
QDir::setCurrent("/tmp");
file.setFileName("readme.txt");
QDir::setCurrent("/home");
file.open(QIODevice::ReadOnly); // opens "/home/readme.txt" under Unix
From http://doc.qt.io/qt-5/qfile.html#QFile

Can not read image in opencv

I am new to opencv and I am starting to make a simple code to read and display image in gui ,I am working in qt IDE, first I wirte this block of code
#include <opencv2/core/core.hpp>
#include<opencv2/highgui/highgui.hpp>
int main()
{
cv::Mat image=cv::imread("image.jpg");
cv::namedWindow("My Image");
cv::imshow("My Image",image);
cv::waitKey(0);
cv::destroyAllWindows();
return 1;
}
But it displays a white window and error in console and then display another window "not responsive" message and then stop working,
This is a screen shot
http://pbrd.co/1u2A0ow
Then I wrote another validity code to check wheater or not the image is been read
int main()
{
Mat image;
cout<<"Size is"<<image.size().height<<","<<image.size().width<<endl;
image=imread("image.jpg");
//Checking first if the image have been read
if(!image.data)
{
cout<<"\n No image has created \n"<<endl;
}
return 1;
}
It displays the message, which means that the image is not read,So The question is
How can I successfully read and load image
note: The image in the same folder of main.cpp file
http://pbrd.co/1u2Bmj1
As you said following code showed you that file not exist:
QFile file("image.jpg");
if(file.exists())
cout<<"\n exist \n"<<endl;
else
cout<<"\n not exist \n"<<endl;
Solution:
First of all, try to set full path to your image. For some reasons Qt search your file in wrong place, so set full path.
For example:
cv::Mat image=cv::imread("G:\\2\\qt.jpg");
QFile file("G:\\2\\qt.jpg");
if(file.exists())
cout<<"\n exist \n"<<endl;
else
cout<<"\n not exist \n"<<endl;
Or UNIX style:
cv::Mat image=cv::imread("G:/2/qt.jpg");
QFile file("G:/2/qt.jpg");
if(file.exists())
qDebug()<<"\n exist \n"<<endl;
else
qDebug()<<"\n not exist \n"<<endl;
it seems that the program does not know where the image is. Try to include main.cpp as one of the libraries that the program will use. That way, the program will be able to find and open the image.

OpenCV Camera Calibration

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