2011年9月3日土曜日

test



// p243 ex8-3

#include
#include

#define CVX_RED CV_RGB(0xff,0x00,0x00)
#define CVX_GREEN CV_RGB(0x00,0xff,0x00)
#define CVX_BLUE CV_RGB(0x00,0x00,0xff)


int main() {
cvNamedWindow("contour", 1);

IplImage *img_8uc1 = cvLoadImage("/Users/yusuke/Dropbox/openCV/OpenCV env/OpenCV env/data/stuff.jpg", CV_LOAD_IMAGE_GRAYSCALE);
IplImage *img_edge = cvCreateImage(cvGetSize(img_8uc1), 8, 1);
IplImage *img_8uc3 = cvCreateImage(cvGetSize(img_8uc1), 8, 3);

cvThreshold(img_8uc1, img_edge, 128, 255, CV_THRESH_BINARY);

CvMemStorage *storage = cvCreateMemStorage();
CvSeq *first_contour = NULL;

//last argument is mode, there are other modes!!, returning the number of contours found
int Nc = cvFindContours(img_edge, storage, &first_contour, sizeof(CvContour), CV_RETR_EXTERNAL);

int n = 0;
printf("Total Contours Detected: %d\n", Nc);
for (CvSeq *c = first_contour; c != NULL; c = c->h_next) { // traverse the list of found contour
cvCvtColor(img_8uc1, img_8uc3, CV_GRAY2BGR);

// draw that contour using color, color is defined above
cvDrawContours(img_8uc3, c, CVX_RED, CVX_BLUE, 0, 2, 8);
printf("contour #%d\n", n);
cvShowImage("contour", img_8uc3);
printf(" %d elements: \n", c->total); // c->total is the number of edges,or points
for (int i = 0; i < c->total; i++) { // traverse the all edges of that contour
CvPoint *p = CV_GET_SEQ_ELEM(CvPoint, c, i);
printf(" (%d, %d)\n", p->x, p->y);
}
cvWaitKey(0);
n++;
}
printf("Finished\n");
cvCvtColor(img_8uc1, img_8uc3, CV_GRAY2BGR);
cvShowImage("contour", img_8uc3);
cvWaitKey(0);

return 0;
}

0 件のコメント:

コメントを投稿