学习opencv——课后题8-1图像检测轮廓和关键点

/习题8-1对图片检测轮廓和关键点检测
#include <cv.h>
#include <opencv2/legacy/legacy.hpp>
#include <stdio.h>
#include <stdlib.h>
#include "highgui.h"
using namespace std;
using namespace cv;

int main( int argc, char** argv )
{
int i, idx;
CvPoint p;
CvMemStorage* storage_ct = cvCreateMemStorage(0);
CvMemStorage* storage_dp = cvCreateMemStorage(0);
IplImage* img = cvLoadImage("tu6-9.jpg", CV_LOAD_IMAGE_GRAYSCALE);

cvNamedWindow( "image" );
cvShowImage( "image", img );

CvSeq* contours = 0;
CvSeq* dps = 0;
double t1=(double)cvGetTickCount();
cvFindContours( img, storage_ct, &contours, sizeof(CvContour),
CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE );

dps = cvFindDominantPoints( contours, storage_dp, CV_DOMINANT_IPAN, 7, 20, 9, 150 );
//多边形逼近
contours = cvApproxPoly( contours, sizeof(CvContour), storage_ct, CV_POLY_APPROX_DP, 3, 1 );

double t2=(double)cvGetTickCount();
printf("run time:\n",(t2-t1)*1000/(cvGetTickFrequency()));

printf("found %d DPs and %d Contours \n", dps->total, contours->total );

for ( i = 0; i < dps->total; i++)
{
idx = *(int *) cvGetSeqElem(dps, i);
p = *(CvPoint *) cvGetSeqElem(contours, idx);
cvDrawCircle( img, p , 1, cvScalarAll(255) );
printf("%d %d %d\n", idx, p.x, p.y);
}
cvDrawContours(img, contours, cvScalarAll(100), cvScalarAll(200), 100 );


cvNamedWindow( "contours" );
cvShowImage( "contours", img );

cvWaitKey(0);
cvReleaseMemStorage( &storage_ct );
cvReleaseMemStorage( &storage_dp );
cvReleaseImage( &img );

return 0;
}

posted on 2019-08-15 15:31  胭脂雪  阅读(153)  评论(0)    收藏  举报