-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathImageRebuild.cpp
105 lines (97 loc) · 3.63 KB
/
ImageRebuild.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
//
// Created by 郭嘉丞 on 15/9/25.
//
#include "ImageRebuild.h"
#include "KinectParameters.h"
#include <iostream>
using std::cerr;
using std::endl;
using std::cout;
struct PixelNumber
{
int nx;
int ny;
};
const int lowResWidth = 512;
const int lowResHeight = 424;
const int highResWidth = 1920;
const int highResHeight = 1080;
static PixelNumber getPixelNumber(const pcl::PointXYZRGB &point, double projectionMat[3][4]);
cv::Mat get2DImageFromPointCloud(PointCloudPtr cloud)
{
cv::Mat rebuiltImage(lowResHeight, lowResWidth, CV_8UC3);
for (int i = 0; i < rebuiltImage.rows; i++)
{
for (int j = 0; j < rebuiltImage.cols; j++)
{
rebuiltImage.at<cv::Vec3b>(i, j)[0] = 0;
rebuiltImage.at<cv::Vec3b>(i, j)[1] = 0;
rebuiltImage.at<cv::Vec3b>(i, j)[2] = 0;
}
}
int minPixelX = INT32_MAX, maxPixelX = -1;
int minPixelY = INT32_MAX, maxPixelY = -1;
for (unsigned i=0; i<cloud->points.size(); i++)
{
const pcl::PointXYZRGB & point = cloud->points[i];
PixelNumber pxNumber = getPixelNumber(point, projectionParameter);
if(pxNumber.nx >= lowResWidth || pxNumber.nx < 0)
{
// cerr << pxNumber.nx << " " << pxNumber.ny << endl;
continue;
}
if(pxNumber.ny >= lowResHeight || pxNumber.nx < 0)
{
// cerr << pxNumber.nx << " " << pxNumber.ny << endl;
continue;
}
cv::Vec3b &pixel = rebuiltImage.at<cv::Vec3b>(pxNumber.ny, pxNumber.nx);
pixel[0] = point.b;
pixel[1] = point.g;
pixel[2] = point.r;
if (pxNumber.nx > maxPixelX) maxPixelX = pxNumber.nx;
if (pxNumber.nx < minPixelX) minPixelX = pxNumber.nx;
if (pxNumber.ny > maxPixelY) maxPixelY = pxNumber.ny;
if (pxNumber.ny < minPixelY) minPixelY = pxNumber.ny;
}
if(maxPixelX < 0 || minPixelX < 0) return cv::Mat();
return cv::Mat(rebuiltImage,
cv::Range(minPixelY, maxPixelY+1),
cv::Range(minPixelX, maxPixelX+1));
}
cv::Mat getHDImageFromPointCloud(PointCloudPtr cloud, cv::Mat &totalImage)
{
int minPixelX = INT32_MAX, maxPixelX = -1;
int minPixelY = INT32_MAX, maxPixelY = -1;
for (unsigned i=0; i<cloud->points.size(); i++)
{
const pcl::PointXYZRGB & point = cloud->points[i];
PixelNumber pxNumber = getPixelNumber(point, projectionParameter1080);
if (pxNumber.nx > maxPixelX) maxPixelX = pxNumber.nx;
if (pxNumber.nx < minPixelX) minPixelX = pxNumber.nx;
if (pxNumber.ny > maxPixelY) maxPixelY = pxNumber.ny;
if (pxNumber.ny < minPixelY) minPixelY = pxNumber.ny;
}
minPixelX = minPixelX < 0 ? 0 : minPixelX;
maxPixelX = maxPixelX >= totalImage.cols ? totalImage.cols - 1 : maxPixelX;
minPixelY = minPixelY < 0 ? 0 : minPixelY;
maxPixelY = maxPixelY >= totalImage.cols ? totalImage.cols - 1 : maxPixelY;
return cv::Mat(totalImage,
cv::Range(minPixelY, maxPixelY+1),
cv::Range(minPixelX, maxPixelX+1));
}
PixelNumber getPixelNumber(const pcl::PointXYZRGB &point, double projectionMat[3][4])
{
double projectedPoint[3]; //2d point in the homogeneous coordinate
for(int i=0; i<3; i++)
{
projectedPoint[i] = projectionMat[i][0] * point.x +
projectionMat[i][1] * point.y +
projectionMat[i][2] * point.z +
projectionMat[i][3];
}
PixelNumber pixelNumber;
pixelNumber.nx = (int)(projectedPoint[0] / projectedPoint[2]);
pixelNumber.ny = (int)(projectedPoint[1] / projectedPoint[2]);
return pixelNumber;
}