Cannot read image - c++

I'm trying to read an image using OpenCV, where I did the following:
input_image = imread(argv[1], IMREAD_UNCHANGED);
if(input_image.empty())
{
cout<<"Image cannot be read";
return -1;
}
I'm using Xcode 6.4, and would like to pass two arguments, one for the input image and another for the output image.
So, I did the following in order to pass the argument:
Product --> Scheme --> Edit Scheme...
And, under Arguments, added two arguments: image1.png and image2.png.
I had only image1.png, which I added to the directory containing main.cpp. But, when I try to run the program, the input image seems not to be read, since the above if-statement evaluates to true, and I get the error message.
Why is that? What could I be doing wrong?
Thanks.

Related

view cv::Mat data while debugging in Visual Studio Code

I need to check elements of some cv::Mats while debugging my OpenCV C++ code.
Tried adding mymat.at<double>(0, 0) to watch but there are two problems:
Sometimes it doesn't work: Couldn't find method cv::Mat::at<double>
Even if it works, it is very hard to check elements one by one.
Currently, I write Mat contents to a file using cv::FileStorage but it is not a good solution also.
Is there any VSCode extension like Image Watch to show value of Mat elements while debugging?

First project in Opencv to show image, error..

When I run opencv to show an image, I got an error like the image.
String index out of range: -8
The code is from Opencv website:
http://docs.opencv.org/doc/tutorials/introduction/display_image/display_image.html
Like it is mentioned in the tutorial, you need to call your program by command line with an argument which is the path to your image :
./DisplayImage HappyFish.jpg //From the tutorial
The arguments you pass to an application are stored in argv and counted by argc. The first parameter is always the path of your own application.
If this don't helps you, you'll need to show us some codes and logs (what is the exact error message, etc.)

How to solve for different input for dir command in MatLab?

I am reading the images from a folder by using the below code in MatLab
[folder1] = uigetdir();
f=dir(folder1);
for k=1:size(f,1)-2
new_file=f(k+2).name;
end
[idx,idx]=sort(cellfun(#(x) str2num(char(regexp(x,'\d*','match'))),new_file));
filename1=new_file(idx);
This code works for images in the folder with name "test_base1", "test_base2", ....
I am facing an error with a different input for this expression.
For file = {'test_30min1','test_30min10','test_30min2'};
it gives error as follows
??? Error using ==> cellfun
Non-scalar in Uniform output, at index 1, output 1.
Set 'UniformOutput' to false.
I tried giving regexp(x,'\w*','match') and many other combinations in this expression. I am not able to get the solution. May i know what is the solution for this??

How do I guard against failure of cvLoad?

I am writing a program that uses OpenCV and involves intrinsic and distortion parameters. These parameters are loaded from .xml files saved on disc. I use the following commands in my opening declarations to load the files:
CvMat *intrinsic = (CvMat*)cvLoad("Intrinsics.xml");
CvMat *distortion = (CvMat*)cvLoad("Distortion.xml");
This works fine as long as the files are in the program's working directory. When they are not, the program crashes without any indication of the nature of the error. I have made the mistake of not having the xml files located correctly multiple times before, and I would like to make this easier to troubleshoot in the future.
I would like to create a guard against the files failing to load. Perhaps if they are not present my program could display an error message and exit gracefully. I saw the method suggested here, and it should work for me, but I was wondering if there was a cleaner way to do it without including another header.
For example, the OpenCV function cvQueryFrame returns 0 if it does not return a frame. I use the code
frame = cvQueryFrame(capture);
if(!frame)
{
printf("ERROR: Could not get frame from webcam.");
exit(-1);
}
to exit the program if cvQueryFrame fails to return a frame. I would like to do something similar with my matrix loading commands. Is this possible, and if so, how should I do it?
I checked the OpenCV documentation and could not find a description of cvLoad's behaviour when it cannot find the file specified so I am not sure how to proceed.
I am writing this project in C++ and running it on Windows 7.
It works. Go ahead and try it yourself:
CvMat *distortion = (CvMat*)cvLoad("Distortion.xml");
if (!distortion)
{
printf("!!! cvLoad failed");
exit(-1);
}

opencv error : bad argument (bad image header) in cvCloneImage file build/buildd/opencv-2.1.0/src/cxcore/cxarray.cpp, line 3122

sorry for my english..
i'm new to opencv, and i want to try samples square.cpp,i used codeblocks on ubuntu.
but i got error when i compiled and run it, it said that (on Xterm):
opencv error : bad argument (bad image header) in cvCloneImage file build/buildd/opencv-2.1.0/src/cxcore/cxarray.cpp, line 3122
so, my question is, how to fix it?
thanks.
It may contain some code which is using some input images. The important thing is that those input images might not be null.
This error comes in cxarray.cpp where we have the code like if (!CV_IS_IMAGE_HDR(src)). Now if you check for the definition of this
#define CV_IS_IMAGE_HDR
You will get it as:
((img) != NULL && ((const IplImage*)(img))->nSize == sizeof(IplImage))
The first thing it checks is for null image. So in your case you might not have the input image with you.
Select a input image you have and then try again. It will work, as it have worked for me.