cvFindContours 获取轮廓中的所有点

利用cvFindContours 函数计算图像的轮廓,遍历轮廓中的每个点

#include 
using namespace std;

#include "cv.h"
#include "highgui.h"
#include "cvaux.h"

//添加宏定义 使linux和xp下兼容
using namespace std;
#pragma  comment(lib,"cxcore.lib")
#pragma  comment(lib,"cv.lib")
#pragma  comment(lib,"cvaux.lib")
#pragma  comment(lib,"highgui.lib")

void getFiles( string path, vector& files )
{
	//文件句柄
	long   hFile   =   0;
	//文件信息
	struct _finddata_t fileinfo;
	string p;
	if((hFile = _findfirst(p.assign(path).append("\\*").c_str(),&fileinfo)) !=  -1)
	{
		do
		{
			//如果是目录,迭代之
			//如果不是,加入列表
			if((fileinfo.attrib &  _A_SUBDIR))
			{
				if(strcmp(fileinfo.name,".") != 0  &&  strcmp(fileinfo.name,"..") != 0)
					getFiles( p.assign(path).append("\\").append(fileinfo.name), files );
			}
			else
			{
				files.push_back(p.assign(path).append("\\").append(fileinfo.name) );
			}
		}while(_findnext(hFile, &fileinfo)  == 0);
		_findclose(hFile);
	}
}
void main()
{
	char * filename = "E:\\ sample";
	char * filesave = "E:\\ contour";
	vector files;

	////获取该路径下的所有文件
	getFiles(filename, files );

	IplImage * image =0;
	IplImage * gray = 0;
	IplImage * bwImage = 0;
	IplImage * contour_image = 0;

	int horizontal,vertical;

	char str[1024];
	int size = files.size();

	//计算轮廓	
	for (int i = 0;i < size;i++)
	{
		CvMemStorage * storage = cvCreateMemStorage(0);
		CvSeq * contours = 0;
		CvSeq * contours_tmp = 0;
		cout<h_next){
			for(int j = 0; j < contours_tmp -> total; j++)    // 提取一个轮廓的所有坐标点
			{
				CvPoint *pt = (CvPoint*) cvGetSeqElem(contours_tmp, j);   // 得到一个轮廓中一个点的函数cvGetSeqElem		
				cvSet2D(image, pt->y, pt->x, cvScalar(0,0,255,0));
			}
			CvSeq *InterCon = contours_tmp->v_next;     // 访问每个轮廓的纵向轮廓
			for(; InterCon != 0; InterCon = InterCon ->h_next)
			{
				for(int j = 0; j < InterCon->total; j++ )
				{
					CvPoint *pt = (CvPoint*)cvGetSeqElem(InterCon, j);
					cvSet2D(image, pt->y, pt->x, cvScalar(0, 255, 0, 0));
				}
			}
		}
		cvSaveImage(str,image);

		cvReleaseMemStorage(&storage);
		cvReleaseImage(&gray);
		cvReleaseImage(&image);
		cvReleaseImage(&bwImage);
		cvReleaseImage(&contour_image);
	}
	
}


cvFindContours 获取轮廓中的所有点_第1张图片


你可能感兴趣的:(图像处理)