-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathREAL_TIME
More file actions
211 lines (155 loc) · 5.06 KB
/
Copy pathREAL_TIME
File metadata and controls
211 lines (155 loc) · 5.06 KB
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
#include <ros/ros.h>
#include <pcl/point_cloud.h>
#include <pcl_conversions/pcl_conversions.h>
#include <sensor_msgs/PointCloud2.h>
#include <pcl/filters/voxel_grid.h>
#include <image_transport/image_transport.h>
#include <cv_bridge/cv_bridge.h>
#include <sensor_msgs/image_encodings.h>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>
#include "opencv2/calib3d/calib3d.hpp"
#include "opencv2/imgproc/imgproc.hpp"
using namespace std;
using namespace cv;
static const string OPENCV_WINDOW = "Raw Image window";
static const string OPENCV_WINDOW_1 = "Edge Detection";
Mat cameraMatrix(3, 3, DataType<double>::type);
Mat distCoeffs(4, 1, DataType<double>::type);
Mat rvec(3, 1, DataType<double>::type);
Mat tvec(3, 1, DataType<double>::type);
Mat image, drawImg;
vector<Point2f> save2Dpoint;
vector<Point2f> get2Dpoint();
Mat display_image(Mat image);
void param_init();
class fusion
{
ros::NodeHandle nh_;
image_transport::ImageTransport it_;
image_transport::Subscriber image_sub_;
image_transport::Publisher image_pub_;
int img_count = 0;
int loop_count = 0;
int cloud_count = 0;
ros::NodeHandle nh;
ros::Subscriber pcl_sub;
ros::Publisher pcl_pub;
public:
fusion()
: it_(nh_)
{
// Subscribe to input video feed and publish output video feed
pcl_sub = nh.subscribe("Sensor/points", 10, &fusion::cloudCB, this);
pcl_pub = nh.advertise<sensor_msgs::PointCloud2>("pcl_filtered", 1);
image_sub_ = it_.subscribe("/image_raw", 1, &fusion::imageCb, this);
image_pub_ = it_.advertise("/edge_detector/raw_image", 1);
cv::namedWindow(OPENCV_WINDOW);
// cout << "loop_count: " << loop_count << endl;
// loop_count++;
}
~fusion()
{
cv::destroyWindow(OPENCV_WINDOW);
}
void imageCb(const sensor_msgs::ImageConstPtr& msg)
{
cv_bridge::CvImagePtr cv_ptr;
namespace enc = sensor_msgs::image_encodings;
try
{
cv_ptr = cv_bridge::toCvCopy(msg, sensor_msgs::image_encodings::BGR8);
}
catch (cv_bridge::Exception& e)
{
ROS_ERROR("cv_bridge exception: %s", e.what());
return;
}
// Draw an example circle on the video stream
if (cv_ptr->image.rows > 400 && cv_ptr->image.cols > 600){
mapping(cv_ptr->image);
image_pub_.publish(cv_ptr->toImageMsg());
}
}
void mapping(Mat img)
{
//start code
int x, y;
//cout << "save2Dpoint size: "<<save2Dpoint.size() << endl;
for (int i = 0; i < save2Dpoint.size(); i++) {
x = save2Dpoint[i].x;
y = save2Dpoint[i].y;
circle(img, Point(x, y), 1, Scalar(0, 0, 255), -1);
}
//circle(img, Point(100, 200), 5, Scalar(0, 255, 0), 2);
imshow(OPENCV_WINDOW, img);
waitKey(95);
cout << "image num: " << img_count << endl;
img_count++;
}
void cloudCB(const sensor_msgs::PointCloud2& input)
{
pcl::PointCloud<pcl::PointXYZ> cloud;
pcl::PointCloud<pcl::PointXYZ> cloud_filtered;
sensor_msgs::PointCloud2 output;
pcl::fromROSMsg(input, cloud);
std::vector<cv::Point3f> points2;
int x, y, z;
for (size_t i = 0; i < cloud.points.size(); ++i) {
x = cloud.points[i].x * 100;
y = cloud.points[i].y * 100;
z = cloud.points[i].z * 100;
points2.push_back(cv::Point3f(x, y, z));
}
// cout << "************************************" << endl;
// cout << points2 << endl;
//vector<Point3f> objectPoints2 = points2;
vector<Point2f> projectedPoints2;
// projectPoints(objectPoints2, rvec, tvec, cameraMatrix, distCoeffs, projectedPoints2);
projectPoints(points2, rvec, tvec, cameraMatrix, distCoeffs, projectedPoints2);
save2Dpoint = projectedPoints2;
// cout << save2Dpoint << endl;
//cout << save2Dpoint[10].x << " " << save2Dpoint[10].y << endl;
//mapping(cv_ptr->image);
cout << "cloud num: "<<cloud_count << endl;
cloud_count++;
// return points2;
// pcl::VoxelGrid<pcl::PointXYZ> vox_obj;
// vox_obj.setInputCloud (cloud.makeShared());
// vox_obj.setLeafSize (0.1f, 0.1f, 0.1f);
// vox_obj.filter(cloud_filtered);
pcl::toROSMsg(cloud_filtered, output);
output.header.frame_id = "point_cloud";
pcl_pub.publish(output);
}
};
main(int argc, char** argv)
{
param_init();
ros::init(argc, argv, "pcl_and_opencv");
ROS_INFO("Started two Node");
fusion ic;
ros::spin();
return 0;
}
void param_init() {
cameraMatrix.at<double>(0, 0) = 1250.978; // fx
cameraMatrix.at<double>(0, 1) = 0;
cameraMatrix.at<double>(0, 2) = 626.673; // cx
cameraMatrix.at<double>(1, 0) = 0;
cameraMatrix.at<double>(1, 1) = 1257.900; // fy
cameraMatrix.at<double>(1, 2) = 354.239; // cy
cameraMatrix.at<double>(2, 0) = 0;
cameraMatrix.at<double>(2, 1) = 0;
cameraMatrix.at<double>(2, 2) = 1;
distCoeffs.at<double>(0) = -0.318184;
distCoeffs.at<double>(1) = 1.125858;
distCoeffs.at<double>(2) = 0.004153;
distCoeffs.at<double>(3) = -0.005453;
rvec.at<double>(0) = 1.300390386513138;
rvec.at<double>(1) = -1.095557374421685;
rvec.at<double>(2) = 1.171722002793529;
tvec.at<double>(0) = -17.74415636162148;
tvec.at<double>(1) = -0.9163904987566065;
tvec.at<double>(2) = -5.427465320880245;
}