进行轮廓提取时要将原图二值化,因为除了图像的轮廓外其余的都是无用的信息,以减少运算量
所用函数基础介绍:
CvMemStorage
- CvMemStorage
- typedef struct CvMemStorage
- {
- struct CvMemBlock* bottom;/* first allocated block */
- struct CvMemBlock* top; /* the current memory block - top of the stack */
- struct CvMemStorage* parent; /* borrows new blocks from */
- int block_size; /* block size */
- int free_space; /* free space in the top block (in bytes) */
- } CvMemStorage;
创建内存块
- CvMemStorage* cvCreateMemStorage( int block_size=0 );
函数 cvCreateMemStorage 创建一内存块并返回指向块首的指针。起初,存储块是空的。头部(即:header)的所有域值都为 0,除了 block_size 外.
cvReleaseMemStorage
释放内存块
- void cvReleaseMemStorage( CvMemStorage** storage );
cvThreshold:
作用:函数 cvThreshold 对单通道数组应用固定阈值操作。该函数的典型应用是对灰度图像进行阈值操作得到二值图像。(cvCmpS 也可以达到此目的) 或者是去掉噪声,例如过滤很小或很大象素值的图像点。本函数支持的对图像取阈值的方法由 threshold_type 确定。
形式:void cvThreshold( const CvArr* src, CvArr* dst, double threshold, double max_value, int threshold_type );
src:原始数组 (单通道 , 8-bit of 32-bit 浮点数)。dst:输出数组,必须与 src 的类型一致,或者为 8-bit。
threshold:阈值
max_value:使用 CV_THRESH_BINARY 和 CV_THRESH_BINARY_INV 的最大值。
threshold_type:阈值类型 threshold_type=CV_THRESH_BINARY:
如果 src(x,y)>threshold 0,dst(x,y) = max_value, 否则.
threshold_type=CV_THRESH_BINARY_INV:
如果 src(x,y)>threshold,dst(x,y) = 0; 否则,dst(x,y) = max_value.
threshold_typ
本函数支持的对图像取阈值的方法由 threshold_type 确定:
threshold_type=CV_THRESH_BINARY:
dst(x,y) = max_value, if src(x,y)>threshold 0, otherwise.
threshold_type=CV_THRESH_BINARY_INV:
dst(x,y) = 0, if src(x,y)>threshold; dst(x,y) = max_value, otherwise.
threshold_type=CV_THRESH_TRUNC:
dst(x,y) = threshold, if src(x,y)>threshold; dst(x,y) = src(x,y), otherwise.
threshold_type=CV_THRESH_TOZERO:
dst(x,y) = src(x,y), if (x,y)>threshold ; dst(x,y) = 0, otherwise.
threshold_type=CV_THRESH_TOZERO_INV:
dst(x,y) = 0, if src(x,y)>threshold ; dst(x,y) = src(x,y), otherwise.
源代码:
#include <iostream>
#include "cv.h"
#include "cxcore.h"
#include "highgui.h"
using namespace std;
int main()
{
CvMemStorage *storage = cvCreateMemStorage(0); // 内存存储序列
IplImage *img = cvLoadImage("F:\\bb2.jpg", 0);
IplImage *imgColor = cvCreateImage(cvGetSize(img), 8, 3);
IplImage *contoursImage = cvCreateImage(cvGetSize(img), 8, 1);
CvSeq *contours = 0, *contoursTemp = 0; //OPENCV中的一种数据结构,类似于数组
cvZero(contoursImage);
cvThreshold(img, img, 100, 255, CV_THRESH_BINARY); // 二值化操作,原图已经变为灰度图
cvCvtColor(img, imgColor, CV_GRAY2BGR);
int totals = cvFindContours(img, storage,&contours, sizeof(CvContour), //img必须是一个二值图像 storage 用来存储的contours指向存储的第一个轮廓
CV_RETR_CCOMP, CV_CHAIN_APPROX_NONE, cvPoint(0,0));
contoursTemp = contours;
int count = 0;
int i;
for(;contoursTemp != 0; contoursTemp = contoursTemp -> h_next) /// 这样可以访问每一个轮廓 ====横向轮廓
{
for(i = 0; i < contoursTemp -> total; i++) // 提取一个轮廓的所有坐标点
{
CvPoint *pt = (CvPoint*) cvGetSeqElem(contoursTemp, i); // 得到一个轮廓中一个点的函数cvGetSeqElem
cvSetReal2D(contoursImage, pt->y, pt->x, 255.0);
cvSet2D(imgColor, pt->y, pt->x, cvScalar(0,0,255,0));
}
count ++;
CvSeq *InterCon = contoursTemp->v_next; // 访问每个轮廓的纵向轮廓
for(; InterCon != 0; InterCon = InterCon ->h_next)
{
for(i = 0; i < InterCon->total; i++ )
{
CvPoint *pt = (CvPoint*)cvGetSeqElem(InterCon, i);
cvSetReal2D(contoursImage, pt->y, pt->x, 255.0);
cvSet2D(imgColor, pt->y, pt->x, cvScalar(0, 255, 0, 0));
}
}
}
cvNamedWindow("contoursImage");
cvShowImage("contoursImage", contoursImage);
cvNamedWindow("imgColor");
cvShowImage("imgColor",imgColor);
cvWaitKey(0);
cvReleaseMemStorage(&storage); // 也要释放内存序列空间
cvReleaseImage(&contoursImage);
cvReleaseImage(&imgColor);
cvDestroyWindow("contoursImage");
cvDestroyWindow("imgColor");
return 0;
}