《学习OPENCV》一书中的部分函数使用:
#include "cv.h"
#include "highgui.h"
#include "cxcore.h"
#include <stdio.h>
int thresh_value = 100;
IplImage* src_image = NULL;
IplImage* gray_src_image = NULL;
IplImage* dst_image = NULL;
IplImage* gray_dst_image = NULL;
CvMemStorage* storge = NULL;
void on_trackbar(int pos); //define the function of trackbarcallback
int main(int agrc, char** agrv)
{
src_image = cvLoadImage("pic3.png");
dst_image = cvCloneImage(src_image);
gray_src_image = cvCreateImage(cvGetSize(src_image), IPL_DEPTH_8U, 1);
gray_dst_image = cvCloneImage(gray_src_image);
//
cvCvtColor(src_image, gray_src_image, CV_RGB2GRAY); //convert to gray image
cvNamedWindow("src", 1);
cvNamedWindow("graysrc", 1);
cvShowImage("src", src_image);
cvShowImage("graysrc", gray_src_image);
//
//
//1、find contours
/*
cvCreateTrackbar("contours", "src", &thresh_value, 255, on_trackbar);
on_trackbar(0);
*/
//
//
/*
CvSeq* contours = NULL;
storge = cvCreateMemStorage(0);
cvThreshold(gray_src_image, gray_src_image, 100, 255, CV_THRESH_BINARY);
int NC = cvFindContours(gray_src_image, storge, &contours, sizeof(CvContour), CV_RETR_LIST);
cvDrawContours(gray_src_image, contours, cvScalar(0), cvScalar(0), 0, 5);
printf("%d\n", NC);
cvShowImage("graysrc", gray_src_image);
*/
//
//p291.3
IplImage* draw_circle = cvCreateImage(cvSize(512, 512), IPL_DEPTH_32F, 3);
cvSet(draw_circle, cvScalar(255, 255, 255));
storge = cvCreateMemStorage(0);
CvSeq* circle = cvCreateSeq(0, sizeof(CvSeq), sizeof(CvPoint3D32f), storge);
float temp[] = {300, 300, 200};
for (int i = 0; i != 3; ++i)
{
cvSeqPush(circle, &temp[i]);
}
float* ptr = (float*) cvGetSeqElem(circle, 0);
cvDrawCircle(draw_circle, cvPoint(cvRound(ptr[0]), cvRound(ptr[1])), cvRound(ptr[2]), cvScalar(0, 0, 255));
cvNamedWindow("circle", 1);
cvShowImage("circle", draw_circle);
// cvReleaseImage(&src_image);
// cvReleaseImage(&dst_image);
// cvReleaseImage(&gray_src_image);
// cvReleaseImage(&gray_dst_image);
int c = cvWaitKey(0);
if (c == 27)
{
return -1;
}
return 0;
}
//
//trackbarcallback function
/*
void on_trackbar(int pos)
{
if (storge == NULL)
{
storge = cvCreateMemStorage(0);
}
else
{
cvClearMemStorage(storge);
}
CvSeq* contours = 0;
cvCvtColor(src_image, gray_src_image, CV_RGB2GRAY);
cvThreshold(gray_src_image, gray_src_image, thresh_value, 255, CV_THRESH_BINARY); //set a threshold for the image
cvNamedWindow("threshold", 1);
cvNamedWindow("contours", 1);
cvShowImage("threshold", gray_src_image);
cvFindContours(gray_src_image, storge, &contours); //find the contours
cvZero(gray_src_image);
CvSeq* temp = contours;
// while (temp)
// {
cvDrawContours(gray_src_image, contours, cvScalar(255), cvScalar(255), 100); //draw the contours
cvShowImage("contours", gray_src_image);
// temp = temp->h_next;
// }
/* CvMemStorage* storge2 = cvCreateMemStorage(0);
CvSeq* poly = NULL;
poly = cvApproxPoly(temp, sizeof(CvContour), storge2, CV_POLY_APPROX_DP, 2, 1);
cvDrawContours(gray_src_image, poly, cvScalar(255), cvScalar(255), 100);
cvNamedWindow("poly", 1);
cvShowImage("poly", gray_src_image);
double arclength = cvContourPerimeter(temp);
printf("arclength = %f\n", arclength);
*//*/
CvPoint2D32f center;
float radius = 0;
cvMinEnclosingCircle(temp, ¢er, &radius); //find the circle
cvDrawCircle(gray_src_image, cvPoint(cvRound(center.x), cvRound(center.y)), radius, cvScalar(255), 2);
cvNamedWindow("circle", 1);
cvShowImage("circle", gray_src_image);
if (contours->total >= 6)
{
CvBox2D box = cvFitEllipse2(contours);
cvEllipseBox(gray_src_image, box, cvScalar(255), 2); //find the ellipse
cvNamedWindow("box", 1);
cvShowImage("box", gray_src_image);
}
*
}
*/