无法显示图像 opencv (c++)

Unable to display image 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() 函数有关。 ..不确定。

非常感谢

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

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

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

当我再次尝试构建 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.

我在谷歌搜索我的问题 时偶然发现了这个 gem 线程,我能够让 pkgconfig 检测到 gtk+-3.0

这次又重新编译了opencv4,成功了!