-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
79 lines (49 loc) · 1.57 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
#include <opencv2/opencv.hpp>
#include <iostream>
using namespace std;
using namespace cv;
int main()
{
Mat img = imread("/home/liqianqi/SVM/5828124.jpg");
if(img.empty())
{
cout << "the image is empty !" << endl;
return -1;
}
// waitKey(0);
Mat gray;
Mat gray1;
Mat vector_mat[3];
cv::split(img,vector_mat);
gray1 = vector_mat[0] - 1*vector_mat[2];
cvtColor(img,gray,COLOR_BGR2GRAY);
threshold(gray,gray,130,255,THRESH_BINARY_INV);
threshold(gray1,gray1,127,255,THRESH_BINARY);
gray = gray & gray1;
// Mat element = getStructuringElement(cv::MORPH_RECT, cv::Size(7, 7));
// dilate(gray,gray,element);
vector<std::vector<cv::Point>> contours;
vector<cv::Vec4i> hierarchy;
findContours(gray,contours,hierarchy,RETR_EXTERNAL,CV_CHAIN_APPROX_NONE);
cout << contours.size() << endl;
drawContours(img,contours,-1,Scalar(0,0,255),1);
RotatedRect rect;
for(unsigned long i = 0; i < contours.size();i++)
{
double area = contourArea(contours[i]);
if(area < 200)
{
continue ;
}
cout << area << endl;
rect = minAreaRect(contours[i]);
float width = rect.size.width;
float height = rect.size.height;
Point2f left_up_conor = Point2f(rect.center.x - height/2,rect.center.y - width/2);
rectangle(img,left_up_conor,Point(left_up_conor.x + height,left_up_conor.y+width),Scalar(0,255,0));
}
imshow("gray",gray);
imshow("color",img);
waitKey(0);
//cv::VideoWriter writer;
}