ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question

Revision history [back]

Lets assume that you successfully launched ros package of your camera and your camera is publishing image under a topic called "/camera_image_raw". Then you can receive this image with cv_brdige and converting to a open_cv image in your callback as ;

#include <cv_bridge/cv_bridge.h>
#include <image_transport/image_transport.h>
#include <sensor_msgs/Image.h>


YouClass::YourClass(){
        // Camera image topic subscriber.
        camera_subscriber_ =
            nh_->subscribe("/camera/image_raw", 1,
                           &YourClass::CameraImageCallback, this);

    }

    //  Callback from camera image topic subscriber.
    void SensorPerception::CameraImageCallback(
        const sensor_msgs::Image::ConstPtr &msg) {
        cv_bridge::CvImageConstPtr cv_ptr;  // CV bridge image.
        try {
            // Copy the Image msg into the CV bridge variable.
            cv_ptr = cv_bridge::toCvCopy(msg, sensor_msgs::image_encodings::BGR8);
        } catch (cv_bridge::Exception &e) {
            return;
        }
        cv::Mat CameraImg =
            cv_ptr->image;  // Obtain the openCV image from the CVbridge.

     //Do you image processing on opencv image CameraImg
    }

Note that in CMakeList.txt of your project you need to link the open_cv and cv_bridge libraries

Lets assume that you successfully launched ros package of your camera and your camera is publishing image under a topic called "/camera_image_raw". Then you can receive this image with cv_brdige and converting to a open_cv image in your callback as ;

#include <cv_bridge/cv_bridge.h>
#include <image_transport/image_transport.h>
#include <sensor_msgs/Image.h>


YouClass::YourClass(){
        // Camera image topic subscriber.
        camera_subscriber_ =
            nh_->subscribe("/camera/image_raw", 1,
                           &YourClass::CameraImageCallback, this);

    }

    //  Callback from camera image topic subscriber.
    void SensorPerception::CameraImageCallback(
YouClass::CameraImageCallback(
        const sensor_msgs::Image::ConstPtr &msg) {
        cv_bridge::CvImageConstPtr cv_ptr;  // CV bridge image.
        try {
            // Copy the Image msg into the CV bridge variable.
            cv_ptr = cv_bridge::toCvCopy(msg, sensor_msgs::image_encodings::BGR8);
        } catch (cv_bridge::Exception &e) {
            return;
        }
        cv::Mat CameraImg =
            cv_ptr->image;  // Obtain the openCV image from the CVbridge.

     //Do you image processing on opencv image CameraImg
    }

Note that in CMakeList.txt of your project you need to link the open_cv and cv_bridge libraries

Lets assume that you successfully launched ros package of your camera and your camera is publishing image under a topic called "/camera_image_raw""/camera/image_raw". Then you can receive this image with cv_brdige and converting to a open_cv image in your callback as ;

#include <cv_bridge/cv_bridge.h>
#include <image_transport/image_transport.h>
#include <sensor_msgs/Image.h>


YouClass::YourClass(){
        // Camera image topic subscriber.
        camera_subscriber_ =
            nh_->subscribe("/camera/image_raw", 1,
                           &YourClass::CameraImageCallback, this);

    }

    //  Callback from camera image topic subscriber.
    void YouClass::CameraImageCallback(
        const sensor_msgs::Image::ConstPtr &msg) {
        cv_bridge::CvImageConstPtr cv_ptr;  // CV bridge image.
        try {
            // Copy the Image msg into the CV bridge variable.
            cv_ptr = cv_bridge::toCvCopy(msg, sensor_msgs::image_encodings::BGR8);
        } catch (cv_bridge::Exception &e) {
            return;
        }
        cv::Mat CameraImg =
            cv_ptr->image;  // Obtain the openCV image from the CVbridge.

     //Do you image processing on opencv image CameraImg
    }

Note that in CMakeList.txt of your project you need to link the open_cv and cv_bridge libraries