Path: blob/master/ColorSpaces/interactiveColorDetect.cpp
3118 views
#include "opencv2/opencv.hpp"1#include <iostream>2using namespace cv;3using namespace std;45//Global Variables6Mat img, placeholder;78// Callback function for any event on he mouse9void onMouse( int event, int x, int y, int flags, void* userdata )10{11if( event == EVENT_MOUSEMOVE )12{1314Vec3b bgrPixel(img.at<Vec3b>(y, x));1516Mat3b hsv,ycb,lab;17// Create Mat object from vector since cvtColor accepts a Mat object18Mat3b bgr (bgrPixel);1920//Convert the single pixel BGR Mat to other formats21cvtColor(bgr, ycb, COLOR_BGR2YCrCb);22cvtColor(bgr, hsv, COLOR_BGR2HSV);23cvtColor(bgr, lab, COLOR_BGR2Lab);2425//Get back the vector from Mat26Vec3b hsvPixel(hsv.at<Vec3b>(0,0));27Vec3b ycbPixel(ycb.at<Vec3b>(0,0));28Vec3b labPixel(lab.at<Vec3b>(0,0));2930// Create an empty placeholder for displaying the values31placeholder = Mat::zeros(img.rows,400,CV_8UC3);3233//fill the placeholder with the values of color spaces34putText(placeholder, format("BGR [%d, %d, %d]",bgrPixel[0],bgrPixel[1],bgrPixel[2]), Point(20, 70), FONT_HERSHEY_COMPLEX, .9, Scalar(255,255,255), 1);35putText(placeholder, format("HSV [%d, %d, %d]",hsvPixel[0],hsvPixel[1],hsvPixel[2]), Point(20, 140), FONT_HERSHEY_COMPLEX, .9, Scalar(255,255,255), 1);36putText(placeholder, format("YCrCb [%d, %d, %d]",ycbPixel[0],ycbPixel[1],ycbPixel[2]), Point(20, 210), FONT_HERSHEY_COMPLEX, .9, Scalar(255,255,255), 1);37putText(placeholder, format("LAB [%d, %d, %d]",labPixel[0],labPixel[1],labPixel[2]), Point(20, 280), FONT_HERSHEY_COMPLEX, .9, Scalar(255,255,255), 1);383940Size sz1 = img.size();41Size sz2 = placeholder.size();4243//Combine the two results to show side by side in a single image44Mat combinedResult(sz1.height, sz1.width+sz2.width, CV_8UC3);45Mat left(combinedResult, Rect(0, 0, sz1.width, sz1.height));46img.copyTo(left);47Mat right(combinedResult, Rect(sz1.width, 0, sz2.width, sz2.height));48placeholder.copyTo(right);49imshow("PRESS P for Previous, N for Next Image", combinedResult);50}51}5253int main( int argc, const char** argv )54{55// filename56// Read the input image57int image_number = 0;58int nImages = 10;5960if(argc > 1)61nImages = atoi(argv[1]);6263char filename[20];64sprintf(filename,"images/rub%02d.jpg",image_number%nImages);65img = imread(filename);66// Resize the image to 400x40067Size rsize(400,400);68resize(img,img,rsize);6970if(img.empty())71{72return -1;73}7475// Create an empty window76namedWindow("PRESS P for Previous, N for Next Image", WINDOW_AUTOSIZE);77// Create a callback function for any event on the mouse78setMouseCallback( "PRESS P for Previous, N for Next Image", onMouse );7980imshow( "PRESS P for Previous, N for Next Image", img );81while(1)82{83char k = waitKey(1) & 0xFF;84if (k == 27)85break;86//Check next image in the folder87if (k =='n')88{89image_number++;90sprintf(filename,"images/rub%02d.jpg",image_number%nImages);91img = imread(filename);92resize(img,img,rsize);93}94//Check previous image in he folder95else if (k =='p')96{97image_number--;98sprintf(filename,"images/rub%02d.jpg",image_number%nImages);99img = imread(filename);100resize(img,img,rsize);101}102}103return 0;104}105106