blob_detection_hsv.cpp
Go to the documentation of this file.
00001  #include "iostream"  
00002  #include "stdlib.h"  
00003  #include "stdio.h"  
00004  #include "cv.h"  
00005  #include "highgui.h"  
00006  #include "cstring"
00007 #include "ros/ros.h"
00008 
00009  /*This program is not done in CVBlob library*/  
00010  /*Purpose of the program to track the blob */  
00011 using namespace std;  
00012 
00013 
00014 
00015 /*Note:The detection algorithm is the same as the one you can find in one of my previous posts 
00016  The same algorithm is copied from that post and pasted in this 
00017  post only the values of the "sthreshold" and the "hupper" and the "hlower" are different....*/  
00018 
00019 int main(int argc, char *argv[])    
00020 {
00021   /*You may change the values of the sthreshold and hupper to get different results....
00022     For red blob for instance this value is to be between 175 and 185, that is 175 and 5
00023     in openCV notation.*/  
00024   int sthreshold;  
00025   double hupper, hlower;  
00026 
00027   if (argc != 5)
00028     {
00029       ROS_INFO("Usage %d <file_name>, <saturation threshold>, <lower hue value>, <upper hue value>", argv[0]);
00030     exit(0);
00031     }
00032   else
00033     {
00034       string image(argv[1]);
00035       sthreshold=atoi(argv[2]);
00036       hlower=atoi(argv[3]);
00037       hupper=atoi(argv[4]);
00038     }
00039 
00040   string image(argv[1]);
00041   //for iterations  
00042   int i,j,k;
00043   int height,width,step,channels;
00044   int stepmono,channelsmono;
00045   uchar *data,*datamono;  
00046   i=j=k=0;
00047   IplImage *frame=cvLoadImage(image.c_str(),1);
00048   //resultant image
00049   IplImage *result = cvCreateImage(cvGetSize(frame), 8, 1);
00050   //hue channel image
00051   IplImage* hue = cvCreateImage( cvGetSize(frame), 8, 1 );
00052 
00053   //allocations
00054   height = frame->height;  
00055   width = frame->width;  
00056   step = frame->widthStep;  
00057   channels = frame->nChannels;    
00058   datamono = (uchar *) result->imageData;
00059   stepmono = result->widthStep;
00060   channelsmono = result->nChannels;
00061  
00062   /* create  windows */  
00063  cvNamedWindow("monoimage", CV_WINDOW_AUTOSIZE);  
00064  cvNamedWindow("hsvimage", CV_WINDOW_AUTOSIZE);  
00065  //cvNamedWindow("original frame", CV_WINDOW_AUTOSIZE);  
00066  //cvNamedWindow("hue", CV_WINDOW_AUTOSIZE);
00067   
00068  IplImage *imageHSV = cvCloneImage(frame);
00069  /*Converting the color space of the image....*/  
00070  cvCvtColor(frame,imageHSV,CV_BGR2HSV);
00071  cvSplit(imageHSV, hue, 0, 0, 0);
00072 
00073  cvZero(result);
00074  data = (uchar *)imageHSV->imageData;  
00075 
00076  for (i = 0; i < height; i++) {
00077    for (j = 0; j < width; j++) {
00078      if( (data[(i) * step + j * channels] <= hupper) &&
00079          (data[(i) * step + j * channels] >= hlower) &&
00080          (data[(i) * step + j * (channels) + 1] > sthreshold) ) {
00081        datamono[(i) * stepmono + j * channelsmono] = 255;
00082      }
00083    }
00084  }
00085 
00086  //see: http://www.dca.fee.unicamp.br/dipcourse/html-dip/c9/s4/front-page.html
00087  cvErode(result,result,0,01);
00088  cvDilate( result,result,0,01); 
00089 
00090  //cvShowImage("original frame", frame);  
00091  cvShowImage("hsvimage", imageHSV);  
00092  cvShowImage("monoimage", result);  
00093  //cvShowImage("hue", hue);  
00094 
00095  while(true)
00096    {
00097      if( cvWaitKey(10) == 27 ) 
00098        {
00099          /* free memory */  
00100          //cvDestroyWindow("original frame");  
00101          cvDestroyWindow("monoimage");  
00102          cvDestroyWindow("hsvimage");  
00103          //cvDestroyWindow("hue");  
00104          cvReleaseImage(&hue);
00105          cvReleaseImage(&imageHSV);
00106          cvReleaseImage(&frame);
00107          cvReleaseImage(&result);
00108          break;
00109        }
00110    }
00111  return 0;
00112 }


image_algos
Author(s): Dejan Pangercic
autogenerated on Mon Oct 6 2014 09:35:23