-
Notifications
You must be signed in to change notification settings - Fork 1
/
kuangshi.cpp
68 lines (68 loc) · 1.91 KB
/
kuangshi.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
#include<opencv2/imgcodecs.hpp>
#include<opencv2/highgui.hpp>
#include<opencv2/imgproc.hpp>
#include<iostream>
using namespace std;
using namespace cv;
Mat img;
int main()
{
//VideoCapture cap(0);
//while (true) {
//cap.read(img);
string path = "C:\\Users\\PC\\Desktop\\5.jpg";
img = imread(path);
Mat imgGray, imgBlur, imgHSV, imgCanny, imgTwo, imgDil, imgErode;
resize(img, img, Size(500, 500));
cvtColor(img, imgHSV, COLOR_BGR2HSV);
inRange(imgHSV, Scalar(18, 50, 103), Scalar(60, 255, 255), imgTwo);
Mat kernel = getStructuringElement(MORPH_RECT, Size(9, 9));
erode(imgTwo, imgErode, kernel);
dilate(imgErode, imgDil, kernel);
vector<vector<Point>>contours;
vector<Vec4i>hierarchy;
findContours(imgDil, contours, hierarchy, RETR_LIST, CHAIN_APPROX_NONE);
double maxarea = 0;
int m, t;
for (int i = 0; i < contours.size(); i++) {
double area = contourArea(contours[i]);
cout << area << endl;
if (area > maxarea) {
maxarea = area;
m = i;
}
}
maxarea = contourArea(contours[0], true);
for (int i = 0; i < contours.size() && i != m; i++) {
double area = contourArea(contours[i]);
if (area > maxarea) {
maxarea = area;
t = i;
}
}
RotatedRect rect = minAreaRect(contours[t]);
Point2f rectPoints[4];
rect.points(rectPoints);
for (int j = 0; j < 4; j++) {
line(img, rectPoints[j], rectPoints[(j + 1) % 4], Scalar(0, 255, 0), 4, 8, 0);
}
double a = rect.size.height / rect.size.width;
cout << a << endl;
double area1 = rect.size.height * rect.size.width;
cout << area1 << endl;
if (area1 < 20000) {
cout << "空白" << endl;
}
else if (a <=0.8) {
cout << "R面" << endl;
}
else if (a > 0.8) {
cout << "条形码面" << endl;
}
imshow("img Two", imgTwo);
imshow("img Erode", imgErode);
imshow("img Dil", imgDil);
imshow("img", img);
waitKey(0);
return 0;
}