@@ -106,7 +106,7 @@ Mat MedianSideWindowFilter(Mat src, int radius = 1) {
106
106
for (int x = -radius; x <= radius; x++) {
107
107
for (int y = -radius; y <= radius; y++) {
108
108
// if (x == 0 && y == 0) continue;
109
- if (filter[pos][id]) now.push_back (src.at <uchar>(i + x, j + y) * filter[pos][id]);
109
+ if (filter[pos][id]) now.push_back (src.at <uchar>(i + x, j + y) * filter[pos][id]);
110
110
id++;
111
111
// val += src.at<uchar>(i + x, j + y) * filter[k][id++];
112
112
}
@@ -186,15 +186,15 @@ static double angle(Point pt1, Point pt2, Point pt0)
186
186
}
187
187
188
188
// 寻找矩形
189
- static void findSquares (const Mat& image, vector<vector<Point > >& squares, int N= 5 , int thresh= 50 )
189
+ static void findSquares (const Mat& image, vector<vector<Point > >& squares, int N = 5 , int thresh = 50 )
190
190
{
191
191
192
192
// 滤波可以提升边缘检测的性能
193
193
Mat timg (image);
194
194
// 普通中值滤波
195
- medianBlur (image, timg, 9 );
195
+ // medianBlur(image, timg, 9);
196
196
// SideWindowFilter的中值滤波
197
- // timg = MedianSideWindowFilter(image, 4);
197
+ timg = MedianSideWindowFilter (image, 4 );
198
198
Mat gray0 (timg.size (), CV_8U), gray;
199
199
// 存储轮廓
200
200
vector<vector<Point > > contours;
@@ -213,8 +213,8 @@ static void findSquares(const Mat& image, vector<vector<Point> >& squares, int N
213
213
// 第一个输入矩阵的通道标记范围为:0 ~src[0].channels() - 1,第二个输入矩阵的通道标记范围为:src[0].channels() ~src[0].channels() + src[1].channels() - 1,
214
214
// 以此类推;其次输出矩阵也用同样的规则标记,第一个输出矩阵的通道标记范围为:0 ~dst[0].channels() - 1,第二个输入矩阵的通道标记范围为:dst[0].channels()
215
215
// ~dst[0].channels() + dst[1].channels() - 1, 以此类推;最后,数组fromTo的第一个元素即fromTo[0]应该填入输入矩阵的某个通道标记,而fromTo的第二个元素即
216
- // fromTo[1]应该填入输出矩阵的某个通道标记,这样函数就会把输入矩阵的fromTo[0]通道里面的数据复制给输出矩阵的fromTo[1]通道。fromTo后面的元素也是这个
217
- // 道理,总之就是一个输入矩阵的通道标记后面必须跟着个输出矩阵的通道标记.
216
+ // fromTo[1]应该填入输出矩阵的某个通道标记,这样函数就会把输入矩阵的fromTo[0]通道里面的数据复制给输出矩阵的fromTo[1]通道。fromTo后面的元素也是这个
217
+ // 道理,总之就是一个输入矩阵的通道标记后面必须跟着个输出矩阵的通道标记.
218
218
// npairs: 即参数fromTo中的有几组输入输出通道关系,其实就是参数fromTo的数组元素个数除以2.
219
219
mixChannels (&timg, 1 , &gray0, 1 , ch, 1 );
220
220
@@ -260,7 +260,7 @@ static void findSquares(const Mat& image, vector<vector<Point> >& squares, int N
260
260
// bool closed:表示输出的多边形是否封闭
261
261
262
262
// arcLength 计算图像轮廓的周长
263
- approxPolyDP (Mat (contours[i]), approx, arcLength ( Mat (contours[i]), true )* 0.02 , true );
263
+ approxPolyDP (Mat (contours[i]), approx, 20 , true );
264
264
265
265
// 近似后,方形轮廓应具有4个顶点
266
266
// 相对较大的区域(以滤除嘈杂的轮廓)并且是凸集。
@@ -301,17 +301,12 @@ void drawSquares(Mat &image, const vector<vector<Point> >& squares) {
301
301
}
302
302
303
303
int main () {
304
- Mat src = imread (" F:\\ 1.jpg" );
305
- for (int i = 0 ; i < 10 ; i++) {
306
- src = MedianSideWindowFilter (src, 3 );
307
- }
308
- imwrite (" F:\\ res.jpg" , src);
309
- /* Mat src = cv::imread("F:\\manyStickies.jpg");
304
+ Mat src = cv::imread (" F:\\ stone.jpg" );
310
305
vector<vector<Point > > squares;
311
306
findSquares (src, squares, 5 , 50 );
312
307
drawSquares (src, squares);
313
308
imshow (" result" , src);
314
309
imwrite (" F:\\ res2.jpg" , src);
315
- waitKey(0);*/
310
+ waitKey (0 );
316
311
return 0 ;
317
312
}
0 commit comments