-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathallcolour.cpp
More file actions
executable file
·95 lines (76 loc) · 2.71 KB
/
allcolour.cpp
File metadata and controls
executable file
·95 lines (76 loc) · 2.71 KB
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
//preprocessor
#include <iostream>
#include <stdio.h>
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
//namespace
using namespace cv;
using namespace std;
//matrix
Mat imgOriginal, imgHSV, imgThresholded, imgErode, imgDilate;
//hue
int LowH = 170;
int HighH = 179;
//saturation
int LowS = 150;
int HighS = 255;
//value
int LowV = 60;
int HighV = 255;
//posisi awal dan akhir
int a = 0;
int b = 0;
//minimal pixel dan box
int minPix = 10;
int minBox = 100;
double posisiAkhir = 320;//posisi akhir
int mx,my;//nilai koordinat
int mflags=0;//0
int main( int argc, char** argv )
{
VideoCapture cap(0);//webcam
//VideoCapture cap("http://192.168.43.74:4747/video?x.mjpeg");//webcam
//cap.set(CAP_PROP_FOURCC, VideoWriter_fourcc('M', 'J', 'P', 'G'))
//cap.set(CV_CAP_PROP_FOURCC, CV_FOURCC('M','J','P','G') );
if ( !cap.isOpened() ) // jika tidak terbaca, exit
{
cout << "webcam tidak terbaca" << endl;
return -1;
}
cap.read(imgOriginal);//capture di imgOriginal
namedWindow("ColourTracking", CV_WINDOW_AUTOSIZE);//nama window
//trackbar
createTrackbar("LowH", "ColourTracking", &LowH, 255); //Hue (0 - 255)
createTrackbar("HighH", "ColourTracking", &HighH, 255);
createTrackbar("LowS", "ColourTracking", &LowS, 255); //Saturation (0 - 255)
createTrackbar("HighS", "ColourTracking", &HighS, 255);
createTrackbar("LowV", "ColourTracking", &LowV, 255);//Value (0 - 255)
createTrackbar("HighV", "ColourTracking", &HighV, 255);
printf("Hue values of basic colors\n Orange 0-22\n Yellow 22- 38\n Green 38-75\n Blue 75-130\n Violet 130-160\n Red 160-179\n");
while (true)//loop sampai exit
{
bool bSuccess = cap.read(imgOriginal); // membaca frame baru dari video
if (!bSuccess) //jika tidak sukses, menghentikan loop
{
cout << "tidak bisa membaca frame dari video" << endl;
break;
}
cvtColor(imgOriginal, imgHSV, COLOR_BGR2HSV); //mengubah frame BGR jadi HSV
inRange(imgHSV, Scalar(LowH, LowS, LowV), Scalar(HighH, HighS, HighV), imgThresholded);//range threshold
//hapus objek kecil
erode(imgThresholded, imgThresholded, getStructuringElement(MORPH_ELLIPSE, Size(5, 5)) );
dilate( imgThresholded, imgThresholded, getStructuringElement(MORPH_ELLIPSE, Size(5, 5)) );
//menghapus hole
dilate( imgThresholded, imgThresholded, getStructuringElement(MORPH_ELLIPSE, Size(5, 5)) );
erode(imgThresholded, imgThresholded, getStructuringElement(MORPH_ELLIPSE, Size(5, 5)) );
//menampilkan image
imshow("Threshold", imgThresholded);
imshow("Original", imgOriginal);
if (waitKey(30) == 27) //exit esc
{
cout << "esc di tekan" << endl;
break;
}
}
return 0;
}