【发布时间】:2021-12-04 20:18:07
【问题描述】:
我终于设法从源代码构建了 opencv4.5.4 库,但现在我遇到了无法修复的错误。
我正在使用这篇中等文章作为我的指南https://medium.com/analytics-vidhya/how-to-install-opencv-for-visual-studio-code-using-ubuntu-os-9398b2f32d53
当我尝试执行一个打印安装的 opencv 版本的简单程序时,它执行时没有错误。
#include <opencv2/opencv.hpp>
#include <iostream>
int main()
{
std::cout << "OpenCV Version: "<< CV_VERSION << std::endl;
return 0;
}
制作文件:
CC = g++
PROJECT = new_output
SRC = new.cpp
LIBS = `pkg-config --cflags --libs opencv4`
$(PROJECT) : $(SRC)
$(CC) $(SRC) -o $(PROJECT) $(LIBS)
输出:
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
现在,当我尝试运行另一个程序来显示图像时,事情很快就会失控。
#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;
}
输出:
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)
但是当我授予root权限时...
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
当我一遍又一遍地重新运行二进制文件 (./new_output) 时,我最终也会遇到断言错误。
我搜索了加载 canberra-gtk-module 和 atk-bridge 的内容,但我找到的都没有任何帮助
- 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(本帖所有解决方案)
注意:我很肯定图像正在被读取,我可以使用 image.size() 函数打印它的大小,我认为它与 imshow() 函数有关......不确定.
任何帮助或细节非常感谢。提前致谢!
【问题讨论】:
标签: c++ opencv gtk ubuntu-20.04