-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsync.cpp
More file actions
201 lines (159 loc) · 4.88 KB
/
Copy pathsync.cpp
File metadata and controls
201 lines (159 loc) · 4.88 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
#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, undistort_img;
vector<Point2f> save2Dpoint;
double x, y;
vector<Point2f> get2Dpoint();
Mat display_image(Mat image);
void param_init();
class cloud
{
ros::NodeHandle nh;
ros::Subscriber pcl_sub;
ros::Publisher pcl_pub;
int cloud_count = 0;
public:
cloud()
{
// Subscribe to input video feed and publish output video feed
pcl_sub = nh.subscribe("Sensor/points", 10, &cloud::cloudCB, this);
pcl_pub = nh.advertise<sensor_msgs::PointCloud2>("pcl_filtered", 1);
}
~cloud()
{
cv::destroyWindow(OPENCV_WINDOW);
}
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;
double x, y, z;
for (size_t i = 0; i < cloud.points.size(); ++i) {
x = cloud.points[i].x;
y = cloud.points[i].y;
z = cloud.points[i].z;
points2.push_back(cv::Point3f(x, y, z));
}
vector<Point2f> projectedPoints2;
projectPoints(points2, rvec, tvec, cameraMatrix, distCoeffs, projectedPoints2);
save2Dpoint = projectedPoints2;
cout << "cloud num: "<< cloud_count << endl;
cloud_count++;
pcl::toROSMsg(cloud_filtered, output);
output.header.frame_id = "point_cloud";
pcl_pub.publish(output);
}
};
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("/undistort_image", 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){
if(save2Dpoint.size()>1){
for (int i = 0; i < save2Dpoint.size(); i++) {
x = save2Dpoint[i].x;
y = save2Dpoint[i].y;
circle(cv_ptr->image, Point(x, y), 1, Scalar(0, 0, 255), -1);
//cout << "x: " << x << endl;
//cout << "y: " << y << endl;
}
imshow(OPENCV_WINDOW, cv_ptr->image);
waitKey(1);
cout << "image num: " << img_count << endl;
img_count++;
image_pub_.publish(cv_ptr->toImageMsg());
}
}
}
};
main(int argc, char** argv)
{
param_init();
ros::init(argc, argv, "pcl_and_opencv");
ROS_INFO("Started two Node");
fusion ic;
cloud jc;
ros::spin();
return 0;
}
void param_init() {
cameraMatrix.at<double>(0, 0) = 1173.861610; // fx
cameraMatrix.at<double>(0, 1) = 0;
cameraMatrix.at<double>(0, 2) = 665.778565; // cx
cameraMatrix.at<double>(1, 0) = 0;
cameraMatrix.at<double>(1, 1) = 1171.727336; // fy
cameraMatrix.at<double>(1, 2) = 352.757616; // cy
cameraMatrix.at<double>(2, 0) = 0;
cameraMatrix.at<double>(2, 1) = 0;
cameraMatrix.at<double>(2, 2) = 1;
distCoeffs.at<double>(0) = -0.281792;
distCoeffs.at<double>(1) = 0.115447;
distCoeffs.at<double>(2) = 0.000875;
distCoeffs.at<double>(3) = -0.001135;
rvec.at<double>(0) = 1.285994831387268;
rvec.at<double>(1) = -1.202461389136498;
rvec.at<double>(2) = 1.180007709399751;
tvec.at<double>(0) = -0.0003491853220207097;
tvec.at<double>(1) = 0.2041162796497789;
tvec.at<double>(2) = -0.03691034276389026;
}