I finally managed to build the opencv4.5.4 library from source but now I'm facing errors that I'm unable to fix.
I'm using this medium article as my guide https://medium.com/analytics-vidhya/how-to-install-opencv-for-visual-studio-code-using-ubuntu-os-9398b2f32d53
When I try to execute a simple program that prints the version of opencv installed, it executes without errors.
#include <opencv2/opencv.hpp>
#include <iostream>
int main()
{
std::cout << "OpenCV Version: "<< CV_VERSION << std::endl;
return 0;
}
makefile:
CC = g++
PROJECT = new_output
SRC = new.cpp
LIBS = `pkg-config --cflags --libs opencv4`
$(PROJECT) : $(SRC)
$(CC) $(SRC) -o $(PROJECT) $(LIBS)
Output:
username#Inspiron-7591:~/SeePluPlu/opencv-test$ sudo make
g++ new.cpp -o new_output `pkg-config --cflags --libs opencv4`
username#Inspiron-7591:~/SeePluPlu/opencv-test$ sudo ./new_output
OpenCV Version: 4.5.4-dev
Now when I try to run another program to display an image things get out of hand really quick.
#include <iostream>
#include <opencv2/opencv.hpp>
#include<opencv2/highgui.hpp>
using namespace cv;
using namespace std;
// Driver code
int main(int argc, char** argv)
{
// Read the image file as
// imread("default.jpg");
Mat image = imread("lena.jpg",IMREAD_GRAYSCALE);
// Error Handling
if (image.empty()) {
cout << "Image File "
<< "Not Found" << endl;
// wait for any key press
cin.get();
return -1;
}
// Show Image inside a window with
// the name provided
imshow("Window Name", image);
// Wait for any keystroke
waitKey(0);
return 0;
}
Output:
username#Inspiron-7591:~/SeePluPlu/opencv-test$ ./new_output
Gtk-Message: 18:49:40.321: Failed to load module "atk-bridge"
Gtk-Message: 18:49:40.324: Failed to load module "canberra-gtk-module"
terminate called after throwing an instance of 'cv::Exception'
what(): OpenCV(4.5.4-dev) /home/username/opencv/modules/core/src/alloc.cpp:73: error: (-4:Insufficient memory) Failed to allocate 120542625076320 bytes in function 'OutOfMemoryError'
Aborted (core dumped)
but when I give root privilages...
username#Inspiron-7591:~/SeePluPlu/opencv-test$ sudo ./new_output
Gtk-Message: 18:49:31.985: Failed to load module "canberra-gtk-module"
terminate called after throwing an instance of 'cv::Exception'
what(): OpenCV(4.5.4-dev) /home/username/opencv/modules/core/src/alloc.cpp:73: error: (-4:Insufficient memory) Failed to allocate 112126257730176 bytes in function 'OutOfMemoryError'
Aborted
when I rerun the binary file (./new_output) over and over again I end up getting assertion errors as well.
I searched for loading the canberra-gtk-module and atk-bridge but whatever I found was not of any help
https://askubuntu.com/a/565789
https://askubuntu.com/a/1300284
https://askubuntu.com/questions/342202/failed-to-load-module-canberra-gtk-module-but-already-installed (all solutions in this thread)
Note: I'm positive that the image is being read and I'm able to print its size using image.size() function and I think it has something to do with the imshow() function... Not sure.
Any help or detail is very much appreciated. Thanks in advance!
When I tried to build opencv4 again, I was able to figure out that cmake was unable to find the gtk+-3.0 module that's installed in my system.
username#Inspiron-7591:~$ pkg-config --modversion gtk+-3.0
Package gtk+-3.0 was not found in the pkg-config search path.
Perhaps you should add the directory containing `gtk+-3.0.pc'
to the PKG_CONFIG_PATH environment variable
No package 'gtk+-3.0' found
even though it was already installed...
username#Inspiron-7591:~$ sudo apt-get install libgtk-3-dev
[sudo] password for username:
Reading package lists... Done
Building dependency tree
Reading state information... Done
libgtk-3-dev is already the newest version (3.24.20-0ubuntu1).
libgtk-3-dev set to manually installed.
0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.
I stumbled upon this gem of a thread while googling my issue https://stackoverflow.com/a/50038996/15341103 and I was able to make pkgconfig detect gtk+-3.0
I rebuilt opencv4 again this time and it works!
The error is not related to the image per se, its possibly an unitialized variable passed to a memory allocation routine.
what(): OpenCV(4.5.4-dev) /home/username/opencv/modules/core/src/alloc.cpp:73: error: (-4:Insufficient memory) Failed to allocate 112126257730176 bytes in function 'OutOfMemoryError'
Put a break point in alloc.cpp to figure out who is requesting allocate 112126257730176 bytes
Related
(This is the first time I ask question in stackoverflow. And I'm not fluent in English. sorry for not asking clearly)
I am trying to cross-compile opencv based c++ code on Ubuntu(using virtual box) and try to use it on RPI 4.
I use Ubuntu 16.04(using virtual box) and downloaded OpenCV 4.0.1 refer to this site https://webnautes.tistory.com/1030?category=704653
And I down loaded cross-compiiler by sudo apt-get install git gcc-arm-linux-gnueabihf make ncurses-dev build-essential
After that I wrote c++ code like this.
#include <opencv2/opencv.hpp>
using namespace cv;
using namespace std;
int main(int ac, char** av){
Mat img =imread("/home/laboman/ex/ex.jpeg", 0);
Mat img_edge;
Canny(img, img_edge, 50, 200);
imshow("original", img);
imshow("img_edge", img_edge);
waitkey(0);
return 0;
}
It works well when I just compile it on Ubuntu.
But when I tried to cross-compile it
arm-linux-gnueabihf-gcc -o qwer /home/laboman/ex/ex.cpp $(pkg-config opencv4 --libs --cflags) -std=c++11
it came out with an error
/usr/local/lib/libopencv_gapi.so: file not recognized: File format not recognized
collect2: error: ld returned 1 exit status
First, I thought there are no libopencv_gapi.so on /usr/local/lib but there were on that file.
I think the problem is that arm-linux cannot cognize(I'm not sure this is right word) the opencv when cross-compiling.
Don't know how cross-compiling can cognize the opencv.
Please tell me what I have to do?
P.S. I also tried old version of OpenCV OpenCV 2.4.9.1
I'm getting started with SDL -- that is, I have no experience and am basically just trying to get something to compile at this point.
I have a basic program:
#include "sdl/SDL.h"
int main(){
// Fire up SDL, this starts all subsystems; audio video etc.
if ( SDL_Init(SDL_INIT_EVERYTHING) < 0 ) {
fprintf(stderr, "Unable to init SDL: %s\n", SDL_GetError());
exit(1);
}
// Now Shut it down
atexit(SDL_Quit);
return 0;
}
And I have this which I'm typing into the terminal:
g++ 'sdl-config --cflags --libs' -o sdltest sdltest.cpp
where sdltest.cpp is the name of the file.
This gives me the error:
g++: error: stl-config --cflags --libs: No such file or directory
Now, previously I have (I think) installed SDL like so:
sudo apt-get install libsdl2-dev
The issue might have to do with a faulty installation -- this might not be the package I was supposed to install, or I might be completely forgetting a step here.
I'm using Ubuntu, and compiling using g++, if it wasn't apparent from context.
As of late I have been getting the following error whenever I try to compile any program that uses the open cv libraries, I use g++ to compile:
g++ Example.cpp -o Ex `pkg-config opencv --cflags --libs`
No matter the content of the file (I have checked with programs that worked a couple of weeks ago) I always get the following error:
/usr/lib64/libX11.so.6: undefined reference to `xcb_poll_for_reply64'
/usr/lib64/libX11.so.6: undefined reference to `xcb_wait_for_reply64'
Do you have any idea of what might be the cause? (and how to fix it)
An example program that fails to compile:
#include "path/opencv2/highgui/highgui.hpp"
#include "path/opencv/highgui.h"
using namespace cv;
int main (int argc, char * argv[])
{
Mat image = imread(argv[1], CV_LOAD_IMAGE_GRAYSCALE) ;
return 0;
}
Add -lxcb to your command line (this will instruct the linker linking w/ the xcb library). Please make sure the 64b version of xcb is in the linker path (you can always put it explicitly via the -L switch)
The error was caused by some changes done to the libX11.so.6, talked with the FE machines support and they fixed it.
here is the full error message:
OpenCV Error: Unspecified error (The node does not represent
a user object (unknown type?)) in cvRead, file
/home/abe/Documents/opencv-2.4.6.1/modules/core/src/persistence.cpp, line 4997
I saw on google that this might be a bug here
http://opencv-users.1802565.n2.nabble.com/face-detection-cvLoad-td4467872.html
here is how im running it
#include <cv.h>
#include <highgui.h>
using namespace std;
int main()
{
cvLoad("/home/abe/Documents/opencv-2.4.6.1/data/haarcascades/haarcascade_frontalface_alt2.xml");
return 0;
}
many programs load xml files with this function and this file is from opencv directory so i know its good I tried the workaround using cvErode in the above link but still got same error any help is appreciated
btw compile on ubuntu raring with this which works for every other program
g++ b.cpp -o b `pkg-config --cflags --libs opencv`
I compiled and installed openCV 2.4.2 in ubuntu 12.04. Under /usr/local/include I can see the directories /usr/local/opencv and /usr/local/opencv2.
Here is the code I wrote:
#include <cv.h>
#include <highgui.h>
#include <iostream>
using namespace cv;
using namespace std;
int main(int argc,char **argv)
{
Mat image;
image = imread(argv[1],1);
if(argc != 2 || !image.data)
{
cout << "No image data\n";
return -1;
}
namedWindow("Display Image",CV_WINDOW_AUTOSIZE);
imshow("Display Image",image);
waitKey(0);
return 0;
}
I compiled it using this command line:
g++ DisplayImage.cpp -o DisplayImage `pkg-config opencv --cflags --libs`
There were no compile time errors, however when I try to run the resulting binary with /DisplayImage code.png I get the following error message:
./DisplayImage: error while loading shared libraries: libopencv_core.so.2.4: cannot open shared object file: No such file or directory
You haven't put the shared library in a location where the loader can find it. look inside the /usr/local/opencv and /usr/local/opencv2 folders and see if either of them contains any shared libraries (files beginning in lib and usually ending in .so). when you find them, create a file called /etc/ld.so.conf.d/opencv.conf and write to it the paths to the folders where the libraries are stored, one per line.
for example, if the libraries were stored under /usr/local/opencv/libopencv_core.so.2.4 then I would write this to my opencv.conf file:
/usr/local/opencv/
Then run
sudo ldconfig -v
If you can't find the libraries, try running
sudo updatedb && locate libopencv_core.so.2.4
in a shell. You don't need to run updatedb if you've rebooted since compiling OpenCV.
References:
About shared libraries on Linux: http://www.eyrie.org/~eagle/notes/rpath.html
About adding the OpenCV shared libraries: http://opencv.willowgarage.com/wiki/InstallGuide_Linux
To make it more clear(and to put it together) I had to do Two things mentioned above.
1- Create a file /etc/ld.so.conf.d/opencv.conf and write to it the paths of folder where your opencv libraries are stored.(Answer by Cookyt)
2- Include the path of your opencv's .so files in LD_LIBRARY_PATH ()
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/opencv/lib
Find the folder containing the shared library libopencv_core.so.2.4 using the following command line.
sudo find / -name "libopencv_core.so.2.4*"
Then I got the result:
/usr/local/lib/libopencv_core.so.2.4.
Create a file called
/etc/ld.so.conf.d/opencv.conf
and write to it the path to the folder where the binary is stored.For example, I wrote /usr/local/lib/ to my opencv.conf file.
Run the command line as follows.
sudo ldconfig -v
Try to run the command again.
Umair R's answer is mostly the right move to solve the problem, as this error used to be caused by the missing links between opencv libs and the programme. so there is the need to specify the ld_libraty_path configuration.
ps. the usual library path is suppose to be:
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/lib
I have tried this and it worked well.
Add this link:
/usr/local/lib/*.so.*
The total is:
g++ -o main.out main.cpp -I /usr/local/include -I /usr/local/include/opencv -I /usr/local/include/opencv2 -L /usr/local/lib /usr/local/lib/*.so /usr/local/lib/*.so.*