无法通过 cv::imshow() 显示 window。我错过了什么?
Unable to display window via cv::imshow(). What am I missing?
我正在研究简单的 OpenCV 程序介绍。我在 Virtualbox 中 运行ning(如 Ubuntu 20.04)。 C++/VS代码。 LLDB 调试器。如果我 运行 MacOS 上的 virtualbox 或 Windows.
同样的错误
---main.cpp---
#include <opencv2/opencv.hpp> // Catch all for all libraries within OpenCV
int main()
{
cv::VideoCapture cap; //initialize capture
cap.open(0);
if (!cap.isOpened()) // check if succeeded to connect to the camera
CV_Assert("Cam open failed");
cap.set(cv::CAP_PROP_FRAME_WIDTH, 1280);
cap.set(cv::CAP_PROP_FRAME_HEIGHT, 720);
// cv::namedWindow("window", 1); //create window to show image
while (1)
{
cv::Mat image; //Create Matrix to store image
cap >> image; //copy webcam stream to image
// If the frame is empty, break immediately
if (image.empty())
break;
cv::imshow("window", image); //print image to screen. <-- fail here.
cv::waitKey(33); //delay 33ms
}
return 0;
}
---CMakeLists.txt---
# https://gist.github.com/UnaNancyOwen/5061d8c966178b753447e8a9f9ac8cf1
cmake_minimum_required(VERSION 3.0.0)
set(CMAKE_TOOLCHAIN_FILE "/home/vagrant/Desktop/vcpkg/scripts/buildsystems/vcpkg.cmake")
project(openCV_test VERSION 0.1.0)
add_executable(${PROJECT_NAME} main.cpp)
set(OpenCV_DIR "/home/vagrant/Desktop/vcpkg/installed/x64-linux/share/opencv")
# set(WITH_GTK "ON") # testing here...
# set(WITH_QT "OFF")
find_package(OpenCV REQUIRED)
# Display some variables
message(STATUS "OpenCV library status:")
message(STATUS " version: ${OpenCV_VERSION}")
message(STATUS " libraries: ${OpenCV_LIBS}")
message(STATUS " include path: ${OpenCV_INCLUDE_DIRS}")
if( OpenCV_FOUND )
# Additional Include Directories
include_directories( ${OpenCV_INCLUDE_DIRS} )
# Additional Library Directories
link_directories( ${OpenCV_LIB_DIR} )
# Additional Dependencies
target_link_libraries( ${PROJECT_NAME} ${OpenCV_LIBS} )
endif()
注意,我已经尝试从 git 存储库手动构建 OpenCV 包,并使用 VCPKG 包管理器。无论哪种方式,结果都是一样的。同样失败。请注意,相机在 virtualbox 中运行良好。通过 html5 网站上的 virtualbox 浏览器进行测试,用于测试相机的使用。当我逐步执行代码时,我可以看到相机 LED 在适当的时候亮起。系统炸弹始终在 cv::imshow() 行代码。
错误消息指出:
terminate called after throwing an instance of 'cv::Exception'
what(): OpenCV(4.5.1-dev)
/home/vagrant/opencv-master/modules/highgui/src/window.cpp:679: error:
(-2:Unspecified error) The function is not implemented. Rebuild the
library with Windows, GTK+ 2.x or Cocoa support. If you are on Ubuntu
or Debian, install libgtk2.0-dev and pkg-config, then re-run cmake or
configure script in function 'cvShowImage'
这是关于 imshow() 函数的 class 信息:
void cv::imshow(const cv::String &winname, cv::InputArray mat)
+1 overload
Displays an image in the specified window. The function imshow
displays an image in the specified window. If the window was created
with the cv::WINDOW_AUTOSIZE flag, the image is shown with its
original size, however it is still limited by the screen resolution.
Otherwise, the image is scaled to fit the window. The function may
scale the image, depending on its depth: - If the image is 8-bit
unsigned, it is displayed as is. - If the image is 16-bit unsigned or
32-bit integer, the pixels are divided by 256. That is, the value
range [0,255
Parameters: winname – Name of the window. mat – Image to be shown.
我已经完成了 libgtk2.0-dev 和 pkg-config 的所有安装组合,但没有结果。在构建具有一致(失败)结果的 OpenCV 库之前,我什至安装了这些库。我知道我在一年前成功地在 virtualbox 上使用了这个例子。根本不清楚错误消息是否准确表示失败。
所以这里是逐步调试时注意到的错误的屏幕截图。那个错误提示函数没有实现。使用 Windows、GTK+ 2.x 或 Cocoa 支持重建库。如果你在 Ubuntu 或 Debian 上,安装 libgtk2.0-dev 和 pkg-config,然后重新 运行 cmake 或在函数 'cvShowImage' 中配置脚本是没有意义的对我来说。但仔细观察绿色圆圈中的注释。 highgui.h 是哪个?哎哟。这些双目录是在尝试通过 VCPKG 包管理器安装 OpenCV 之后创建的(并且因 CMakeLists.txt 而失败)。所以我然后通过 github repo 进行了额外的安装。我没有删除 VCPKG 的东西。这真是一团糟。
修复?删除整个 virtualbox 并从头开始。我按照 https://linuxize.com/post/how-to-install-opencv-on-debian-10/
中概述的步骤从 github 源成功构建了 OpenCV
$ sudo apt install build-essential cmake git pkg-config libgtk-3-dev \
libavcodec-dev libavformat-dev libswscale-dev libv4l-dev \
libxvidcore-dev libx264-dev libjpeg-dev libpng-dev libtiff-dev \
gfortran openexr libatlas-base-dev python3-dev python3-numpy \
libtbb2 libtbb-dev libdc1394-22-dev
$ mkdir ~/opencv_build && cd ~/opencv_build
$ git clone https://github.com/opencv/opencv.git
$ git clone https://github.com/opencv/opencv_contrib.git
$ cd ~/opencv_build/opencv
$ mkdir build && cd build
$ cmake -D CMAKE_BUILD_TYPE=RELEASE \
-D CMAKE_INSTALL_PREFIX=/usr/local \
-D INSTALL_C_EXAMPLES=ON \
-D INSTALL_PYTHON_EXAMPLES=ON \
-D OPENCV_GENERATE_PKGCONFIG=ON \
-D OPENCV_EXTRA_MODULES_PATH=~/opencv_build/opencv_contrib/modules \
-D BUILD_EXAMPLES=ON ..
$ make -j4 # Start the compilation process (long time!)
$ sudo make install # Install OpenCV
$ pkg-config --modversion opencv4 # test the install
--> 4.5.1
我仍然不完全理解整个 rebuild the library... GTK.. pkg-config
的东西,但我怀疑根本问题是对支持库所在位置的混淆。此处发布的信息以防其他人遇到错误消息。
还有一个旁注 -- 这是我的最终 CMakeLists.txt 文件,它完全适用于从源代码构建的库:
cmake_minimum_required(VERSION 3.0.0)
project(openCV_test VERSION 0.1.0)
add_executable(${PROJECT_NAME} main.cpp)
set(OpenCV_DIR "/usr/local/lib") # per github install of OpenCV
find_package(OpenCV REQUIRED)
target_include_directories(${PROJECT_NAME} PRIVATE ${OpenCV_INCLUDE_DIRS})
我正在研究简单的 OpenCV 程序介绍。我在 Virtualbox 中 运行ning(如 Ubuntu 20.04)。 C++/VS代码。 LLDB 调试器。如果我 运行 MacOS 上的 virtualbox 或 Windows.
同样的错误---main.cpp---
#include <opencv2/opencv.hpp> // Catch all for all libraries within OpenCV
int main()
{
cv::VideoCapture cap; //initialize capture
cap.open(0);
if (!cap.isOpened()) // check if succeeded to connect to the camera
CV_Assert("Cam open failed");
cap.set(cv::CAP_PROP_FRAME_WIDTH, 1280);
cap.set(cv::CAP_PROP_FRAME_HEIGHT, 720);
// cv::namedWindow("window", 1); //create window to show image
while (1)
{
cv::Mat image; //Create Matrix to store image
cap >> image; //copy webcam stream to image
// If the frame is empty, break immediately
if (image.empty())
break;
cv::imshow("window", image); //print image to screen. <-- fail here.
cv::waitKey(33); //delay 33ms
}
return 0;
}
---CMakeLists.txt---
# https://gist.github.com/UnaNancyOwen/5061d8c966178b753447e8a9f9ac8cf1
cmake_minimum_required(VERSION 3.0.0)
set(CMAKE_TOOLCHAIN_FILE "/home/vagrant/Desktop/vcpkg/scripts/buildsystems/vcpkg.cmake")
project(openCV_test VERSION 0.1.0)
add_executable(${PROJECT_NAME} main.cpp)
set(OpenCV_DIR "/home/vagrant/Desktop/vcpkg/installed/x64-linux/share/opencv")
# set(WITH_GTK "ON") # testing here...
# set(WITH_QT "OFF")
find_package(OpenCV REQUIRED)
# Display some variables
message(STATUS "OpenCV library status:")
message(STATUS " version: ${OpenCV_VERSION}")
message(STATUS " libraries: ${OpenCV_LIBS}")
message(STATUS " include path: ${OpenCV_INCLUDE_DIRS}")
if( OpenCV_FOUND )
# Additional Include Directories
include_directories( ${OpenCV_INCLUDE_DIRS} )
# Additional Library Directories
link_directories( ${OpenCV_LIB_DIR} )
# Additional Dependencies
target_link_libraries( ${PROJECT_NAME} ${OpenCV_LIBS} )
endif()
注意,我已经尝试从 git 存储库手动构建 OpenCV 包,并使用 VCPKG 包管理器。无论哪种方式,结果都是一样的。同样失败。请注意,相机在 virtualbox 中运行良好。通过 html5 网站上的 virtualbox 浏览器进行测试,用于测试相机的使用。当我逐步执行代码时,我可以看到相机 LED 在适当的时候亮起。系统炸弹始终在 cv::imshow() 行代码。
错误消息指出:
terminate called after throwing an instance of 'cv::Exception'
what(): OpenCV(4.5.1-dev) /home/vagrant/opencv-master/modules/highgui/src/window.cpp:679: error: (-2:Unspecified error) The function is not implemented. Rebuild the library with Windows, GTK+ 2.x or Cocoa support. If you are on Ubuntu or Debian, install libgtk2.0-dev and pkg-config, then re-run cmake or configure script in function 'cvShowImage'
这是关于 imshow() 函数的 class 信息:
void cv::imshow(const cv::String &winname, cv::InputArray mat) +1 overload
Displays an image in the specified window. The function imshow displays an image in the specified window. If the window was created with the cv::WINDOW_AUTOSIZE flag, the image is shown with its original size, however it is still limited by the screen resolution. Otherwise, the image is scaled to fit the window. The function may scale the image, depending on its depth: - If the image is 8-bit unsigned, it is displayed as is. - If the image is 16-bit unsigned or 32-bit integer, the pixels are divided by 256. That is, the value range [0,255
Parameters: winname – Name of the window. mat – Image to be shown.
我已经完成了 libgtk2.0-dev 和 pkg-config 的所有安装组合,但没有结果。在构建具有一致(失败)结果的 OpenCV 库之前,我什至安装了这些库。我知道我在一年前成功地在 virtualbox 上使用了这个例子。根本不清楚错误消息是否准确表示失败。
所以这里是逐步调试时注意到的错误的屏幕截图。那个错误提示函数没有实现。使用 Windows、GTK+ 2.x 或 Cocoa 支持重建库。如果你在 Ubuntu 或 Debian 上,安装 libgtk2.0-dev 和 pkg-config,然后重新 运行 cmake 或在函数 'cvShowImage' 中配置脚本是没有意义的对我来说。但仔细观察绿色圆圈中的注释。 highgui.h 是哪个?哎哟。这些双目录是在尝试通过 VCPKG 包管理器安装 OpenCV 之后创建的(并且因 CMakeLists.txt 而失败)。所以我然后通过 github repo 进行了额外的安装。我没有删除 VCPKG 的东西。这真是一团糟。
修复?删除整个 virtualbox 并从头开始。我按照 https://linuxize.com/post/how-to-install-opencv-on-debian-10/
中概述的步骤从 github 源成功构建了 OpenCV$ sudo apt install build-essential cmake git pkg-config libgtk-3-dev \
libavcodec-dev libavformat-dev libswscale-dev libv4l-dev \
libxvidcore-dev libx264-dev libjpeg-dev libpng-dev libtiff-dev \
gfortran openexr libatlas-base-dev python3-dev python3-numpy \
libtbb2 libtbb-dev libdc1394-22-dev
$ mkdir ~/opencv_build && cd ~/opencv_build
$ git clone https://github.com/opencv/opencv.git
$ git clone https://github.com/opencv/opencv_contrib.git
$ cd ~/opencv_build/opencv
$ mkdir build && cd build
$ cmake -D CMAKE_BUILD_TYPE=RELEASE \
-D CMAKE_INSTALL_PREFIX=/usr/local \
-D INSTALL_C_EXAMPLES=ON \
-D INSTALL_PYTHON_EXAMPLES=ON \
-D OPENCV_GENERATE_PKGCONFIG=ON \
-D OPENCV_EXTRA_MODULES_PATH=~/opencv_build/opencv_contrib/modules \
-D BUILD_EXAMPLES=ON ..
$ make -j4 # Start the compilation process (long time!)
$ sudo make install # Install OpenCV
$ pkg-config --modversion opencv4 # test the install
--> 4.5.1
我仍然不完全理解整个 rebuild the library... GTK.. pkg-config
的东西,但我怀疑根本问题是对支持库所在位置的混淆。此处发布的信息以防其他人遇到错误消息。
还有一个旁注 -- 这是我的最终 CMakeLists.txt 文件,它完全适用于从源代码构建的库:
cmake_minimum_required(VERSION 3.0.0)
project(openCV_test VERSION 0.1.0)
add_executable(${PROJECT_NAME} main.cpp)
set(OpenCV_DIR "/usr/local/lib") # per github install of OpenCV
find_package(OpenCV REQUIRED)
target_include_directories(${PROJECT_NAME} PRIVATE ${OpenCV_INCLUDE_DIRS})