0
$\begingroup$

Rosanswers logo

I am trying to process color kinect frames following the tutorial from the link working with cv bridge. I have a training image juice_color.png which I can visualize as RGB. I specified the encoding as cv_img.encoding = "rgb8";

Going by the same logic, I used rgb8 for the incoming kinect frames, but the display window imshow( WINDOW2, cv_ptr_frames->image ); shows the kinect frames in gray scale. How do I work with RGB kinect frames ? How to prevent the color format change to gray scale? Thanking you.

static const std::string OPENCV_WINDOW1 = "Good Matches";
static const std::string WINDOW1 = "Training Image ";
static const std::string WINDOW2 = "Test Frames ";

class ImageConverter
{
  ros::NodeHandle nh_;
  image_transport::ImageTransport it_;
  image_transport::Subscriber image_sub_;
  image_transport::Publisher image_pub_;
  

  
public:
  ImageConverter()
    : it_(nh_)
  {
    // Subscrive to input video feed and publish output video feed
    image_sub_ = it_.subscribe("/camera/rgb/image_rect", 1, 
      &ImageConverter::imageCb, this);
    image_pub_ = it_.advertise("/image_converter/output_video", 1);

   cv::namedWindow(OPENCV_WINDOW1);
   
  }

  ~ImageConverter()
  {
    cv::destroyWindow(OPENCV_WINDOW1);
   
  }

  void imageCb(const sensor_msgs::ImageConstPtr& msg)
  {
  
 cv_bridge::CvImage cv_img;   // training image
 cv_bridge::CvImage cv_img_proc;  

 cv_bridge::CvImagePtr cv_ptr_frames; // kinect frames
 
 cv::Mat object = imread( "juice_color.png" );
  
   imshow( WINDOW1, object );  //Displayed in RGB color
     
 cv_img.header.stamp = ros::Time::now();
 cv_img.header.frame_id=msg->header.frame_id;
 cv_img.encoding = "rgb8";
 cv_img.encoding = sensor_msgs::image_encodings::RGB8;
 cv_img.image = object;
 
  
  sensor_msgs::Image im;
  cv_img.toImageMsg(im);  
       
       
   
  try
  {
     cv_ptr_frames = cv_bridge::toCvCopy(msg, sensor_msgs::image_encodings::RGB8);
   }
    catch (cv_bridge::Exception& e)
    {
     ROS_ERROR("cv_bridge exception: %s", e.what());
     return;
  }
   
         
       
         
    imshow( WINDOW2, cv_ptr_frames->image   );
}

Originally posted by SM on ROS Answers with karma: 15 on 2013-11-07

Post score: 0

$\endgroup$

1 Answer 1

0
$\begingroup$

Rosanswers logo

Typically OpenCV highgui functions (imread, imwrite, imshow etc) use color model BGR8, so probably your image is in BGR8 not RGB8. Pushing it through cv_bridge with wrong color model may cause trouble, so try BGR8.


Originally posted by Wolf with karma: 7555 on 2014-01-17

This answer was ACCEPTED on the original site

Post score: 1

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.