void Ellipse(IplImage * sabunn_img)
{
// ellipseの処理用
IplImage *dst_img;
IplImage *src_img_gray = 0;
IplImage *tmp_img;
CvMemStorage *storage = cvCreateMemStorage(0);
CvSeq *contours = 0;
CvBox2D ellipse;
CvTreeNodeIterator it;
CvPoint2D32f pt[4];
//src_img_gray = cvCreateImage(cvGetSize(sabunn_img), IPL_DEPTH_8U, 1);
src_img_gray = cvCloneImage(sabunn_img);
//cvCvtColor (sabunn_img, src_img_gray, CV_BGR2GRAY);
//printf("%d\n", __LINE__);
tmp_img = cvCreateImage(cvGetSize(sabunn_img), IPL_DEPTH_8U, 1);
//printf("%d\n", __LINE__);
dst_img = cvCloneImage(sabunn_img);
// (2)二値化と輪郭の検出
cvThreshold(src_img_gray, tmp_img, 95, 255, CV_THRESH_BINARY);
cvReleaseImage(&src_img_gray);
cvFindContours(tmp_img, storage, &contours, sizeof(CvContour),
CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE, cvPoint(0, 0));
if (contours == NULL)
printf("\n");
else{
// (3)ツリーノードイテレータの初期化
cvInitTreeNodeIterator(&it, contours, 3);
while ((contours = (CvSeq *) cvNextTreeNode(&it)) != NULL) {
if (contours->total > 6) {
// (4)楕円のフィッティング
ellipse = cvFitEllipse2(contours);
ellipse.angle = 90.0 - ellipse.angle;
//if (ellipse.size.width * ellipse.size.height > 2350)
// yubi_count++; //指の個数をカウント
//kukei_count++;
//printf("yubi_count:%d\n",yubi_count);
// (5)輪郭,楕円,包含矩形の描画
if (ellipse.size.width > ellipse.size.height
&& ellipse.size.height * 2 <= ellipse.size.width
|| ellipse.size.width < ellipse.size.height
&& ellipse.size.height >= ellipse.size.width * 2) {
if (ellipse.size.width * ellipse.size.height > 2350) {
// printf("size:%f w:%f,h:%f,angle:%f\n",
// ellipse.size.width * ellipse.size.height,
// ellipse.size.width, ellipse.size.height,
// ellipse.angle);
yubi_count++;
cvDrawContours(dst_img, contours,
CV_RGB(255, 0, 0), CV_RGB(255, 0,
0), 0, 1,
CV_AA, cvPoint(0, 0));
cvEllipseBox(dst_img, ellipse, CV_RGB(0, 0, 255),
2);
cvBoxPoints(ellipse, pt);
cvLine(dst_img, cvPointFrom32f(pt[0]),
cvPointFrom32f(pt[1]), CV_RGB(0, 255, 0));
cvLine(dst_img, cvPointFrom32f(pt[1]),
cvPointFrom32f(pt[2]), CV_RGB(0, 255, 0));
cvLine(dst_img, cvPointFrom32f(pt[2]),
cvPointFrom32f(pt[3]), CV_RGB(0, 255, 0));
cvLine(dst_img, cvPointFrom32f(pt[3]),
cvPointFrom32f(pt[0]), CV_RGB(0, 255, 0));
}
}
}
}
}
if (dst_img->origin == 0) {
// 左上が原点の場合
cvFlip(dst_img, dst_img, 0);
}
// 画像を表示させる
cvShowImage(windowNameFit, dst_img);
// メモリを解放する
cvReleaseImage(&dst_img);
cvReleaseMemStorage(&storage);
cvReleaseImage(&tmp_img);
//free(storage);
//free(contours);
}
最終更新:2010年01月29日 13:54