I'm working simple intro OpenCV program. I'm running in Virtualbox (as Ubuntu 20.04). C++/ VS Code. LLDB debugger. Same fault if I run the virtualbox on MacOS or 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()
Note, I've tried manually building the OpenCV package from git repo, and using the VCPKG package manager. Same results either way. Same fail. Note, camera works great in virtualbox. Tested via in virtualbox browser on html5 websites for testing camera use. As I step thru the code, I can see the camera LED come on at appropriate time. System bombs consistently at cv::imshow() line of code.
Error message states:
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'
Here is the class info on the imshow() function:
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.
I've done every combination of installation for both libgtk2.0-dev and pkg-config without result. I've even installed those libraries before building the OpenCV library with consistent (fail) results. I know I've used this example on a virtualbox a year ago with success. It's not at all clear that the error message is an accurate representation of the failure.