I am using CodeBlocks in my windows 7 64 bit and I use MinGw for my default c/c++ compiler.
Few days ago I need to use OpenCV, after I struggle a lot of error, I get unsolveable error like this :
The sample code:
#include "cv.h"
#include "highgui.h"
int main( int argc, char** argv ) {
IplImage* img = cvLoadImage( argv[1] );
cvNamedWindow( "Example1", CV_WINDOW_AUTOSIZE );
cvShowImage( "Example1", img );
cvWaitKey(0);
cvReleaseImage( &img );
cvDestroyWindow( "Example1" );
}
I believe my linked & directory setting is correct. So please help me :) I am about to give up :(
Assuming that you are doing everything correct in the code and the image, this can be a problem due to incompatible opencv binaries.
Please have a look at a similar installation to compile and see if it works. I had a similar problem in my installation, which was fixed by compiling the binaries again.
The problem is most probably a failure when loading the image. But you will only be certain if you check the return of cvLoadImage():
IplImage* img = cvLoadImage( argv[1] );
if (!img)
{
printf("!!! cvLoadImage failed\n");
}
The function fails if the image format is not supported, or if the image is not found in the specified location.
You application expects to load the file passed from the command line, so you better execute your application with: Main.exe C:\some_img.png
You can also hardcode the filename in your code:
IplImage* img = cvLoadImage("C:\\some_img.png");
if (!img)
{
printf("!!! cvLoadImage failed\n");
}
Related
I have recently been trying to properly configure eclipse to be able to use OpenCV. After installing it to my computer, (Ubuntu, using cmake) I attempted to build some sample code from the OpenCV tutorials.
This is the sample code.
#include <cv.h>
#include <opencv2/opencv.hpp>
#include <opencv2/core.hpp>
#include <highgui.h>
using namespace cv;
int main( int argc, char** argv )
{
Mat image;
image = imread( argv[1], 1 );
if( argc != 2 || !image.data )
{
printf( "No image data \n" );
return -1;
}
namedWindow( "Display Image", CV_WINDOW_AUTOSIZE );
imshow( "Display Image", image );
waitKey(0);
return 0;
}
Some additions to includes to make it work.
Everything is found properly, but when build, gives these 3 errors. (the file I am compiling is named test.cpp)
./test.o: relocation R_X86_64_32 against `.rodata' can not be used when making a shared object; recompile with -fPIC
make: *** [libOpenCV] Error 1
recipe for target 'libOpenCV' failed
I dont know why these errors are happening, or how to fix it. Anyone have any clue?
I think you need to check the link obj, opencv requires number of libraries to work properly. Pls check the detail manual on opencv website.
#include <opencv/cv.h>
#include <opencv/highgui.h>
using namespace cv;
int main( int argc, char** argv )
{
const char* imageName = "samp.png";
Mat image;
image = imread( imageName, 1 );
Mat gray_image;
cvtColor( image, gray_image, CV_BGR2GRAY );
imwrite( "/home/Downloads/Pictures/Gray_Image.jpg",gray_image );
namedWindow( imageName, CV_WINDOW_AUTOSIZE );
namedWindow( "Gray image", CV_WINDOW_AUTOSIZE );
imshow( imageName, image );
imshow( "Gray image", gray_image );
waitKey(0);
return 0;
}
'imwrite" in the above code does not create image.Everything else works fine,and I can see the image using imshow.But I dont know why it is not creating a image.I tried 'convertTo' and replacing '/' to '\' in the path, but it does not work.I will be pleased if I get any lead.Thanks!
This is not documented clearly, but imwrite returns a boolean which is true if and only if it thinks that it could write the file successfully. You should check that value!
You'll probably find out that imwrite returns false. Most likely you do not have sufficient permissions or -- as berak pointed out -- your file path is invalid.
By the way, for proper error handling, you should also catch exceptions, in particular, if the user provides the output file URL. If OpenCV, for some reason, can't find a suitable encoder (i.e. it can't recognize which type of file you are going to write), it will throw an exception.
Markus Mayr's response helped me find out that I was getting a false when I printed the result of imwrite. The reason turned out to be, the directory in which I was trying to write, didnt exist. I was assuming it will create the directory but imwrite silently failed.After I manually created the directory it worked
When i specify the destination folder path of a image in my open cv project it works fine but when i keep that image in the project folder the image is not recognized.
i tried to run the following sample code
include "opencv\cv.h" // include it to used Main OpenCV functions.
#include "opencv\highgui.h" //include it to use GUI functions.
int main(int argc, char** argv)
{
IplImage* img = cvLoadImage( "Desert.jpg" ); //change the name (image.jpg) according to your Image filename.
cvNamedWindow( "Example1", CV_WINDOW_AUTOSIZE );
cvShowImage("Example1", img);
cvWaitKey(0);
cvReleaseImage( &img );
cvDestroyWindow( "Example1" );
return 0;
}
when i specify C:\Users\mia\Desktop\Desert.jpg problem is solved.
IplImage* img = cvLoadImage( "Desert.jpg" );
It's because you're using relative path here. If so, you should put the image in your TargetDir (the directory where your program are supposed to generated into).
P.S.: As commented by #berak, you should use the new OpenCV's API (e.g. use Mat instead of IplImage *).
I have tried to build the sample program from OpenCV documentation, but i have encountered a problem:
error: ‘CV_WINDOW_AUTOSIZE’ was not declared in this scope
Source of program:
#include <stdio.h>
#include <opencv2/opencv.hpp>
using namespace cv;
int main( int argc, char** argv )
{
Mat image;
image = imread( argv[1], 1 );
if( argc != 2 || !image.data )
{
printf( "No image data \n" );
return -1;
}
namedWindow( "Display Image", CV_WINDOW_AUTOSIZE );
imshow( "Display Image", image );
waitKey(0);
return 0;
}
I think that CV_WINDOW_AUTOSIZE constants have been contained in a certain header file, but I can't find the necessary header file.
CV_WINDOW_AUTOSIZE actually really is found in highgui.h, BUT, as #berak pointed out in the comments, that's part of the obsolete c-api. You should instead do one of two things:
Use WINDOW_AUTOSIZE instead, which is part of the C++ API. You don't need to change anything else to make this work, not even #include anything that isn't already #included in the example.
Use namedWindow( "Display Image" ) instead, since namedWindow uses WINDOW_AUTOSIZE by default and so you don't even have to include it as an argument.
Tested for OpenCV 3.0.0
It appears that in OpenCV 3.1 you need to use cv::WindowFlags::WINDOW_AUTOSIZE which is located in <opencv2/highgui.hpp>.
For opencv 4, it is defined in <opencv2/highgui/highgui_c.h>
since all the windowing stuff is in the highgui module, you'll need
#include <opencv2/highgui/highgui.hpp>
also, you'll need to link against the opencv_highgui library later
I have same issue and use
WINDOW_AUTOSIZE instead of
CV_WINDOW_AUTOSIZE
It has been change in version 4.
You can use directly WIDOW_AUTOSIZE if have defined using namespace cv as in your example.
Also, do not forget to add the correct dependencies for opencv
You will find it in highgui.h.
I have build OpenCV 2.4.1 on Ubuntu 12.04 32 bit platform with OpenGl,Qt and OpenNI but whenever I am running example programs listed in the Learning OpenCV Book.
For Example:
#include "highgui.h"
int main( int argc, char** argv ) {
cvNamedWindow( "Example2", CV_WINDOW_AUTOSIZE );
//CvCapture* capture = cvCaptureFromAVI( argv[1] );
CvCapture* capture = cvCreateFileCapture( argv[1] );
IplImage* frame;
while(1) {
frame = cvQueryFrame( capture );
if( !frame ) break;
cvShowImage( "Example2", frame );
char c = cvWaitKey(33);
if( c == 27 ) break;
}
cvReleaseCapture( &capture );
cvDestroyWindow( "Example2" );
}
I get this message in the console:
init done
opengl support available
I wonder where I am going wrong.I am not getting any errors in compilation.
This is not an error. I have a similar configuration on my machine and I see these statements every time I run something. These statements have nothing to do with what you have programmed. I have run your exact code and it displayed the video without any problems. Perhaps add this error check after you open the capture to make sure it found the video:
if (!capture) {
std::cout << "COULD NOT OPEN CAPTURE\n";
}
I was having the same problem and then I added waitKey(0) at the end and the image was displayed.