Created
October 14, 2014 13:36
-
-
Save jetarin-a/50d036396b9d09979138 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <opencv2/opencv.hpp> | |
#include <opencv2/opencv_lib.hpp> | |
int | |
main (int argc, char *argv[]) | |
{ | |
int i; | |
IplImage *src_img = 0; | |
IplImage *src_img_gray = 0; | |
IplImage *tmp_img; | |
CvMemStorage *storage = cvCreateMemStorage (0); | |
CvSeq *contours = 0; | |
CvPoint *point, *tmp; | |
CvSeq *contour; | |
CvTreeNodeIterator it; | |
FILE *fp; | |
src_img = cvLoadImage ("original.jpg", CV_LOAD_IMAGE_COLOR); | |
if (src_img == 0) | |
exit(-1); | |
src_img_gray = cvCreateImage (cvGetSize (src_img), IPL_DEPTH_8U, 1); | |
cvCvtColor (src_img, src_img_gray, CV_BGR2GRAY); | |
tmp_img = cvCreateImage (cvGetSize (src_img), IPL_DEPTH_8U, 1); | |
// (1)画像の二値化と輪郭の検出 | |
cvThreshold (src_img_gray, tmp_img, 120, 255, CV_THRESH_BINARY); | |
cvFindContours (tmp_img, storage, &contours, sizeof (CvContour), CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE); | |
/* 輪郭シーケンスから座標を取得 */ | |
if(NULL == (fp=fopen("output.csv", "w"))){ | |
printf("Can not open the outfile.\n"); | |
return(-1); | |
} | |
cvInitTreeNodeIterator (&it, contours, 1); | |
// (3)各ノード(輪郭)を走査 | |
while ((contour = (CvSeq *) cvNextTreeNode (&it)) != NULL) { | |
// (4)輪郭を構成する頂点座標を取得 | |
tmp = CV_GET_SEQ_ELEM (CvPoint, contour, 0); | |
for (i = 0; i < contour->total; i++) { | |
point = CV_GET_SEQ_ELEM (CvPoint, contour, i); | |
cvLine (src_img, *tmp, *point, CV_RGB (0, 0, 255), 2); | |
if(i != (contour->total)-1){ | |
fprintf(fp, "%d, %d, ", point->x, point->y); | |
}else{ | |
fprintf(fp, "%d, %d", point->x, point->y); | |
} | |
tmp = point; | |
} | |
} | |
fclose(fp); | |
cvNamedWindow ("Contours", CV_WINDOW_AUTOSIZE); | |
cvShowImage ("Contours", src_img); | |
cvWaitKey (0); | |
cvDestroyWindow ("Contours"); | |
cvReleaseImage (&src_img); | |
cvReleaseImage (&src_img_gray); | |
cvReleaseImage (&tmp_img); | |
cvReleaseMemStorage (&storage); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment