# Which are the steps to extract contours/shapes from depth videos?

I am trying to create a histogram of the depth videos (converted to grayscale first) in order to apply a threshold to keep only highest values, and then do some dilation in order to extract contours. Apparently I am stuck, and besides that i don't know if what I am thinking is the right way to extract contours from depth videos.

In the following code I got stuck in the point of applying the threshold. I think that iam applying it in the wrong way. Which is the correct to apply a threshold in this situation in order to obtain a black and white image?

Any suggestions or tutorials would be awesome!!!

Thank you very much!

int bins = 256;
int hsize[] = {bins};

//max and min value of the histogram
float max_value = 0, min_value = 0;

float value;
int normalized;

//ranges - grayscale 0 to 256
float xranges[] = { 0, 256 };
float* ranges[] = { xranges };

//image is the actual source from input depth video
gray = cvCreateImage( cvGetSize(image), 8, 1 );
cvCvtColor( image, gray, CV_BGR2GRAY );

cvNamedWindow("original",1);
cvNamedWindow("gray",1);
cvNamedWindow("histogram",1);
cvNamedWindow("black & white",1);

IplImage* planes[] = { gray };

//get the histogram and some info about it
hist = cvCreateHist( 1, hsize, CV_HIST_ARRAY, ranges,1);
cvCalcHist( planes, hist, 0, NULL);
cvGetMinMaxHistValue( hist, &min_value, &max_value);
printf("min: %f, max: %f\n", min_value, max_value);

imgHistogram = cvCreateImage(cvSize(bins, image->height),8,1);
cvRectangle(imgHistogram, cvPoint(0,0), cvPoint(256,image->height), CV_RGB(255,255,255),-1);

//I think that here i have messed up things :( Any suggestions ???
bw_img = cvCreateImage(cvGetSize(imgHistogram), IPL_DEPTH_8U, 1);
cvThreshold(imgHistogram, bw_img, 150, 255, CV_THRESH_BINARY);

//draw the histogram
for(int i=0; i < bins; i++){
value = cvQueryHistValue_1D( hist, i);
normalized = cvRound(value*image->height/max_value);
cvLine(imgHistogram,cvPoint(i,image->height), cvPoint(i,image->height-normalized), CV_RGB(0,0,0));
}

//show the image results
cvShowImage( "original", image );
cvShowImage( "gray", gray );
cvShowImage( "histogram", imgHistogram );
cvShowImage( "balck & white", bw_img);


Images showing the step processes 1) source video input ( http://imageshack.us/photo/my-images/823/sourcevideo.jpg/ ) 2) gray-scale video ( http://imageshack.us/photo/my-images/23/grayscalei.jpg/ ) 3) binary video ( http://imageshack.us/photo/my-images/684/binaryi.jpg/ ) 4) final contour output ( http://imageshack.us/photo/my-images/35/finaloutput.jpg/ )

The images show what i would like to accomplish but for depth videos. I got problem obtaining the binary video. I have create a histogram and i haven't found a way to apply threshold in order to obtain highest pixel values and to transform the video to black and white.

First of all, what i am asking is what are right steps to extract contours from depth videos and if my approach is correct or not as described below??

1) grayscale an image 2) create histogram 3) apply threshold to convert to black and white image and keep only highest pixel values 4) (dilation/erosion) depends on the black and white image 5) find contours on the black and white image

Second of all, my prolem is how can i create a binary(black and white image) with only the highest values retrived from the histogram of a gray-scale image??? I think that I am applying it the wrong way because i don't get the expected result (black & white image), the only thing i get is a totaly black image and nothing else.

Third of all, i have put the code to make things more clear and not vaguely.