Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

an example of code that get image from camera then publish the image:

#include "camera.h"



CameraDriver::CameraDriver()
{

    ros::NodeHandle n("~");

    n.param( "camera_index", id_camera, -1);
    n.param( "show", show , false );
    n.param( "fps", fps , 30 );

    cvi.header.frame_id = "image";
    cvi.encoding = sensor_msgs::image_encodings::BGR8;



    it = new image_transport::ImageTransport(n);

    pub=it->advertise("/image_raw",1);


    if(id_camera == -1)
    {
          ROS_WARN("camera's id has not recived");
          ROS_WARN("I will open  every camera that I find :P ");
    }

    input_video.open(id_camera);
    input_video.set(CV_CAP_PROP_FPS,fps);

    if(!input_video.isOpened())
    {
        ROS_ERROR("Couldn't Open The Camera !");
        ROS_ERROR("Babay :(");
        ros::shutdown();
    }


    ros::Rate loop_rate(fps);

    while (ros::ok())
    {

        input_video.read(frame);
        cvi.image=frame;
        cvi.header.stamp = ros::Time::now();
        pub.publish(cvi.toImageMsg());

        if(show)
        {
            imshow("imgout.jpg", frame);
            if(waitKey(1) > 0);
        }

        loop_rate.sleep();

    }



}

an example of code that get image from camera then publish the image:

#include "camera.h"



CameraDriver::CameraDriver()
{

    ros::NodeHandle n("~");

    n.param( "camera_index", id_camera, -1);
    n.param( "show", show , false );
    n.param( "fps", fps , 30 );

    cvi.header.frame_id = "image";
    cvi.encoding = sensor_msgs::image_encodings::BGR8;



    it = new image_transport::ImageTransport(n);

    pub=it->advertise("/image_raw",1);


    if(id_camera == -1)
    {
          ROS_WARN("camera's id has not recived");
          ROS_WARN("I will open  every camera that I find :P ");
    }

    input_video.open(id_camera);
    input_video.set(CV_CAP_PROP_FPS,fps);

    if(!input_video.isOpened())
    {
        ROS_ERROR("Couldn't Open The Camera !");
        ROS_ERROR("Babay :(");
        ros::shutdown();
    }


    ros::Rate loop_rate(fps);

    while (ros::ok())
    {

        input_video.read(frame);
        cvi.image=frame;
        cvi.header.stamp = ros::Time::now();
        pub.publish(cvi.toImageMsg());

        if(show)
        {
            imshow("imgout.jpg", frame);
            if(waitKey(1) > 0);
        }

        loop_rate.sleep();

    }



}

and .h

#include <ros/ros.h>
#include <sensor_msgs/Image.h>
#include "opencv/cv.h"
#include "opencv/highgui.h"
#include "cv_bridge/cv_bridge.h"
#include <sensor_msgs/image_encodings.h>
#include <image_transport/image_transport.h>
#include <sensor_msgs/image_encodings.h>



using namespace std;
using namespace cv;

class CameraDriver
{
public:
    int id_camera;
    int fps;
    bool show;
    VideoCapture input_video;
    Mat frame;
    cv_bridge::CvImage cvi;
    image_transport::ImageTransport *it;
    image_transport::Publisher pub;
    explicit CameraDriver();

};