-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
136 lines (114 loc) · 3.91 KB
/
main.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
#include "canny.h"
int main()
{
uint16 i, j;
int flag=0;
string area, time;
vector<Feather> featherList; // 存放连通域特征
vector<Point> pointss;
Mat Image = imread("D:/detection/hough14.bmp");
Mat dstImg = Image.clone(); //原图备份
Mat cdst;
int height = Image.rows;
int width = Image.cols;
Mat gray;
cvtColor(Image, gray, CV_BGR2GRAY); //转灰度图
Mat grayimg=gray.clone();
Mat proImg; //= Image.clone();
int64 t0 = getTickCount();
//medianBlur(Image, proImg, 3);
fastMedianBlur(gray, proImg, 3);
Canny(proImg, proImg, 6, 18);
cvtColor(proImg, cdst, CV_GRAY2BGR);
uchar **ptr = (uchar **)malloc(height * sizeof(uchar *)); //二维数组ptr[][]
for (i = 0; i < height; i++)
ptr[i] = (uchar *)malloc(width * sizeof(uchar *));
for (i = 0; i < height; i++)
{
for (j = 0; j < width; j++)
{
ptr[i][j] = proImg.at<uchar>(i, j); //img的矩阵数据传给二维数组ptr[][]
}
}
//float t00 = getTickCount();
vector<struct line> lines = houghLine(ptr, height, width, 90);//90
vector<struct line> sorted_lines= SortLines(lines);
vector<struct line> correct_lines = Filter(sorted_lines);
proImg = drawLines(ptr, cdst, proImg, correct_lines,flag);
if (flag == 1)
{
Mat mage = imread("D:/detection/error.bmp");
imshow("result", mage);
waitKey(1000000);
}
else
{
//float t11 = getTickCount();
//float timee = (t11 - t00) / cv::getTickFrequency();
//printf("%2f", timee);
//system("pause");
Mat element = getStructuringElement(MORPH_RECT, Size(3, 3));
//Dilate(proImg, Image, element);
fastDilate(proImg, Image); //膨胀操作
bwLabel(Image, proImg, grayimg, featherList);//标记连通区域 计算面积
vector<vector<Point>> contours;
for (vector<Feather>::iterator it = featherList.begin(); it < featherList.end(); it++)
{
if (it->flag == 0)
{
area = to_string(it->area);
rectangle(dstImg, it->boundingbox, Scalar(0, 255, 0), 2, 2);
putText(dstImg, area, it->boundingbox.br(), FONT_HERSHEY_SIMPLEX, 0.4, Scalar(0, 0, 255), 1, 2);
}
else
{
area = to_string(it->area);
rectangle(dstImg, it->boundingbox, Scalar(255, 0, 0), 2, 2);
putText(dstImg, area, it->boundingbox.br(), FONT_HERSHEY_SIMPLEX, 0.4, Scalar(0, 0, 255), 1, 2);
}//tl
/* area = to_string(it->area);
rectangle(dstImg, it->boundingbox, Scalar(0, 255, 0), 2, 2);
putText(dstImg, area, it->boundingbox.br(), FONT_HERSHEY_SIMPLEX, 0.4, Scalar(0, 0, 255), 1, 2);*/
}
int64 t1 = getTickCount();
time = to_string((t1 - t0) / getTickFrequency());
putText(dstImg, time, Point(10, 10), FONT_HERSHEY_SIMPLEX, 0.4, Scalar(255, 0, 0), 1, 2);
imshow("Image", Image);
imshow("dstImg", dstImg);
waitKey(1000000);
destroyAllWindows();
imwrite("F:/detect/canny15.bmp", proImg);
return 0;
}
//Mat Image, cdst, edge;
//int i, j;
//Image = imread("F:/detect/canny10.bmp", 0);
//int height = Image.rows;
//int width = Image.cols;
//uchar **ptr = (uchar **)malloc(height * sizeof(uchar *)); //二维数组ptr[][]
//for (i = 0; i < height; i++)
// ptr[i] = (uchar *)malloc(width * sizeof(uchar *));
//for (i = 0; i < height; i++)
//{
// for (j = 0; j < width; j++)
// {
// ptr[i][j] = Image.at<uchar>(i, j); //img的矩阵数据传给二维数组ptr[][]
// }
//}
//cvtColor(Image, cdst, CV_GRAY2BGR);
//float t0 = getTickCount();
//vector<struct line> lines = houghLine(ptr, height, width, 250);
//float t1 = getTickCount();
//float time = (t1 - t0) / cv::getTickFrequency();
//printf("%2f", time);
//Image = drawLines(ptr, cdst, Image, lines);
//float t2 = getTickCount();
//float time0 = (t2 - t1) / cv::getTickFrequency();
//imwrite("F:/detect/hough16.bmp", Image);
//printf("\n");
//printf("%2f", time0);
//namedWindow("result", 1);
//imshow("result", Image);
//waitKey();
//return 0;
}