OpenCV exception while reading a yaml file compressed with gz using filestorage - opencv

The output file when I save some variable using FileStorage is a yaml file of 9MB. I saw we can just add ".gz" to file name in order to compress the file. The resulting file is 3.5MB. That is ok. But when I try to open the myfile.yaml.gz file to APPEND using filestorage i am having an unhandled exception. Do you know if OpenCV supports this? If yes, how should I read the file? If not, any suggestion to compress/decompress the file easily?
Here is a sample code:
std::vector<int> myArray;
myArray.push_back(0); myArray.push_back(1);
FileStorage fs("myFile.yaml.gz", FileStorage::WRITE);
fs << "myArrayName" << myArray;
fs.release();
myArray.clear();
fs = FileStorage("myFile.yaml.gz", FileStorage::APPEND);
fs << "intValue" << 3;
fs.release();
fs = FileStorage("myFile.yaml.gz", FileStorage::READ);
fs["myArrayName"] >> myArray;
fs.release();

Related

How do you access runfiles with Bazel 5.2.0 in c++

Im trying to access runfiles within c++. Im using Bazel 5.2.0. I tried to access like this:
std::string error;
std::unique_ptr<Runfiles> runfiles(Runfiles::Create(argv[0], &error));
if (!runfiles) {
std::cerr << error << std::endl;
return 1;
}
std::string path = runfiles->Rlocation("Test/Example.tx");
std::cout << "Example.tx: " << path << std::endl;
std::ifstream in(path);
if (!in.is_open())
{
std::cout << "Example.tx not found" << std::endl;
return -1;
}
(Example.tx is right, just to lazy to change)
The program is finding a path but the path starts from the bazelisk directory and doesn't point to the binary dir.
Example.tx: C:\users\nikla\_bazel_nikla\d47dtf2d\execroot\__main__\bazel-out\x64_windows-fastbuild\bin\Test\Test.exe.runfiles/Test/Example.tx
Example.tx not found
Im getting this as an result.
Maybe there is a new way to access runfiles but Im not finding it.
The answer is you have to include the workspace name in the path, if it isn't set you have to use __main__.
Closed.

opencv read image “Premature end of JPEG file”

I was using OpenCV to read the images from a folder. A lot of messages like this show up:
Corrupt JPEG data: premature end of data segment
Premature end of JPEG file
Premature end of JPEG file
Premature end of JPEG file
How to catch this exception and remove these image files?
Since you said you are reading 'images' (multiple images), you would be looping through files in the folder that you are reading them from.
In that case, if you check if the image is valid or not by using the following :
Mat image;
image = imread(argv[1], CV_LOAD_IMAGE_COLOR); // Read the file
if(! image.data ) // Check for invalid input
{
cout << "Could not open or find the image" << std::endl ;
return -1;
}
you can then proceed to deleting files which are corrupt/bad.
I've been struggling to find a solution too. Read tens of articles, most of which just state that openCV does not throw errors and only outputs the error on stderr.
Some suggest to use PIL, but that does not detect most of the image corruptions. Usually only premature end of file.
However the same errors that OpenCV warns about can be detected via imagemagick.
Install imagemagick (https://imagemagick.org/)
Make sure you have it in the path.
Put the following sub into your code and call it to verify a file from wherever you need to. It also outputs errors to stderr, however it raises an error (thanks to "-regard-warnings")
import subprocess
def checkFile(imageFile):
try:
subprocess.run(["identify", "-regard-warnings", imageFile]).check_returncode()
return true
except (subprocess.CalledProcessError) as e:
return false
If you don't want the check to spam your outputs, add stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL params to the run function call.
On windows if you have not installed the legacy commands use the new syntax:
subprocess.run(["magick", "identify", "-regard-warnings", imageFile]).check_returncode()

loading image in opencv2.4.6

In opencv 2.4.6. I am trying to load a mat image file with a simple code given below. But the image is not loaded as I print the image size, it is showing '0'. Can anybody please tell me , what is going wrong?
int main(int argc, char argv[])
{
Mat a=imread("C:/image3.jpg");
cv::Size frame11_size = a.size();
printf("%d",frame11_size.height);
return 0;
}
Update: I solved the problem. The problem was, I was only including all the library,include and additional dependencies in 'debug mode' only. I did not change anything in 'release mode'. When I change the properties in 'release mode' as-well, it worked. thanks all for your kind responses, I am giving '+1' for your answers.
I think there should be single slash on your image path, and always check whether image is successfully loaded.
Mat a=imread("C:/image3.jpg");
if(! a.data ) // Check for invalid input
{
cout << "Could not open or find the image" << std::endl ;
return -1;
}
OpenCV can't open jpg files by itself. It depends on third parties to do so. Maybe you are missing certain dlls, or maybe your OpenCV installation don't have the right path to them. To test this assumption store your image in other formats. For example pgm or ppm. Those formats does not perform any encoding and just store image buffer in file as is. As a result OpenCV will not need any external libraries to open image in ppm format.

writing a video of depth images using opencv

I am trying to write a video with some sequence of depth map.
I have converted the depth image from cv::Mat_ to cv::Mat with single channel.
But no codec I use is able to open the avi file I want to write. The VideoCapture.open(...) doesn't seem to be able to either create the file or open it.
I think its the problem choosing the right codec. I might be wrong. I have posted a small code snippet.
cv::VideoWriter outputVideo_;
source_ = "~/Hello.avi";
cv::Size S(480, 640);
outputVideo_.open(source_, CV_FOURCC('D','I','B', ' '), 20, S, false);
if (!outputVideo_.isOpened())
{
std::cout << "Could not open the output video for write: " << source_ << std::endl;
return;
}
How do I get opencv to work correctly in this case. I am using linux 12.04, ROS (Robot Operating System) and OpenCV 2.4.2
Try to use open() function with only file name. because here with me it works well.
VideoWriter outputVideo_;
source_ = "~/Hello.avi";
// cv::Size S(480, 640);
outputVideo_.open(source_);
if (!outputVideo_.isOpened())
{
std::cout << "Could not open the output video for write: " << source_ << std::endl;
return;

displaying an image in mat form

I am trying to read an image in mat form in opencv and display it.
the code compiles fine but when run gives runtime error and the image is not displayed.
here is my code:
#include "stdafx.h"
#include<cv.h>
#include<highgui.h>
#include<iostream>
using namespace cv;
using namespace std;
void main()
{
Mat Img;
Img=imread("C:/Documents and Settings/image1.jpg");
cvNamedWindow("Image",CV_WINDOW_AUTOSIZE);
imshow( "Image", Img );
cvWaitKey(0);
}
Could anyone please tell me where am I going wrong?
Most probably imread() is failing. You will know for certain unless you start checking the success of the functions, such an important thing to do when you code.
There's a couple of reasons why it might fail:
You don't have permission to access the file.
The path is wrong.
In your case, it's probably the first. So change your code to:
Mat Img;
Img = imread("C:\\Documents and Settings\\image1.jpg");
if (Img.empty()) {
std::cout << "!!! Failed to open the file\n";
return;
}
and notice how the path to file is different.

Resources