Video feed freeze on mouse click event OpenCV

asked 2018-05-29 02:44:44 -0500

eirikaso gravatar image

Hi I'm doing some work on stereo vision using ROS and OpenCV, and are currently trying to draw on a relevant area from a live image feed. To do this, I have created a script to open up a second image with a still image when I double click the live stream. This still image is the one I want to draw on.

The problem is that when I double click the image stream, the image stream pauses as well as the still image comes up. I then have to press a random key on the keyboard in order for it to resume it's video feed.

This error only happens when I double click the image for the first time after starting this node. If I continue and double click to pause at a new image, it works well.

Is there a problem with the imageCallback from the camera not being updated after using the onMouseClick callback for the first time?

This is my code:

#include <ros/ros.h>
#include <image_transport/image_transport.h>
#include <opencv2/highgui.hpp>
#include <cv_bridge/cv_bridge.h>


using namespace cv;

cv_bridge::CvImagePtr cv_src_ptr;



//static void onMouseClick (int event, int x, int y, int flags, void*)
void onMouseClick (int event, int x, int y, int flags, void*)
{
  if (event == EVENT_LBUTTONDBLCLK)
  {
      namedWindow("Select object");
      imshow("Select object", cv_src_ptr->image);
      //waitKey(1);
  }

}

void imageCallback(const sensor_msgs::ImageConstPtr& msg)
{
  try
  {
    cv_src_ptr = cv_bridge::toCvCopy(msg, sensor_msgs::image_encodings::BGR8);
    imshow("view", cv_bridge::toCvShare(msg, "bgr8")->image);
    cv::waitKey(30);
  }
  catch (cv_bridge::Exception& e)
  {
    ROS_ERROR("Could not convert from '%s' to 'bgr8'.", msg->encoding.c_str());
  }
}

int main(int argc, char **argv)
{
  ros::init(argc, argv, "image_listener");    // initialize node
  ros::NodeHandle nh;                         // start node
  cv::namedWindow("view");
  cv::startWindowThread();
  image_transport::ImageTransport it(nh);
  //setMouseCallback("view", onMouseClick, 0);
  setMouseCallback("view", onMouseClick, 0);
  image_transport::Subscriber sub = it.subscribe("/stereo/left/image_raw", 0, imageCallback);
  ros::spin();
  cv::destroyWindow("view");
}
edit retag flag offensive close merge delete