我終于設法從源代碼構建了 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() 函式有關......不確定。
非常感謝任何幫助或細節。提前致謝!
uj5u.com熱心網友回復:
當我再次嘗試構建 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 并且它有效!
uj5u.com熱心網友回復:
該錯誤與影像本身無關,它可能是傳遞給記憶體分配例程的未初始化變數。
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
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/327246.html
標籤:C opencv gtk ubuntu-20.04
