-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcolour_detection.cpp
91 lines (77 loc) · 2.34 KB
/
colour_detection.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
#include "colour_detection.h"
#include "utility.h"
#include <iostream>
#include <opencv2/opencv.hpp>
#include <string>
using cv::imread;
using cv::Mat;
using cv::Scalar;
using cv::createTrackbar;
using std::cout;
using std::endl;
using std::string;
using utility::hsvVals;
using utility::MAX_HSV;
using utility::MIN_HSV;
namespace colour_detection {
Mat ColourDetector::convertImageHSV(const Mat &img) {
cv::cvtColor(img, imgHSV, cv::COLOR_BGR2HSV);
return imgHSV;
}
const Mat &ColourDetector::getImgHSV() {
return imgHSV;
}
Mat ColourDetector::findColour(const Mat &img, const hsvVals min, const hsvVals max) {
Scalar lower(min.hue, min.sat, min.val);
Scalar upper(max.hue, max.sat, max.val);
inRange(img, lower, upper, mask);
return mask;
}
void ColourDetector::imageColourPicker(const Mat &img) {
Mat imgHSV = convertImageHSV(img), mask;
imshow("Image", img);
imshow("Image HSV", imgHSV);
hsvVals hsv_min = utility::MIN_HSV;
hsvVals hsv_max = utility::MAX_HSV;
utility::createHSVTrackbars(hsv_min, hsv_max);
while (1) {
mask = findColour(imgHSV, hsv_min, hsv_max);
cout << hsv_min.hue << ' ' << hsv_min.sat << ' ' << hsv_min.val << endl;
cout << hsv_max.hue << ' ' << hsv_max.sat << ' ' << hsv_max.val << endl;
cout << endl;
imshow("Image Colours", mask);
cv::waitKey(1);
}
}
void ColourDetector::videoColourPicker() {
cv::VideoCapture cap(0);
Mat img, imgHSV, mask;
if (cap.isOpened() == false) {
std::cout << "cannot open webcam\n";
exit(1);
}
hsvVals hsv_min = MIN_HSV;
hsvVals hsv_max = MAX_HSV;
utility::createHSVTrackbars(hsv_min, hsv_max);
while (true) {
cap.read(img);
cout << hsv_min.hue << ' ' << hsv_min.sat << ' ' << hsv_min.val << endl;
cout << hsv_max.hue << ' ' << hsv_max.sat << ' ' << hsv_max.val << endl;
cout << endl;
cv::cvtColor(img, imgHSV, cv::COLOR_BGR2HSV);
mask = findColour(imgHSV, hsv_min, hsv_max);
cv::imshow("Image", img);
cv::imshow("Image Colours", mask);
cv::waitKey(1);
}
}
void ColourDetector::imageColourDetection(string path) {
Mat img = cv::imread(path);
Mat imgHSV = convertImageHSV(img);
Mat mask = findColour(imgHSV, {129, 54, 0}, {179, 255, 255}); // Tesla colours
cv::imshow("Image", img);
cv::imshow("Image HSV", imgHSV);
cv::imshow("Image Colours", mask);
cv::waitKey(0);
}
} // namespace colour_detection