Thresholding in HSV and Trackbars

In this example is explained how to convert a image from RGB color space to HSV color space.

Likewise, the code described below includes a simple example of trackbar use.

 

#include <iostream>

#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>

#include <string.h>
#include <time.h>
#include <stdlib.h>
using namespace cv;
using namespace std;

// Variables for minimum and maximun value of Hue
int hueMin=30;
int hueMax=170;

// Variables for minimum and maximun value of Saturation
int satMin=30;
int satMax=170;

// Variables for minimum and maximun value of Value
int valMin=30;
int valMax=170;

// Methods to handle changes values in trackbars
void eventoTrack1(int valor, void* p){
	//hueMin=valor;
	cout << "Valor Hue Min: " << hueMin << endl;
}

void eventoTrack2(int valor, void* p){
	//hueMin=valor;
	cout << "Valor Hue Max: " << hueMax << endl;
}

void eventoTrack3(int valor, void* p){
}
void eventoTrack4(int valor, void* p){
}
void eventoTrack5(int valor, void* p){
}
void eventoTrack6(int valor, void* p){
}


int main(int argc,char *argv[]){
	Mat frame;
	Mat hsv;
	Mat umbralizada;
	VideoCapture video(0);
	namedWindow("original",CV_WINDOW_AUTOSIZE);
	namedWindow("hsv",CV_WINDOW_AUTOSIZE);
	namedWindow("umbral",CV_WINDOW_AUTOSIZE);
	createTrackbar("HueMin","umbral",&hueMin,179,eventoTrack1);
	createTrackbar("HueMax","umbral",&hueMax,179,eventoTrack2);
	createTrackbar("SatMin","umbral",&satMin,255,eventoTrack3);
	createTrackbar("SatMax","umbral",&satMax,255,eventoTrack4);
	createTrackbar("ValMin","umbral",&valMin,255,eventoTrack5);
	createTrackbar("ValMax","umbral",&valMax,255,eventoTrack6);
	if(video.isOpened()){
		while(1==1){
			video >> frame;
			cvtColor(frame,hsv,CV_BGR2HSV);
			inRange(hsv,Scalar(hueMin,satMin,valMin),Scalar(hueMax,valMax,valMax),umbralizada);
			imshow("umbral",umbralizada);
			imshow("original",frame);
			imshow("hsv",hsv);
			if(waitKey(20)>0)
				break;
		}
	}
	return 0;
}