-
Notifications
You must be signed in to change notification settings - Fork 0
/
capturer.cpp
132 lines (120 loc) · 3.12 KB
/
capturer.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#include <stdio.h>
#include <iostream>
#include <opencv/cv.h>
#include <opencv/highgui.h>
#include <ros/ros.h>
#include <sensor_msgs/fill_image.h>
#include <image_transport/image_transport.h>
class UsbCamNode
{
/**
* Class for ROS node
*/
public:
ros::NodeHandle &node_;
sensor_msgs::Image img_;
std::string video_device_name_;
std::string io_method_name_;
int image_width_, image_height_;
std::string pixel_format_name_;
ros::Time next_time_;
int count_;
IplImage *src;
CvCapture *capture;
image_transport::Publisher image_pub;
UsbCamNode(ros::NodeHandle &node) : node_(node)
{
src = NULL;
capture = NULL;
image_transport::ImageTransport it(node);
image_pub = it.advertise("image_raw", 1);
int camera_num = -1;
ros::NodeHandle nh("~");
nh.getParam("camera_index", camera_num);
printf("%d", camera_num);
// cvCaptureFromCAM(-1) uses any camera
if (NULL == (capture = cvCaptureFromCAM(camera_num)))
{
printf("\nError on cvCaptureFromCAM");
}
if (NULL == (src = cvQueryFrame(capture)))
{
printf("\nError on cvQueryFrame");
}
printf("Acquired image (%d/%d)\n", src->width, src->height);
printf("Channels (%d)\n", src->nChannels);
printf("Depth (%d)\n", src->depth);
//-----
#ifdef OUTPUT_ENABLED
cvNamedWindow("Capture", CV_WINDOW_AUTOSIZE);
cvMoveWindow("Capture", 50, 50);
#endif
next_time_ = ros::Time::now();
count_ = 0;
}
virtual ~UsbCamNode()
{
cvReleaseCapture(&capture);
#ifdef OUTPUT_ENABLED
cvDestroyWindow("Capture");
#endif
}
bool take_and_send_image()
{
int key;
if (NULL == (src = cvQueryFrame(capture)))
{
printf("\nError on cvQueryFrame");
return false;
}
else
{
#ifdef OUTPUT_ENABLED
cvShowImage("Capture", src);
key = cvWaitKey(10);
#endif
// fill image message
fillImage(
img_,
"bgr8",
src->height,
src->width,
src->nChannels * src->width,
src->imageData);
// publish
image_pub.publish(img_);
return true;
}
}
bool spin()
{
while (node_.ok())
{
if (take_and_send_image())
{
count_++;
ros::Time now_time = ros::Time::now();
if (now_time > next_time_)
{
printf("%d frames/sec at %s\n", count_, now_time);
count_ = 0;
next_time_ = next_time_ + ros::Duration(1, 0);
}
}
else
{
ROS_ERROR("couldn't take image.");
usleep(1000000);
}
}
return true;
}
};
int main(int argc, char **argv)
{
ros::init(argc, argv, "raspi_cam_ros");
ros::NodeHandle n;
UsbCamNode a(n);
a.spin();
return 0;
}