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!
CodePudding user response:
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!
CodePudding user response:
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