Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
hackassin
GitHub Repository: hackassin/learnopencv
Path: blob/master/ColorSpaces/interactiveColorDetect.cpp
3118 views
1
#include "opencv2/opencv.hpp"
2
#include <iostream>
3
using namespace cv;
4
using namespace std;
5
6
//Global Variables
7
Mat img, placeholder;
8
9
// Callback function for any event on he mouse
10
void onMouse( int event, int x, int y, int flags, void* userdata )
11
{
12
if( event == EVENT_MOUSEMOVE )
13
{
14
15
Vec3b bgrPixel(img.at<Vec3b>(y, x));
16
17
Mat3b hsv,ycb,lab;
18
// Create Mat object from vector since cvtColor accepts a Mat object
19
Mat3b bgr (bgrPixel);
20
21
//Convert the single pixel BGR Mat to other formats
22
cvtColor(bgr, ycb, COLOR_BGR2YCrCb);
23
cvtColor(bgr, hsv, COLOR_BGR2HSV);
24
cvtColor(bgr, lab, COLOR_BGR2Lab);
25
26
//Get back the vector from Mat
27
Vec3b hsvPixel(hsv.at<Vec3b>(0,0));
28
Vec3b ycbPixel(ycb.at<Vec3b>(0,0));
29
Vec3b labPixel(lab.at<Vec3b>(0,0));
30
31
// Create an empty placeholder for displaying the values
32
placeholder = Mat::zeros(img.rows,400,CV_8UC3);
33
34
//fill the placeholder with the values of color spaces
35
putText(placeholder, format("BGR [%d, %d, %d]",bgrPixel[0],bgrPixel[1],bgrPixel[2]), Point(20, 70), FONT_HERSHEY_COMPLEX, .9, Scalar(255,255,255), 1);
36
putText(placeholder, format("HSV [%d, %d, %d]",hsvPixel[0],hsvPixel[1],hsvPixel[2]), Point(20, 140), FONT_HERSHEY_COMPLEX, .9, Scalar(255,255,255), 1);
37
putText(placeholder, format("YCrCb [%d, %d, %d]",ycbPixel[0],ycbPixel[1],ycbPixel[2]), Point(20, 210), FONT_HERSHEY_COMPLEX, .9, Scalar(255,255,255), 1);
38
putText(placeholder, format("LAB [%d, %d, %d]",labPixel[0],labPixel[1],labPixel[2]), Point(20, 280), FONT_HERSHEY_COMPLEX, .9, Scalar(255,255,255), 1);
39
40
41
Size sz1 = img.size();
42
Size sz2 = placeholder.size();
43
44
//Combine the two results to show side by side in a single image
45
Mat combinedResult(sz1.height, sz1.width+sz2.width, CV_8UC3);
46
Mat left(combinedResult, Rect(0, 0, sz1.width, sz1.height));
47
img.copyTo(left);
48
Mat right(combinedResult, Rect(sz1.width, 0, sz2.width, sz2.height));
49
placeholder.copyTo(right);
50
imshow("PRESS P for Previous, N for Next Image", combinedResult);
51
}
52
}
53
54
int main( int argc, const char** argv )
55
{
56
// filename
57
// Read the input image
58
int image_number = 0;
59
int nImages = 10;
60
61
if(argc > 1)
62
nImages = atoi(argv[1]);
63
64
char filename[20];
65
sprintf(filename,"images/rub%02d.jpg",image_number%nImages);
66
img = imread(filename);
67
// Resize the image to 400x400
68
Size rsize(400,400);
69
resize(img,img,rsize);
70
71
if(img.empty())
72
{
73
return -1;
74
}
75
76
// Create an empty window
77
namedWindow("PRESS P for Previous, N for Next Image", WINDOW_AUTOSIZE);
78
// Create a callback function for any event on the mouse
79
setMouseCallback( "PRESS P for Previous, N for Next Image", onMouse );
80
81
imshow( "PRESS P for Previous, N for Next Image", img );
82
while(1)
83
{
84
char k = waitKey(1) & 0xFF;
85
if (k == 27)
86
break;
87
//Check next image in the folder
88
if (k =='n')
89
{
90
image_number++;
91
sprintf(filename,"images/rub%02d.jpg",image_number%nImages);
92
img = imread(filename);
93
resize(img,img,rsize);
94
}
95
//Check previous image in he folder
96
else if (k =='p')
97
{
98
image_number--;
99
sprintf(filename,"images/rub%02d.jpg",image_number%nImages);
100
img = imread(filename);
101
resize(img,img,rsize);
102
}
103
}
104
return 0;
105
}
106