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
00010
00011 using namespace std;
00012
00013
00014
00015
00016
00017
00018
00019 int main(int argc, char *argv[])
00020 {
00021
00022
00023
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
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
00049 IplImage *result = cvCreateImage(cvGetSize(frame), 8, 1);
00050
00051 IplImage* hue = cvCreateImage( cvGetSize(frame), 8, 1 );
00052
00053
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
00063 cvNamedWindow("monoimage", CV_WINDOW_AUTOSIZE);
00064 cvNamedWindow("hsvimage", CV_WINDOW_AUTOSIZE);
00065
00066
00067
00068 IplImage *imageHSV = cvCloneImage(frame);
00069
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
00087 cvErode(result,result,0,01);
00088 cvDilate( result,result,0,01);
00089
00090
00091 cvShowImage("hsvimage", imageHSV);
00092 cvShowImage("monoimage", result);
00093
00094
00095 while(true)
00096 {
00097 if( cvWaitKey(10) == 27 )
00098 {
00099
00100
00101 cvDestroyWindow("monoimage");
00102 cvDestroyWindow("hsvimage");
00103
00104 cvReleaseImage(&hue);
00105 cvReleaseImage(&imageHSV);
00106 cvReleaseImage(&frame);
00107 cvReleaseImage(&result);
00108 break;
00109 }
00110 }
00111 return 0;
00112 }