基于OpenCV的计算机视觉技术实现.rar
源代码在线查看: 4-9.cpp
// ***************************************************************
// 4-9 version: 1.0 ? date: 09/10/2007
// -------------------------------------------------------------
//
// -------------------------------------------------------------
// Copyright (C) 2007 - All Rights Reserved
// ***************************************************************
//
// ***************************************************************
///*
#include "cv.h"
#include "cxcore.h"
#include "highgui.h"
#include "iostream.h"
void main( )
{
int i=0;
int mode =CV_RETR_EXTERNAL; //提取轮廓的模式
int contours_num=0; //图像中提取的轮廓的数目
CvMemStorage * storage = cvCreateMemStorage(0); //提取轮廓时需要的存储容器
CvSeq * contour = 0; //存储提取轮廓的序列指针
IplImage* bin_img=cvLoadImage("bin_img.jpg",0); //读取待检测轮廓的图像
cvThreshold(bin_img, bin_img,128,255,CV_THRESH_BINARY); //二值化
IplImage* pContourImg = cvCreateImage(cvGetSize(bin_img),IPL_DEPTH_8U,3); //轮廓绘制图
cvNamedWindow( "bin_img",1);
cvShowImage( "bin_img", bin_img ); //显示二值图
//-------------------cvFindContours查找物体轮廓-------------------------
mode = CV_RETR_EXTERNAL; //只提取最外层的轮廓
contours_num=cvFindContours( bin_img, storage, &contour, sizeof(CvContour), mode, CV_CHAIN_APPROX_NONE);
cout
//---------------------------逐点将轮廓画出----------------------
CvSeqReader reader; //读序列
int count =0;
if(contour!=0) //输出组成轮廓的点的数目
{
count = contour->total;
cout }
cvStartReadSeq( contour, &reader, 0 );
CvPoint pt1;
CvScalar color = CV_RGB( 255, 255, 255 );;
cvNamedWindow( "contour",1);
cvShowImage( "contour", pContourImg );
for( i = 0; i < count; i++ ) //逐点将轮廓画出
{
CV_READ_SEQ_ELEM( pt1, reader );
cvCircle(pContourImg,pt1,1,color);
cvShowImage( "contour", pContourImg );
cvWaitKey(5);
}
cvWaitKey(0);
//释放
cvReleaseImage( &bin_img );
cvReleaseImage( &pContourImg );
cvReleaseMemStorage(&storage);
}
//*/