-
Notifications
You must be signed in to change notification settings - Fork 0
/
qcvdetectfilter.cpp
104 lines (88 loc) · 3.63 KB
/
qcvdetectfilter.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
#include "qcvdetectfilter.h"
cv::CascadeClassifier classifier;
QVideoFilterRunnable* QCvDetectFilter::createFilterRunnable()
{
return new QCvDetectFilterRunnable(this);
}
QVideoFrame QCvDetectFilterRunnable::run(QVideoFrame *input, const QVideoSurfaceFormat &surfaceFormat, RunFlags flags)
{
Q_UNUSED(flags);
// qDebug() << flags;
input->map(QAbstractVideoBuffer::ReadOnly);
if(surfaceFormat.handleType() == QAbstractVideoBuffer::NoHandle)
{
int input_w = input->width();
int input_h = input->height();
cv::Mat yuyv(input_h, input_w,CV_8UC2, input->bits());
cv::Mat mat (input_h, input_w,CV_8UC3);
cvtColor(yuyv, mat, CV_YUV2BGR_YUYV);
// cvtColor(tmp, mat, input->pixelFormat() == QVideoFrame::Format_YUV420P ? CV_YUV2BGR_YV12 : CV_YUV2BGR_NV12);
// cv::flip(mat, mat, 0);//ham xoay hinh anh
/*
* Resize in not mandatory but it can speed up things quite a lot!
*/
// QSize resized = image.size().scaled(320, 240, Qt::KeepAspectRatio);
// cv::resize(mat, mat, cv::Size(resized.width(), resized.height()));
///--------------------------------------------------------------------------------------------------------------
vector<decodedObject> decodedObjects;
decode(mat, decodedObjects);
// Loop over all decoded objects
for(int i = 0; i < decodedObjects.size(); i++)
{
vector<Point> points = decodedObjects[i].location;
vector<Point> hull;
// If the points do not form a quad, find convex hull
if(points.size() > 4)
convexHull(points, hull);
else
hull = points;
// Number of points in the convex hull
int n = hull.size();
if(n==4){
emit filter->objectDetected(float(hull[0].x) / float(mat.cols),
float(hull[0].y) / float(mat.rows),
tinhKhoangCach(hull[0],hull[2]) / float(mat.cols),
tinhKhoangCach(hull[0],hull[1])/ float(mat.rows));
}
}
///--------------------------------------------------------------------------------------------------------------
}
input->unmap();
return *input;
}
void QCvDetectFilterRunnable::decode(Mat &im, vector<decodedObject> &decodedObjects)
{
// Create zbar scanner
ImageScanner scanner;
// Configure scanner
scanner.set_config(ZBAR_NONE, ZBAR_CFG_ENABLE, 1);
// Convert image to grayscale
Mat imGray;
cvtColor(im, imGray,COLOR_BGR2GRAY);
// Wrap image data in a zbar image
Image image(im.cols, im.rows, "Y800", (uchar *)imGray.data, im.cols * im.rows);
// Scan the image for barcodes and QRCodes
int n = scanner.scan(image);
// Print results
for(Image::SymbolIterator symbol = image.symbol_begin(); symbol != image.symbol_end(); ++symbol)
{
decodedObject obj;
obj.type = symbol->get_type_name();
obj.data = symbol->get_data();
// Print type and data
cout << "Type : " << obj.type << endl;
cout << "Data : " << obj.data << endl << endl;
// Obtain location
for(int i = 0; i< symbol->get_location_size(); i++)
{
obj.location.push_back(Point(symbol->get_location_x(i),symbol->get_location_y(i)));
}
decodedObjects.push_back(obj);
}
}
double QCvDetectFilterRunnable::tinhKhoangCach(Point X, Point Y)
{
double kc;
kc = sqrt(pow((X.x - Y.x), 2) + pow((X.y - Y.y), 2));
return kc;
}