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
|
#include "BhBlocks.h"
#include "opencv2/core/core.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp"
using namespace cv;
using namespace std;
void BhBlocks::setColsRowsCount(int colCount, int rowCount)
{
this->colSize = -1;
this->rowSize = -1;
this->colCount = colCount;
this->rowCount = rowCount;
}
void BhBlocks::setColRowSize(float colSize, float rowSize)
{
this->colCount = -1;
this->rowCount = -1;
this->colSize = colSize;
this->rowCount = rowCount;
compute();
}
void BhBlocks::setRect(IplImage* srcImage)
{
rct = cvGetImageROI(srcImage);
compute();
}
void BhBlocks::setRect(const CvRect& srcRect)
{
rct = srcRect;
compute();
}
int BhBlocks::getColCount()
{
return colCount;
}
int BhBlocks::getRowCount()
{
return rowCount;
}
float BhBlocks::getColSize()
{
return colSize;
}
float BhBlocks::getRowSize()
{
return rowSize;
}
BhBlocks::BhBlocks(const IplImage* srcImage, float colSize, float rowSize, int colCount, int rowCount)
{
rct = cvGetImageROI(srcImage);
this->colCount = colCount;
this->rowCount = rowCount;
if (colSize == -1 && colSize == -1)
return;
if (rowSize == -1)
rowSize = colSize;
this->colSize = colSize;
this->rowSize = rowSize;
compute();
}
BhBlocks::BhBlocks(const Mat& srcMat, float colSize, float rowSize, int colCount, int rowCount)
{
rct.x = 0;
rct.y = 0;
rct.width = srcMat.cols;
rct.height = srcMat.rows;
this->colCount = colCount;
this->rowCount = rowCount;
if (colSize == -1 && colSize == -1 && colCount == -1 && rowCount == -1)
return;
if (rowSize == -1)
rowSize = colSize;
this->colSize = colSize;
this->rowSize = rowSize;
compute();
}
BhBlocks::BhBlocks(const CvRect& srcRect, float colSize, float rowSize, int colCount, int rowCount)
{
rct = srcRect;
this->colCount = colCount;
this->rowCount = rowCount;
if (colSize == -1 && colSize == -1)
return;
if (rowSize == -1)
rowSize = colSize;
this->colSize = colSize;
this->rowSize = rowSize;
compute();
}
void BhBlocks::compute()
{
float cSize;
float rSize;
if (colSize != -1 && rowSize != -1)
{
colCount = int(rct.width / colSize);
rowCount = int(rct.height / rowSize);
cSize = (float)rct.width / colCount;
rSize = (float)rct.height / rowCount;
}
else if (colCount != -1 && rowCount != -1)
{
colSize = (float)rct.width / colCount;
rowSize = (float)rct.height / rowCount;
cSize = colSize;
rSize = rowSize;
}
else return;
rects.clear();
rects.resize(rowCount);
for (int i = 0; i < rowCount; i++)
rects[i].resize(colCount);
float sumCol = 0;
for (int j = 0; j < colCount; j++)
{
float newSumCol = sumCol + cSize;
float curColSize = int(newSumCol - int(sumCol));
for (int i = 0; i < rowCount; i++)
{
rects[i][j].width = curColSize;
rects[i][j].x = sumCol;
}
sumCol = newSumCol;
}
float sumRow = 0;
for (int i = 0; i < rowCount; i++)
{
float newSumRow = sumRow + rSize;
float curRowSize = int(newSumRow - int(sumRow));
for (int j = 0; j < colCount; j++)
{
rects[i][j].height = curRowSize;
rects[i][j].y = sumRow;
}
sumRow = newSumRow;
}
}
void BhBlocks::drawBlocks(Mat srcImage, Scalar color, int thickness)
{
for (int i = 0; i < rowCount; i++)
for (int j = 0; j < colCount; j++)
rectangle(srcImage, rects[i][j], color);
}
vector<Rect> BhBlocks::getRects()
{
vector<Rect> result;
for (int i = 0; i < rowCount; i++)
for (int j = 0; j < colCount; j++)
result.push_back(rects[i][j]);
return result;
}
void BhBlocks::displayBlocks(char* title, IplImage* srcImage, CvScalar color, int thickness)
{
//IplImage* viewImg = cvCloneImage(srcImage);
//drawBlocks(viewImg, color, thickness);
//cvShowImage(title, viewImg);
//cvWaitKey(0);
//cvDestroyWindow(title);
//cvReleaseImage(&viewImg);
}
|