Skip to content

yuv422_yuy2 support #170

@TomoyukiaW

Description

@TomoyukiaW

I'm using the YUYV format output camera and usb_cam package as a driver. usb_cam uses image_transport::CameraPublisher to publish the image. Published raw_image encoding was "encoding: yuv422_yuy2" and raw_image/compressed format was "format: yuv422_yuy2; jpeg compressed mono8".
raw_image could be shown in RViz2. But raw_image/compressed couldn't shown in RViz2, and we got some errors in some image conversion processes (e.g. compressing with ffmpeg). It seems that the compression process for yuv image has a problem.

Then, the following modification worked in my environment. How about this?
cv_bridge supports to convert yuv422_yuy2 and yuv422 format image to cv Mat. For compressed_subscriber.cpp, I couldn't find the suitable conversion pattern and I thought probably most application don't use yuv format in its processing.

--- a/compressed_image_transport/src/compressed_publisher.cpp
+++ b/compressed_image_transport/src/compressed_publisher.cpp
@@ -201,7 +201,7 @@ void CompressedPublisher::publish(
       {
         // Target image format
         std::string targetFormat;
-        if (enc::isColor(message.encoding))
+        if (enc::isColor(message.encoding) || message.encoding == enc::YUV422 || message.encoding == enc::YUV422_YUY2)
         {
           // convert color images to BGR8 format
           targetFormat = "bgr8";

--- a/compressed_image_transport/src/compressed_subscriber.cpp
+++ b/compressed_image_transport/src/compressed_subscriber.cpp
@@ -143,7 +143,7 @@ void CompressedSubscriber::internalCallback(const CompressedImage::ConstSharedPt
 
       cv_ptr->encoding = image_encoding;
 
-      if ( enc::isColor(image_encoding))
+      if ( enc::isColor(image_encoding) || image_encoding == enc::YUV422 || image_encoding == enc::YUV422_YUY2)
       {
         std::string compressed_encoding = message->format.substr(split_pos);
         bool compressed_bgr_image = (compressed_encoding.find("compressed bgr") != std::string::npos);
@@ -160,6 +160,9 @@ void CompressedSubscriber::internalCallback(const CompressedImage::ConstSharedPt
 
           if ((image_encoding == enc::BGRA8) || (image_encoding == enc::BGRA16))
             cv::cvtColor(cv_ptr->image, cv_ptr->image, CV_BGR2BGRA);
+
+          if ((image_encoding == enc::YUV422) || (image_encoding == enc::YUV422_YUY2))
+            cv_ptr->encoding = enc::BGR8;
         } else
         {
           // if necessary convert colors from rgb to bgr
@@ -171,6 +174,9 @@ void CompressedSubscriber::internalCallback(const CompressedImage::ConstSharedPt
 
           if ((image_encoding == enc::RGBA8) || (image_encoding == enc::RGBA16))
             cv::cvtColor(cv_ptr->image, cv_ptr->image, CV_RGB2RGBA);
+
+          if ((image_encoding == enc::YUV422) || (image_encoding == enc::YUV422_YUY2))
+            cv_ptr->encoding = enc::RGB8;
         }
       }
       if (message->format.find("jpeg") != std::string::npos && enc::bitDepth(image_encoding) == 16) {

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions