This repository has been archived by the owner on Dec 9, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtape-detector-live.cpp
68 lines (56 loc) · 2.11 KB
/
tape-detector-live.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
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include "ShapeMatcher.h"
#include "utils.h"
#include "Contour.h"
using namespace cv;
using namespace std;
/** @function main */
int main( int argc, char** argv )
{
VideoCapture cap(0); // open the default camera
if(!cap.isOpened()) // check if we succeeded
return -1;
namedWindow("TapeFinder",1);
Mat frame;
BackwardsLMatcher blm(NULL);
LMatcher lm(NULL);
bool isMatch;
for(;;)
{
cap >> frame; // get a new frame from camera
vector<Contour> contours = detectContoursColorFiltered(&frame, &HSV_YELLOW,6, 25);
Contour matchingContour;
Rect matchingBoundRect;
blm.findMatch(&contours, &frame, &matchingContour, &matchingBoundRect);
isMatch = lm.findMatch(&contours, &frame, &matchingContour, &matchingBoundRect);
// Mask all except yellow :)
Mat hsvImg, maskImg, masked;
cvtColor(frame, hsvImg, CV_BGR2HSV);
Scalar hsvMin = Scalar(20, 100, 100);
Scalar hsvMax = Scalar(30, 255, 255);
inRange(hsvImg, hsvMin, hsvMax, maskImg);
frame.copyTo(frame, maskImg);
// Estimate distance
if(isMatch) {
float dist = 4740.0 / matchingBoundRect.width;
cout << "Distance is " << dist << endl;
// Estimate dist from center
float toteCenterX = matchingBoundRect.x + matchingBoundRect.width/2;
float frameCenter = frame.rows/2;
float toteDeltaPix = frameCenter - toteCenterX;
float pixPer7Inches = matchingBoundRect.width; // pixels in 7 inches
float toteDeltaInches = (toteDeltaPix * pixPer7Inches) / 7;
cout << "toteDeltaPix " << toteDeltaPix << " toteDeltaInches = " << toteDeltaInches << endl;
}
imshow("Src", frame);
imshow("Masked", maskImg);
if(waitKey(30) >= 0) break;
//usleep(100000);
}
return 0;
}