https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=Y3F5GV7T4Y5LE


Introduction
In color based object detection project, one can detect the color object. User needs to change the trackbar values, As trackbar changes the value, the color space filters with HSV filtered image processed bounding rectangle. Bounding rectangle (MAX 2000 (RECT AREA) in project) detects the contourArea
(2000) and draws the rectangle.
Sample - One
In this RED color spotted. then using bounding rectangle the STOP SIGN detected. LowerH:168, UpperH:256, LowerS:175, UpperS: 256, LoweV:0, UpperV:256 By manipulating these values, one can detect the color based any object.

Sample - Two
In this YELLOW color spotted. Then using bounding rectangle the SIGN detected. LowerH:0, UpperH:75, LowerS:139, UpperS: 256, LoweV:132, UpperV:198 By manipulating these values, one can detect the color based any object.

Sample - Three
In this WHITE color spotted, then using bounding rectangle the SIGN detected. LowerH:0, UpperH:256, LowerS:0, UpperS: 256, LoweV:0, UpperV:61 By manipulating these values, one can detect the color based any object.

Code
Here is the complete code for color based object detection using the opencv. The following code has been completed using Visual Studio 2008 and Opencv Libraries.
Process of Setting up the Visual Studio - Click to View
User Libraries and Study Materials - Click to View
#include "opencv/cv.h"
#include "opencv/highgui.h"
#include "opencv2/opencv.hpp"
#include "opencv2/core/core.hpp"
#include "opencv2/opencv.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "iostream"
#include "stdio.h"
#include "string"
#include <stdlib.h>
using namespace cv;
using namespace std;
int lowerH=168;
int upperH=256;
int lowerS=175;
int upperS=256;
int lowerV=0;
int upperV=256;
IplImage* frame = 0 ;
IplImage* frame2 = 0 ;
IplImage* imgHSV = 0 ;
IplImage* imgThresh = 0 ;
void draw_conture(IplImage * temp)
{
CvSeq* contour; CvSeq* result; CvMemStorage *storage = cvCreateMemStorage(0);
cvFindContours(temp, storage, &contour, sizeof(CvContour),
CV_RETR_LIST, CV_CHAIN_APPROX_SIMPLE, cvPoint(0,0));
for (; contour != 0; contour = contour->h_next)
{
result = cvApproxPoly(contour, sizeof(CvContour),
storage, CV_POLY_APPROX_DP, cvContourPerimeter(contour)*0.02, 0);
CvRect rect = cvBoundingRect(contour, 0); if( cvContourArea(result, CV_WHOLE_SEQ,0) >= 1000 )
{
frame2 = cvCreateImage(cvGetSize(frame), frame->depth, frame->nChannels);
cvCopy(frame, frame2);
cvRectangle(frame2, cvPoint(rect.x, rect.y),
cvPoint(rect.x+rect.width, rect.y+rect.height), CV_RGB(0,0,255),2, 8, 0);
}
}
cvShowImage("Object Color Range", frame2);
}
#pragma region trackbar
void on_trackbar1(int position)
{
lowerH = position;
cvInRangeS(imgHSV, cvScalar(lowerH,lowerS,lowerV), cvScalar(upperH,upperS,upperV),imgThresh);
cvShowImage("Object Detected", imgThresh);
draw_conture(imgThresh);
}
void on_trackbar2(int position)
{
upperH = position;
cvInRangeS(imgHSV, cvScalar(lowerH,lowerS,lowerV), cvScalar(upperH,upperS,upperV),imgThresh);
cvShowImage("Object Detected", imgThresh);
draw_conture(imgThresh);
}
void on_trackbar3(int position)
{
lowerS = position;
cvInRangeS(imgHSV, cvScalar(lowerH,lowerS,lowerV), cvScalar(upperH,upperS,upperV),imgThresh);
cvShowImage("Object Detected", imgThresh);
draw_conture(imgThresh);
}
void on_trackbar4(int position)
{
upperS = position;
cvInRangeS(imgHSV, cvScalar(lowerH,lowerS,lowerV), cvScalar(upperH,upperS,upperV),imgThresh);
cvShowImage("Object Detected", imgThresh);
draw_conture(imgThresh);
}
void on_trackbar5(int position)
{
lowerV = position;
cvInRangeS(imgHSV, cvScalar(lowerH,lowerS,lowerV), cvScalar(upperH,upperS,upperV),imgThresh);
cvShowImage("Object Detected", imgThresh);
draw_conture(imgThresh);
}
void on_trackbar6(int position)
{
upperV = position;
cvInRangeS(imgHSV, cvScalar(lowerH,lowerS,lowerV), cvScalar(upperH,upperS,upperV),imgThresh);
cvShowImage("Object Detected", imgThresh);
draw_conture(imgThresh);
}
#pragma endregion trackbar
int main()
{
cvNamedWindow("Object Color Range",1);
cvNamedWindow("Object Detected",1);
cvCreateTrackbar("LowerH", "Object Detected", &lowerH, 256, on_trackbar1);
cvCreateTrackbar("UpperH", "Object Detected", &upperH, 256, on_trackbar2);
cvCreateTrackbar("LowerS", "Object Detected", &lowerS, 256, on_trackbar3);
cvCreateTrackbar("UpperS", "Object Detected", &upperS, 256, on_trackbar4);
cvCreateTrackbar("LowerV", "Object Detected", &lowerV, 256, on_trackbar5);
cvCreateTrackbar("UpperV", "Object Detected", &upperV, 256, on_trackbar6);
frame = cvLoadImage("stop.jpg",1);
imgHSV = cvCreateImage(cvGetSize(frame), IPL_DEPTH_8U, 3 ) ;
imgThresh = cvCreateImage(cvGetSize(imgHSV),IPL_DEPTH_8U, 1);
cvCvtColor(frame, imgHSV, CV_BGR2HSV);
cvInRangeS(imgHSV, cvScalar(lowerH,lowerS,lowerV), cvScalar(upperH,upperS,upperV),imgThresh);
cvShowImage("Object Detected", imgThresh);
cvShowImage("Object Color Range", frame);
draw_conture(imgThresh);
cvWaitKey(0);
cvReleaseImage(&imgHSV);
cvReleaseImage(&imgThresh);
cvReleaseImage(&frame);
cvDestroyAllWindows();
return 0;
}
Point of Interest
- Learn how to set up OpenCV with Visual Studio
- Learn how to process the image
- Learn how to filter the color space
- Learn how to use bounding rectangle
- Learn how to detect the object based on color
- Learn how to use opencv trackbar
References