Click here to Skip to main content
15,122,559 members
Articles / All Topics
Technical Blog
Posted 4 Dec 2014

Tagged as

Stats

37.7K views
9 bookmarked

In color based object detection project, one can detect the color object. User needs to change the trackbar values,

Rate me:
Please Sign up or sign in to vote.
5.00/5 (5 votes)
4 Dec 2014CPOL1 min read
Color based object detection
  • Download source code

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

Image 1

Image 2

 

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.

Image 3  Image 4

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.

Image 5  Image 6

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.

Image 7   Image 8

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

C++
#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 ;

//vector<vector<Point> > contours;
//vector<Vec4i> hierarchy;

void draw_conture(IplImage * temp)
{
  /*
   cv::Mat tempm(temp,true);  
   findContours( tempm , contours, hierarchy, CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE, cv::Point(0, 0) );
   vector<vector<cv::Point> > contours_poly( contours.size() );
   vector<Rect> boundRect( contours.size() );
   vector<float>area( contours.size() );

   for(  int i = 0; i < contours.size(); i++ )
   {
       approxPolyDP( Mat(contours[i]), contours_poly[i], 3, true );
      boundRect[i] = boundingRect( Mat(contours_poly[i]) ) ;
      area[i]= contourArea(Mat(contours_poly[i]));
   }

   const CvScalar color=CV_RGB(0x00,0xFF,0x00);                        
   for( int i = 0; i< contours.size(); i++ )
   {                
     if (area[i]>1000)
         rectangle(tempm, boundRect[i].tl(), boundRect[i].br(), color, 1, 1, 0 );
   }
  */ 

   CvSeq* contour;  //hold the pointer to a contour
   CvSeq* result;     //hold sequence of points of a contour
   CvMemStorage *storage = cvCreateMemStorage(0); //storage area for all contours

   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); //extract bounding box for current contour
       if( cvContourArea(result, CV_WHOLE_SEQ,0) >= 1000 )
       {
         //cvDrawContours(frame, contour, CV_RGB(0,0,255), CV_RGB(0,0,255), 2, 1, 8, cvPoint(0, 0));
         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

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

SOHAM_GANDHI
Software Developer
India India
I am Soham Gandhi from India. I studied BE in Information Technology at University of Pune. (www.unipune.ac.in). I have been learning OpenCV, Enjoying it.
My research interest in Image Processing, Artificial Intelligence, Security Systems, Security Metrics. I work on OpenCV, EmguCV, Visual Studio 2008/10, Java platform.

Home Page (Blog)


You Tube Channel

Comments and Discussions

 
Questionissue regarding Download the Code Pin
Member 1349301114-Feb-18 1:12
MemberMember 1349301114-Feb-18 1:12 
QuestionWant to know the working of code portions. Pin
Member 114887441-Mar-15 1:20
MemberMember 114887441-Mar-15 1:20 
AnswerRe: Want to know the working of code portions. Pin
SOHAM_GANDHI2-Mar-15 23:56
MemberSOHAM_GANDHI2-Mar-15 23:56 
Questionc instead of c++ openCV functions? Pin
LWessels8-Dec-14 7:56
MemberLWessels8-Dec-14 7:56 
AnswerRe: c instead of c++ openCV functions? Pin
SOHAM_GANDHI8-Dec-14 18:27
MemberSOHAM_GANDHI8-Dec-14 18:27 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.