-
Notifications
You must be signed in to change notification settings - Fork 0
/
BasicDataStructure.cpp
146 lines (116 loc) · 3.53 KB
/
BasicDataStructure.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#include <cmath>
#include "BasicDataStructure.h"
PointD operator-( const PointD& point1, const PointD& point2 )
{
PointD retPoint;
retPoint._x = point1._x - point2._x;
retPoint._y = point1._y - point2._y;
return retPoint;
}
static inline double pabs(const PointD& point)
{
return sqrt(point._x*point._x+point._y*point._y);
}
double minPointDistance( int& selectIndex, const PointD& point,std::vector<PointD> pointList )
{
double minDistance = 1000;
double r;
int num = pointList.size();
for ( int i = 0; i < num; ++i )
{
r = pabs( point - pointList[i] );
if ( r < minDistance )
{
selectIndex = i;
minDistance = r;
}
}
return minDistance;
}
int colorTable[30] =
{
100, 149, 23,//cornflowerblue
255,127, 80,//coral
127, 255, 0,//chartreuse
144, 43, 226,//blueviolet
85, 107, 47,//darkolivegreen
200, 255, 200,//honeydew
47, 79, 79,//darkslategray
220, 20, 60,//crimson
255, 146, 0,//darkorange
134, 134,134//gray
};
cv::Mat getLabelMap(QImage* labelImage)
{
if( !labelImage ) return cv::Mat();
cv::Mat mask;
mask.create(labelImage->height(), labelImage->width(), CV_8UC1);
mask.setTo(cv::GC_PR_BGD);
int R, G, B;
QRgb curRGB;
int num[2] = {0, 0};
for(int i = 0; i < labelImage->height(); i++)
{
for(int j = 0; j < labelImage->width(); j++)
{
curRGB = labelImage->pixel(j, i);
R = qRed(curRGB);
G = qGreen(curRGB);
B = qBlue(curRGB);
if(R == colorTable[3] && G == colorTable[4] && B == colorTable[5]) // foreground
{
mask.at<uchar>(i, j) = cv::GC_FGD;
num[0]++;
}
else if(R == colorTable[0] && G == colorTable[1] && B == colorTable[2]) // background
{
mask.at<uchar>(i, j) = cv::GC_BGD;
num[1]++;
}
}
}
if(num[0] < 50 || num[1] < 50) return cv::Mat();
return mask;
}
QImage* setMaskMap(IplImage* ocvImage, IplImage* mask)
{
int height = ocvImage->height;
int width = ocvImage->width;
QImage *retImage = new QImage( width, height, QImage::Format_RGB32 );
int R, G, B;
for(int y = 0; y < height; y++)
{
for(int x = 0; x < width; x++)
{
CvScalar color1 = cvGet2D(ocvImage, y, x);
CvScalar color2 = cvGet2D(mask, y, x);
if(ocvImage->nChannels == 3)
{
R = 0.6 * color1.val[2] + 0.4 * color2.val[2];
G = 0.6 * color1.val[1] + 0.4 * color2.val[1];
B = 0.6 * color1.val[0] + 0.4 * color2.val[0];
}
if(x > 0 && x < width-1 && y > 0 && y < height-1 && (color2.val[2] != 0 || color2.val[1] != 0 || color2.val[0] != 0))
{
CvScalar color3 = cvGet2D(mask, y, x-1);
CvScalar color4 = cvGet2D(mask, y, x+1);
CvScalar color5 = cvGet2D(mask, y-1, x);
CvScalar color6 = cvGet2D(mask, y+1, x);
if(isAllZero(color3) || isAllZero(color4) || isAllZero(color5) || isAllZero(color6))
{
R = 255;
G = 255;
B = 0;
}
}
retImage->setPixel(x, y, qRgb(R, G, B));
}
}
return retImage;
}
bool isAllZero(CvScalar color)
{
if(color.val[2] == 0 && color.val[1] == 0 && color.val[1] == 0)
return true;
else return false;
}