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.
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 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");
}
Im using Opencv 2.3.1 on Visual studio 2010 (vc10)
I have configured opencv based on many tutorials and can compile & run C-syntax program like:
#include "StdAfx.h"
#include <iostream>
#include <opencv2/opencv.hpp>
using namespace std;
using namespace cv;
int main ()
{
IplImage* img = cvLoadImage("D:\cat_helmet.jpg", CV_LOAD_IMAGE_UNCHANGED);
cvNamedWindow("display", CV_WINDOW_AUTOSIZE);
cvShowImage("display", img );
cvWaitKey(0);
return 0;
}
However, I cannot run the C++ syntax program like
#include "StdAfx.h"
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <iostream>
using namespace cv;
using namespace std;
int main( )
{
namedWindow( "Display window", CV_WINDOW_AUTOSIZE );
Mat image;
image = imread("D:\cat_helmet", CV_LOAD_IMAGE_COLOR);
if(! image.data )
{
cout << "Could not open or find the image" << std::endl ;
return -1;
}
imshow( "Display window", image );
waitKey(0);
return 0;
}
I got the error messages (in the function calls: namedWindow, imread, imshow)
First-chance exception at 0x5361fcc3 in FirstOpencv2.3.exe: 0xC0000005: Access violation reading location 0x2079616c.
Unhandled exception at 0x5361fcc3 in FirstOpencv2.3.exe: 0xC0000005: Access violation reading location 0x2079616c.
How can I fix this?
You say that you have followed a multitude of guides and tutorials. I've had great success with this one
http://www.anlak.com/using-opencv-2-3-1-with-visual-studio-2010-tutorial/
The thing is that this guy walks you through the 'park' and helps you unravel two major issues whilst setting up OpenCV 2.3.1; one of which is placement of .dll files in your project folder. The other is a missing .dll 'tbb_debug.dll' (the absense of this .dll is considered a bug in OpenCV 2.3.1).
He also provides some decent code-snippets for your to try out (in c++ syntax).
Good luck.
The above mentioned answers doesn't make sense. I am also facing the same problem. The main reason for this exception is that you are trying to display image (read by imread) which is empty. The main problem in the program is the line
image = imread("D:\cat_helmet", CV_LOAD_IMAGE_COLOR);
I think imread function is not behaving the way it is expected. One more thing, while going through the references i came across a following link:
http://docs.opencv.org/modules/highgui/doc/reading_and_writing_images_and_video.html?highlight=imread#Mat imread(const string& filename, int flags)
Here, imread is used via call by reference method. I am not a C++ expert, but i feel it could be the problem.
int main()
{
std::string imgPath("splash.bmp"); //Add your file name
Mat img = imread(imgPath);
namedWindow( "Example1", WINDOW_AUTOSIZE);
imshow("Example1", img);
waitKey(0);
return 0;
}
This code worked for me. Also, I put the file next to executable to decrease complexity.