Problems building OpenCV code on linux - c++

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.

Related

OpenCV program wouldn't compile on Visual Studio 2010

I am trying to compile a very simple OpenCV (2.4.9) program in Visual Studio 2010 just to check whether opencv libraries have been linked properly. Here is the program code I am trying to run.
#include "stdafx.h"
#include <stdio.h>
#include <opencv2/opencv.hpp>
using namespace cv;
int main(int argc, char** argv )
{
Mat image;
image = imread( "MyPic.jpg", 1 );
if ( !image.data )
{
printf("No image data \n");
return -1;
}
namedWindow("Display Image", CV_WINDOW_AUTOSIZE );
imshow("Display Image", image);
waitKey(0);
return 0;
}
But when I press the build button, the compilation fails and shows the following error.
1>LINK : fatal error LNK1104: cannot open file 'opencv_gpu249.lib'
What the problem could be? How to solve it?
You need to add the OpenCV library path to the Visual Studio's Library Directories:
OpenCV-2.4.9-Path\build\x86\vc10\lib
or for 64bits
OpenCV-2.4.9-Path\build\x64\vc10\lib

OpenCV - Getting picture from default webcam in C/C++ - GTK Issues

I have a simple GTK+ v3 GUI application, and I am making use of the OpenCV library so that I have a simple function for taking pictures from the one webcam connected to my computer. The code is included at the bottom of this post.
I'm able to successfully acquire image data and render it on screen, but when I include this code in my GTK+ v3 project, I get a startup error like so:
(result:2944): Gtk-ERROR **: GTK+ 2.x symbols detected.
Using GTK+ 2.x and GTK+3 in the same process is not supported.
Trace/breakpoint trap.
So, this makes sense so far. One of the OpenCV libraries apparently makes use of Gtk+ v2. It turns out that if I remove libopencv_highgui from my list of libraries to link against, I won't have this issue. However, the functions used to acquire image data from the webcam is included in that library for some reason.
Are there other functions accessble via the C or C++ APIs for OpenCV that don't require me to make use of libopencv_highgui and allow me to take a snapshot from a webcam with ease?
The other alternative seem to be re-writing my project as a Gtk+ v2 application, which wouldn't be so bad, seeing as I haven't gone too far into it.
Is there a hidden option C out there? (Pardon the pun ;) ).
#include <opencv2/objdetect/objdetect.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <iostream>
#include <stdio.h>
#include <errno.h>
using namespace std;
using namespace cv;
#define PROJECT_NAME "CAMERA_MODULE" // Include before liblog
#include "../../lib/liblog/inc/log.h"
int cameraAcquireImage(void) {
CvCapture* capture = 0;
Mat frame, frameCopy, image;
//0=default, -1=any camera, 1..99=your camera
capture = cvCaptureFromCAM(CV_CAP_ANY);
if(!capture) {
logError("No camera interface detected");
return (-EIO);
}
cvNamedWindow( "result", CV_WINDOW_AUTOSIZE );
if(capture) {
logError("Capture in progress");
for( ; ;) {
IplImage* iplImg = cvQueryFrame(capture);
frame = iplImg;
if(frame.empty()) {
break;
}
if(iplImg->origin == IPL_ORIGIN_TL) {
frame.copyTo(frameCopy);
} else {
flip(frame, frameCopy, 0);
}
cvShowImage( "result", iplImg );
if( waitKey( 10 ) >= 0 ) {
break;
}
}
}
cvReleaseCapture( &capture );
cvDestroyWindow("result");
return 0;
}
I can think of the following solutions:
Downgrade to GTK 2 - pretty simple.
Since VideoCapture is one the very few modules that depend on
highgui, use something else for video capture (Video4Linux perhaps)
and then use OpenCV modules which do not depend on highgui.
Build OpenCV with GTK 3 support (WITH_GTK=ON WITH_GTK3=ON).
Use Qt instead of GTK if you can.

OpenCV: where can I find CV_WINDOW_AUTOSIZE constants?

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.

OpenCV 2.4 error 0xc0000005 in Windows 7 64 bit

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

Cannot run Opencv with C++ syntax

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.