I'm working on detecting the shape of any object. I have a binary image where the 开发者_如何学JAVAbackground is in white and the foreground/object is in black. I need to detect the shape of the object in the foreground which is in black.
How can i do it? The shape may be of a man/car/box etc. Please help
I'm not sure which is your final goal as amphetamachine said but a pretty common approach to detect shapes could be the use of cvFindContours which given a binary image and returns a set of 'CvContour' (which in fact is a cvSeq). Binary image can be retrieved quite simple by thresholding the image (cvThreshold). Check out the contours.c example in the sample/ of opencv src directory. Check this link as well:
Noah (2009) opencv tutorial
this sample code will give you an general idea:
cvThreshold( g_gray, g_gray, g_thresh, 255, CV_THRESH_BINARY );
cvFindContours( g_gray, g_storage, &contours );
cvZero( g_gray );
if( contours ){
cvDrawContours(
g_gray,
contours,
cvScalarAll(255),
cvScalarAll(255),
100 );
}
cvShowImage( "Contours", g_gray );
Once you have an encoding of the contour you can use cvMatchShapes which take 2 contours and return a measure of similarity between these contours.
Hope this approach provide you a head start!
For accurate shape detection you need to use haar detection or at the least K nearest neighbor. Haar detection can be very accurate, but it takes a long time to set up. K nearest neighbor is easier to set up but is not as accurate. Check out this youtube video. This guy is using KNN to detect different hand gestures. Notice that the comparison image is basically a black blob. The bad thing about KNN is that it is that it takes a lot more resources to run the program, but with haar detection, the major processing has already been done when you create the cascade xml files with haartraining.exe
精彩评论