【问题标题】:Unable to display image opencv (c++)无法显示图像opencv(c ++)
【发布时间】: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 的内容,但我找到的都没有任何帮助

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

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

【问题讨论】:

    标签: c++ opencv gtk ubuntu-20.04


    【解决方案1】:

    当我再次尝试构建 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,它可以工作了!

    【讨论】:

      【解决方案2】:

      错误与图像本身无关,它可能是传递给内存分配例程的未初始化变量。

      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'

      alloc.cpp 中设置一个断点,以确定是谁在请求allocate 112126257730176 bytes

      【讨论】:

      • 无法加载模块“atk-bridge”无法加载模块“canberra-gtk-module”怎么办?
      • 该问题与 openCV 有关,因为缺少库不应导致内存分配错误。 OpenCV 应该检测到库的缺失并正常关闭或尝试使用回退。正确安装 gtk 可能会消除此错误 - 但我认为用户也检测到了内部错误。
      • 我修复了这个问题,原来它与 gtk 模块有关,我重建了 opencv 以确保编译器这次能够在构建过程中检测到 gtk-+3.0 模块. stackoverflow.com/a/50038996/15341103
      猜你喜欢
      • 1970-01-01
      • 2015-12-03
      • 1970-01-01
      • 1970-01-01
      • 2016-05-12
      • 2021-04-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多