Exception when showing an Image with OpenCV

a 夏天 提交于 2020-01-25 07:26:06

问题


I try to show an Image with the OpenCV-function 'imshow()'.

 cv::Mat im = cv::imread("/home/franek/Schreibtisch/image.jpg",cv::IMREAD_GRAYSCALE);
 cv::imshow("original", im);

Now it tells me:

OpenCV Error: Assertion failed (scn == 3 || scn == 4) in cvtColor, file /build/opencv-L2vuMj/opencv-3.2.0+dfsg/modules/imgproc/src/color.cpp, line 9716 terminate called after throwing an instance of 'cv::Exception' what(): /build/opencv-L2vuMj/opencv-3.2.0+dfsg/modules/imgproc/src/color.cpp:9716: error: (-215) scn == 3 || scn == 4 in function cvtColor

When I replace cv::IMREAD_GRAYSCALE with cv::IMREAD_COLOR, the picture is shown and everything is fine. But I need a grayscale image.

Edit: the whole code:

cv::namedWindow("original", cv::WINDOW_GUI_EXPANDED);
  cv::namedWindow("grayScale", cv::WINDOW_GUI_EXPANDED);
  cv::namedWindow("cornerImage", cv::WINDOW_GUI_EXPANDED);
  cv::startWindowThread();

  cv::Mat im = cv::imread("/home/franek/Schreibtisch/image.jpg", cv::IMREAD_COLOR);
  cv::imshow("original", im);
  cv::waitKey(0);
  cv::Mat img = cv::imread("/home/franek/Schreibtisch/image.jpg", cv::IMREAD_GRAYSCALE);
  cv::imshow("grayScale", img);
  cv::waitKey(0);

edit edit: with ldd i found out the dependencies:

libopencv_imgcodecs.so.3.2 => /usr/lib/x86_64-linux-gnu/libopencv_imgcodecs.so.3.2 (0x00007f2ba95d0000) libopencv_imgproc.so.3.2 => /usr/lib/x86_64-linux-gnu/libopencv_imgproc.so.3.2 (0x00007f2ba8d77000) libopencv_core.so.3.2 => /usr/lib/x86_64-linux-gnu/libopencv_core.so.3.2 (0x00007f2ba893c000) libopencv_highgui.so.4.1 => /usr/local/lib/libopencv_highgui.so.4.1 (0x00007f2ba8724000) libopencv_imgcodecs.so.4.1 => /usr/local/lib/libopencv_imgcodecs.so.4.1 (0x00007f2ba7e72000) libopencv_imgproc.so.4.1 => /usr/local/lib/libopencv_imgproc.so.4.1 (0x00007f2ba557e000) libopencv_core.so.4.1 => /usr/local/lib/libopencv_core.so.4.1 (0x00007f2ba429b000)

Is that the solution? The core and the highui aren't the same version...


回答1:


I found the solution, the Problem was, that my Code depends in another function from < <cv_bridge/cv_bridge.h> this is a ROS Specific lib. This lib uses Opencv 2.3. I head to use a fork, where someone ported this lib to OpenCV 4 here. Now the whole thing works just fine!




回答2:


You can resolve your problem if you are going to compile opencv with the correct settings:

git clone https://github.com/opencv/opencv.git
git clone https://github.com/opencv/opencv_contrib.git
cd opencv
mkdir build
cd build/
cmake -D CMAKE_BUILD_TYPE=DEBUG -D CMAKE_INSTALL_PREFIX=/opt/opencv -D \ WITH_V4L=ON BUILD_EXAMPLES=ON -D WITH_OPENGL=ON -D \ OPENCV_EXTRA_MODULES_PATH=../../opencv_contrib/modules ..
make -j4

sudo make install



来源:https://stackoverflow.com/questions/57100214/exception-when-showing-an-image-with-opencv

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!