-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdebug_generateTriangles.cpp
More file actions
137 lines (114 loc) · 5.08 KB
/
debug_generateTriangles.cpp
File metadata and controls
137 lines (114 loc) · 5.08 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
//
// Created by ale on 19-3-11.
//
//
// Created by ale on 19-3-5.
//
#include <vector>
#include <opencv2/opencv.hpp>
using namespace cv;
using namespace std;
extern int floodFillWithoutHoles(const Mat& gray, Mat& mask, const Point& seed, const double lowDiff, const double highDiff, const bool isStrict = false);
extern int antiClockContour(const Mat& mask, const Point& seed, vector<Point>& contour);
extern int clockWiseContour(const Mat& mask, const Point& seed, vector<Point>& contour);
extern int triangulateSimplePolygon(const vector<Point>& vertices, vector<Point>& triangles);
//全局变量声明部分
Mat g_srcImage, g_dstImage, g_grayImage, g_maskImage, g_depthImage;//定义原始图、目标图、灰度图、掩膜图
int g_nLowDifference = 20, g_nUpDifference = 20;//负差最大值,正差最大值
//===============【onMouse()函数】=======================
static void onMouse(int event, int x, int y, int, void *) {
//若鼠标左键没有按下,便返回
if (event != EVENT_LBUTTONDOWN)
return;
Mat mask_draw = g_maskImage.clone();
Mat color_draw = g_srcImage.clone();
Mat color_draw_triangle = g_srcImage.clone();
Point seed = Point(x, y);
double minVal, maxVal;
Point minLoc, maxLoc;
minMaxLoc(g_depthImage, &minVal, &maxVal, &minLoc, &maxLoc);
if(maxVal > 0)
seed = maxLoc;
else
return;
double diff = 0.05*g_depthImage.at<float>(seed.y, seed.x);
int temp = floodFillWithoutHoles(g_depthImage, mask_draw, seed, diff*20.0/double(g_nLowDifference), diff*20.0/double(g_nUpDifference));
cout<<"temp: "<<temp<<endl;
imshow("mask_draw", mask_draw);
//g_depthImage.setTo(0, mask_draw);
//g_srcImage.setTo(Scalar(0,0,0), mask_draw);
for(int i=0; i<mask_draw.rows; ++i)
for(int j=0; j<mask_draw.cols; ++j)
if(mask_draw.at<uchar>(i,j) > 0)
{
g_depthImage.at<float>(i,j) = 0;
g_srcImage.at<Vec3b>(i,j) = Vec3b(0,0,0);
}
vector<Point> contour;
//antiClockContour(mask_draw, seed, contour);
//reverse(contour.begin(), contour.end());
clockWiseContour(mask_draw, seed, contour);
vector<Point> contour_approx;
cv::approxPolyDP(contour, contour_approx, 3, true);
for(size_t i=0;i< contour_approx.size()-1;i++)
{
//cout << pointListOut[i].x << "," << pointListOut[i].y << endl;
cv::line(color_draw, contour_approx[i], contour_approx[i+1], cv::Scalar(0,0,255), 1, CV_AA);
cout<<contour_approx[i]<<","<< contour_approx[i+1]<<"\t";
}
cout<<endl<<endl;
cout<<"seed points: "<<seed<<endl;
//cout<<"point n-4, n-3, n-2, n-1, 0, 1, 2, 3: "
//<<contour[contour.size()-4]-seed<<", "<<contour[contour.size()-3]-seed<<", "
//<<contour[contour.size()-2]-seed<<", "<<contour[contour.size()-1]-seed<<", "
//<<contour[0]-seed<<", "<<contour[1]-seed<<", "<<contour[2]-seed<<", "<<contour[3]-seed<<endl<<endl;
imshow("color_draw", color_draw);
vector<Point> triangles;
triangulateSimplePolygon(contour_approx, triangles);
for(int i=0; i<triangles.size()/3; ++i)
{
cv::line(color_draw_triangle, triangles[i*3+0], triangles[i*3+1], cv::Scalar(0,0,255), 1, CV_AA);
cv::line(color_draw_triangle, triangles[i*3+1], triangles[i*3+2], cv::Scalar(0,0,255), 1, CV_AA);
cv::line(color_draw_triangle, triangles[i*3+2], triangles[i*3+0], cv::Scalar(0,0,255), 1, CV_AA);
cv::circle(color_draw_triangle, triangles[i*3+0], 2, Scalar(0, 0, 0), -1, FILLED);
cv::circle(color_draw_triangle, triangles[i*3+1], 2, Scalar(0, 0, 0), -1, FILLED);
cv::circle(color_draw_triangle, triangles[i*3+2], 2, Scalar(0, 0, 0), -1, FILLED);
}
cout<<"sum(mask): "<<sum(mask_draw)/255<<"\t contour sizes:"<<contour.size()<<"\t triangle sizes: "<<triangles.size()/3<<endl;
imshow("color_draw_triangle", color_draw_triangle);
}
//main()函数
int main(int argc, char** argv) {
//载入原图
g_srcImage = imread("/home/ale/CLionProjects/i3d/data/Images/1000.jpg", 1);
cv::Mat depth = imread("/home/ale/CLionProjects/i3d/data/Depths/1000.png", CV_LOAD_IMAGE_ANYCOLOR|CV_LOAD_IMAGE_ANYDEPTH);
if (!g_srcImage.data || !depth.data)
{
printf("读取g_srcImage错误!\n");
return false;
}
g_srcImage.copyTo(g_dstImage);//复制原图到目标图
depth.convertTo(g_depthImage, CV_32FC1);
g_maskImage.create(g_srcImage.rows + 2, g_srcImage.cols + 2, CV_8UC1);//用原图尺寸初始化掩膜mask
namedWindow("效果图", WINDOW_AUTOSIZE);
//创建Trackbar
createTrackbar("负差最大值", "效果图", &g_nLowDifference, 255, 0);
createTrackbar("正差最大值", "效果图", &g_nUpDifference, 255, 0);
//鼠标回调函数
setMouseCallback("效果图", onMouse, 0);
//循环轮询按键
while (1)
{
//先显示效果图
imshow("效果图", g_dstImage);
//获取按键键盘
int c = waitKey(0);
//判断ESC是否按下,按下退出
if (c == 27)
{
cout << "程序退出........、\n";
break;
}
}
return 0;
}