首页 > 解决方案 > 无法显示图像opencv(c ++)

问题描述

我终于设法从源代码构建了 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 但我发现的任何内容都没有任何帮助

注意:我很肯定图像正在被读取,我可以使用 image.size() 函数打印它的大小,我认为它与 imshow() 函数有关......不确定。

非常感谢任何帮助或细节。提前致谢!

标签: c++opencvgtkubuntu-20.04

解决方案


当我再次尝试构建 opencv4 时,我发现 cmake 无法找到安装在我系统中的 gtk+-3.0 模块。

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

即使它已经安装...

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.

我在谷歌搜索我的问题https://stackoverflow.com/a/50038996/15341103时偶然发现了这个线程的宝石,我能够让 pkgconfig 检测到 gtk+-3.0

这次我再次重建了opencv4,它可以工作了!


推荐阅读