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

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.

Related

4 same unhelpful errors for every error I receive in c++

I am using spdlog, opengl, glad, and glfw in my project. No matter what error I am receiving, it is ALWAYS preceded by the 4 same errors. For example, let's say I have the following code:
std::cout << "sup";
then it will run perfectly. Okay now, let's say I have a typo and I do this:
std:cout<< "sup";
I get the following errors:
Here are the first 2 errors.. they redirect to FMT core.h file
and then the last two redirect to FMT format.h
Does anyone know why this could be happening? it is not made or break, whenever I have an error and I fix it these go away but it's just annoying and I would like to understand why.
It looks as though this PR will fix your issue?
https://github.com/fmtlib/fmt/pull/1279
Maybe just update fmt to the latest version?

FREAK is not a member of cv?

I am trying a code from tutorial to test the marker-less AR experience with OpenCV + OpenGL. But the problem is that when I compiled the code I found out that there is an error with the following segment of code:
cv::Ptr<cv::DescriptorExtractor> extractor = cv::FREAK(false, false),
and the error is:
FREAK is not a member of cv.
So, can someone tell me why is that happening? I have tried to search for a solution and tried to solve as much as I can but every time I got error.

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 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);
}

Segmentation fault when different input is given

I do some image processing work in C++. For this i use CImg.h library which i feel is good for my work.
Here is small piece of code written by me which just reads an image and displays it.
#include "../CImg.h"
#include "iostream"
using namespace std;
using namespace cimg_library;
int main(int argc,char**argv)
{
CImg<unsigned char> img(argv[1]);
img.display();
return 0;
}
When i give lena.pgm as input this code it displays the image. Where as if i give some other image, for example ddnl.pgm which i present in the same directory i get "Segmentation Fault".
When i ran the code using gdb i get the output as follows:
Program received signal SIGSEGV, Segmentation fault.
0x009823a3 in strlen () from /lib/libc.so.6
Missing separate debuginfos, use: debuginfo-install glibc-2.9-2.i686 libX11-1.1.4-5.fc10.i386 libXau-1.0.4-1.fc10.i386 libXdmcp-1.0.2-6.fc10.i386 libgcc-4.3.2-7.i386 libstdc++-4.3.2-7.i386 libxcb-1.1.91-5.fc10.i386
Can some one please tell me what the problem is? and how to solve it.
Thank you all
Segfault comes when you are trying to access memrory which you are not allowed to access.
So please check that out in the code.
The code itself looks just fine. I can suggest some ways to go ahead with debugging -
Try removing the display() call. Does the problem still occur? (I'd assume it does).
Try finding out where in the CImg code is the strlen() that causes the segmentation fault (by using a debugger). This may give additional hints.
If it is in the PGM file processing, maybe the provided PGM file is invalid in some way, and the library doesn't do error detection - try opening it in some other viewer, and saving it again (as PGM). If the new one works, comparing the two may reveal something.
Once you have more information, more can be said.
EDIT -
Looking at the extra information you provided, and consulting the code itself, it appears that CImg is failing when trying to check what kind of file you are opening.
The relevant line of code is -
if (!cimg::strcmp(ftype,"pnm")) load_pnm(filename);
This is the first time 'ftype' is used, which brings me to the conclusion that it has an invalid value.
'ftype' is being given a value just a few lines above -
const char *const ftype = cimg::file_type(0,filename);
The file_type() function itself tries to guess what file to open based on its header, probably because opening it based on the extension - failed. There is only one sane way for it to return an invalid value, which would later cause strcmp() to fail - when it fails to identify the file as anything it is familiar with, it returns NULL (0, actually).
So, I reiterate my suggestion that you try to verify that this is indeed a valid file. I can't point you at any tools that are capable of opening/saving PGM files, but I'm guessing a simple Google search would help. Try to open the file and re-save it as PGM.
Another "fun to track down" cause of segmentation faults is compilier mismatches between libraries - this is especially prevalent when using C++ libraries.
Things to check are:
Are you compiling with the same compiler as was used to compile the CImg library?
Are you using the same compiler flags?
Were there any defines that were set when compiling the library that you're not setting now?
Each of these has bitten me in subtle ways before.