openCv imshow doesn't display image
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:
namedWindow "view":