Search code examples
c++opencvubuntuvirtualboxvcpkg

Unable to display window via cv::imshow(). What am I missing?


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.


Solution

  • C++ Error Message in VS Code

    So here is a screen shot of the error noted when doing debugging step by step. That error note 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' makes no sense to me at all. But look closely at the notes in the green circle. Which highgui.h is which? Ouch. Those dual directories were created after trying to install OpenCV via the VCPKG package manager (and failing terribly with CMakeLists.txt). So I then did a total additional install via github repo. I did not delete the VCPKG stuff. This was a mess.

    The fix? Delete the entire virtualbox and start again from scratch. I did a successful OpenCV build from github source following the steps outlined at https://linuxize.com/post/how-to-install-opencv-on-debian-10/

    $ 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 
    

    I still don't fully understand the whole rebuild the library... GTK.. pkg-config stuff, but I'm suspecting the root issue is confusion on where support libraries are located. Info posted here in case others hit the error message.

    One more side note -- Here's my final CMakeLists.txt file that was fully functional with the library built from source:

    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})