Ask Your Question
0

openCv imshow doesn't display image

asked 2019-01-16 09:43:15 -0600

ROStutorialLearner gravatar image

updated 2019-01-16 11:41:29 -0600

I'm trying to display a Compressed Image from camera/image/Compressed in C++ using OpenCV, however, all I get is a blank window with no image, I've verified with rqt_image_view_imageView that there is infact images being published.

The error:

OpenCV Error: Assertion failed (0 <= i && i < (int)vv.size()) in getMat_, file /build/opencv-L2vuMj/opencv-3.2.0+dfsg/modules/core/src/matrix.cpp, line 1244
terminate called after throwing an instance of 'cv::Exception'
  what():  /build/opencv-L2vuMj/opencv-3.2.0+dfsg/modules/core/src/matrix.cpp:1244: error: (-215) 0 <= i && i < (int)vv.size() in function getMat_

What's going on and why is it not working?

void imageCallback(const sensor_msgs::CompressedImageConstPtr& msg)
{
    try
    {
        cv::Mat image = cv::imdecode(cv::Mat(msg->data),1);//convert compressed image data to cv::Mat
        cv::imshow("view", image);

        cv::waitKey(10);
    }
    catch (cv_bridge::Exception& e)
    {
        ROS_ERROR("Could not convert to image!");
    }
}

int main(int argc, char **argv)
{
    ros::init(argc, argv, "image_listener");
    ros::NodeHandle nh;
    cv::namedWindow("view");
    cv::startWindowThread();
    ros::Subscriber sub = nh.subscribe("/camera/image/compressed", 1, imageCallback);
    ros::spin();
    cv::destroyWindow("view");
}

Cmakelists.txt:

cmake_minimum_required(VERSION 2.8.3)
project(test)

## OpenCV required
find_package( OpenCV 2 REQUIRED )

find_package(catkin REQUIRED COMPONENTS
  geometry_msgs
  roscpp
  sensor_msgs
  std_msgs
  image_transport
  cv_bridge
  std_msgs
)

catkin_package(
)

include_directories(
# include
  ${catkin_INCLUDE_DIRS}
)

add_executable(our_robot src/lf.cpp)
target_link_libraries(test ${catkin_LIBRARIES})
target_link_libraries(test ${OpenCV_LIBS} )

rqt_image_view_ImageView: image description

namedWindow "view":

image description

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
1

answered 2019-01-16 12:07:01 -0600

ROStutorialLearner gravatar image

All of my issues are solved by upgrading to OpenCV 3 instead of using OpenCV 2.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2019-01-16 09:43:15 -0600

Seen: 3,323 times

Last updated: Jan 16 '19