Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032 #include <cstdio>
00033 #include <ros/ros.h>
00034 #include <ros/time.h>
00035 #include "uvc_cam/uvc_cam.h"
00036 #include "sensor_msgs/Image.h"
00037 #include "sensor_msgs/image_encodings.h"
00038
00039 #include <image_transport/image_transport.h>
00040 #include <opencv/cvwimage.h>
00041 #include <opencv/highgui.h>
00042 #include <cv_bridge/CvBridge.h>
00043
00044 int main(int argc, char **argv)
00045 {
00046 ros::init(argc, argv, "uvc_cam");
00047 ros::NodeHandle n;
00048 ros::NodeHandle n_private("~");
00049
00050 std::string device;
00051 std::string out_topic;
00052 n_private.param<std::string>("device", device, "/dev/video0");
00053 n_private.param<std::string>("topic", out_topic, "/camera/image");
00054 int width, height, fps, modetect_lum, modetect_count;
00055 n_private.param("width", width, 640);
00056 n_private.param("height", height, 480);
00057 n_private.param("fps", fps, 30);
00058 n_private.param("motion_threshold_luminance", modetect_lum, 100);
00059 n_private.param("motion_threshold_count", modetect_count, -1);
00060
00061 image_transport::ImageTransport it(n);
00062 image_transport::Publisher pub = it.advertise(out_topic.c_str(), 1);
00063
00064
00065 ROS_INFO("opening uvc_cam at %dx%d, %d fps", width, height, fps);
00066 uvc_cam::Cam cam(device.c_str(), uvc_cam::Cam::MODE_RGB, width, height, fps);
00067 cam.set_motion_thresholds(modetect_lum, modetect_count);
00068 IplImage *imageIpl = cvCreateImageHeader(cvSize(640,480), 8, 3);
00069
00070 ros::Time t_prev(ros::Time::now());
00071 int count = 0, skip_count = 0;
00072 while (n.ok())
00073 {
00074 unsigned char *frame = NULL;
00075 uint32_t bytes_used;
00076 int buf_idx = cam.grab(&frame, bytes_used);
00077 if (count++ % fps == 0)
00078 {
00079 ros::Time t(ros::Time::now());
00080 ros::Duration d(t - t_prev);
00081 ROS_INFO("%.1f fps skip %d", (double)fps / d.toSec(), skip_count);
00082 t_prev = t;
00083 }
00084 if (frame)
00085 {
00086
00087
00088 imageIpl->imageData = (char *)frame;
00089 sensor_msgs::Image::Ptr image = sensor_msgs::CvBridge::cvToImgMsg( imageIpl, "bgr8");
00090
00091
00092
00093 image->header.stamp = ros::Time::now();
00094 image->encoding = sensor_msgs::image_encodings::RGB8;
00095 image->height = height;
00096 image->width = width;
00097 image->step = 3 * width;
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113 pub.publish(image);
00114 cam.release(buf_idx);
00115 }
00116 else
00117 skip_count++;
00118 }
00119 return 0;
00120 }
00121