Use opencv-gray processing in ROS

Nothing to say, just look at the code to understand:

#include<ros/ros.h> //ros standard library header file
#include<iostream> //C++ standard input and output library
/*
  CvBridge library included in cv_bridge
*/
#include<cv_bridge/cv_bridge.h>
/*
  ROS image type encoding function
*/
#include<sensor_msgs/image_encodings.h>
/*
   The image_transport header file is used to publish and subscribe to image messages on topics in the ROS system.
*/
#include<image_transport/image_transport.h>

/ / OpenCV2 standard header file
#include<opencv2/core/core.hpp>
#include<opencv2/highgui/highgui.hpp>
#include<opencv2/imgproc/imgproc.hpp>

Static const std::string OPENCV_WINDOW1 = "Image window"; //Define the input window name
Static const std::string OPENCV_WINDOW2 = "Gray window"; //Define the output window name
/ / Define a converted class
Class RGB_GRAY
{
Private:
    Ros::NodeHandle nh_; //Define the ROS handle
    Image_transport::ImageTransport it_; //Define an image_transport instance
    Image_transport::Subscriber image_sub_; //Define ROS image receiver
    //image_transport::Publisher image_pub_; //Define the ROS image publisher
Public:
    RGB_GRAY()
      :it_(nh_) // constructor
    {
        Image_sub_ = it_.subscribe("cv_camera/image_raw", 1, &RGB_GRAY::convert_callback, this); //Define the image acceptor, subscribe to the topic "camera/rgb/image_raw"
       // image_pub_ = it_.publishe("", 1); //Define the image publisher
        / / Initialize the input and output window
        Cv::namedWindow(OPENCV_WINDOW1);
        Cv::namedWindow(OPENCV_WINDOW2);
    }
    ~RGB_GRAY() //Destructor
    {
         Cv::destroyWindow(OPENCV_WINDOW1);
         Cv::destroyWindow(OPENCV_WINDOW2);
    }
    /*
      This is a format conversion callback function for ROS and OpenCV, which takes the image format from sensor_msgs/Image ---> cv::Mat
    */
    Void convert_callback(const sensor_msgs::ImageConstPtr& msg)
    {
        Cv_bridge::CvImagePtr cv_ptr; // Declare an instance of a CvImage pointer

        Try
        {
            Cv_ptr = cv_bridge::toCvCopy(msg, sensor_msgs::image_encodings::BGR8); // extract the image information in the ROS message, generate a new cv type image, and copy it to the CvImage pointer.
        }
        Catch(cv_bridge::Exception& e) //Exception handling
        {
            ROS_ERROR("cv_bridge exception: %s", e.what());
            Return;
        }

        Image_process(cv_ptr->image); //Get an image of type cv::Mat, in the image of the CvImage pointer, pass the result to the handler
    }
    /*
       This is the main function of image processing. The main program of image processing is generally written in this function. The example here is just a color image to grayscale image conversion.
    */
    Void image_process(cv::Mat img)
    {
       Cv::Mat img_out;
       Cv::cvtColor(img, img_out, CV_RGB2GRAY); //convert to grayscale image
       Cv::imshow(OPENCV_WINDOW1, img);
       Cv::imshow(OPENCV_WINDOW2, img_out);
       Cv::waitKey(5);
    }
};

/ / Main function
Int main(int argc, char** argv)
{
    Ros::init(argc, argv, "RGB");
    RGB_GRAY obj;
    Ros::spin();
}