BlobLabeling.h 및 BlobLabeling.cpp 는 http://martinblog.net/826 에 있는 소스 사용
#include <opencv/highgui.h>
#include <opencv/cv.h>
#include <stdio.h>
#include "BlobLabeling.h"
extern void ThinningGonzalez(IplImage *inImg, IplImage *outImg);
int main(int argc, char *argv[])
{
IplImage *video_orig;
IplImage *video_gray;
IplImage *video_out;
IplImage *bmp_orig;
IplImage *bmp_gray;
IplImage *bmp_out;
CvCapture* capture;
IplConvKernel *kernel;
// 윈도우 생성
cvNamedWindow("video_orig",CV_WINDOW_AUTOSIZE);
cvNamedWindow("video_out",CV_WINDOW_AUTOSIZE);
cvNamedWindow("bmp_orig",CV_WINDOW_AUTOSIZE);
cvNamedWindow("bmp_out",CV_WINDOW_AUTOSIZE);
// 카메라 객체
printf("카메라 객체\n");
capture = cvCaptureFromCAM(1);
// 이미지 객체
printf("이미지 객체\n");
bmp_orig = cvLoadImage("C:\\hello_newyork.bmp");
bmp_gray = cvCreateImage(cvGetSize(bmp_orig), IPL_DEPTH_8U, 1);
bmp_out = cvCreateImage(cvGetSize(bmp_orig), IPL_DEPTH_8U, 3);
printf("영상 객체\n");
video_orig = cvQueryFrame(capture);
video_gray = cvCreateImage(cvGetSize(video_orig), IPL_DEPTH_8U, 1);
video_out = cvCreateImage(cvGetSize(video_orig), IPL_DEPTH_8U, 1);
// 필터 객체
kernel = cvCreateStructuringElementEx(9, 9, 4, 4, CV_SHAPE_RECT, NULL);
// 이미지 출력
printf("이미지 출력\n");
cvCvtColor(bmp_orig, bmp_gray, CV_RGB2GRAY);
cvCvtColor(bmp_gray, bmp_out, CV_GRAY2BGR);
//cvMorphologyEx(bmp_gray, bmp_gray, NULL, kernel, CV_MOP_TOPHAT, 3);
//cvMorphologyEx(bmp_out, bmp_out, NULL, kernel, CV_MOP_TOPHAT, 3);
cvThreshold( bmp_gray, bmp_gray, 200.0, 255.0,
CV_THRESH_BINARY|CV_THRESH_OTSU);
cvThreshold( bmp_gray, bmp_gray, 1, 255, CV_THRESH_BINARY_INV);
CBlobLabeling blob;
blob.SetParam( bmp_gray, 200 );
blob.DoLabeling();
for(int i=0; i<blob.m_nBlobs; i++){
CvPoint pt1 = cvPoint( blob.m_recBlobs[i].x,
blob.m_recBlobs[i].y );
CvPoint pt2 = cvPoint( pt1.x + blob.m_recBlobs[i].width,
pt1.y + blob.m_recBlobs[i].height );
CvScalar color = cvScalar( 0, 0, 255 );
printf("(%3d,%3d) - (%3d,%3d)\n", pt1.x, pt1.y, pt2.x, pt2.y);
cvDrawRect( bmp_out, pt1, pt2, color );
}
cvShowImage("bmp_orig", bmp_gray);
cvShowImage("bmp_out", bmp_out);
int i=0;
while(1){
if( cvWaitKey(10) == 27 ) break;
cvCvtColor(video_orig, video_out, CV_RGB2GRAY);
cvShowImage("video_orig", video_orig);
cvMorphologyEx(video_out, video_out, NULL, kernel, CV_MOP_TOPHAT, 1);
cvShowImage("video_out", video_out);
video_orig = cvQueryFrame(capture);
}
// 이미지 해제
cvReleaseImage(&video_out); printf("video_out 해제 완료\n");
cvReleaseImage(&bmp_orig); printf("bmp_orig 해제 완료\n");
// 캡쳐 해제
cvReleaseCapture(&capture); printf("capture 해제 완료\n");
// 윈도우 파괴
cvDestroyAllWindows(); printf("종료\n");
return 0;
}