opencv之图像轮廓提取

进行轮廓提取时要将原图二值化,因为除了图像的轮廓外其余的都是无用的信息,以减少运算量

所用函数基础介绍:

CvMemStorage

  1. CvMemStorage   
  2. typedef struct CvMemStorage   
  3. {   
  4. struct CvMemBlock* bottom;/* first allocated block */   
  5. struct CvMemBlock* top; /* the current memory block - top of the stack */   
  6. struct CvMemStorage* parent; /* borrows new blocks from */   
  7. int block_size; /* block size */   
  8. int free_space; /* free space in the top block (in bytes) */   
  9. } CvMemStorage;   
内存存储器是一个可用来存储诸如序列,轮廓,图形,子划分等动态增长数据结构的底层结构。它是由一系列以同等大小的内存块构成,呈列表型 ---bottom 域指的是列首,top 域指的是当前指向的块但未必是列尾.在bottom和top之间所有的块(包括bottom, 不包括top)被完全占据了空间;在 top和列尾之间所有的块(包括块尾,不包括top)则是空的;而top块本身则被占据了部分空间 -- free_space 指的是top块剩余的空字节数。新分配的内存缓冲区(或显示的通过 cvMemStorageAlloc 函数分配,或隐示的通过 cvSeqPush, cvGraphAddEdge等高级函数分配)总是起始于当前块(即top块)的剩余那部分,如果剩余那部分能满足要求(够分配的大小)。分配后,free_space 就减少了新分配的那部分内存大小,外加一些用来保存适当列型的附加大小。当top块的剩余空间无法满足被分配的块(缓冲区)大小时,top块的下一个存储块被置为当前块(新的top块) -- free_space 被置为先前分配的整个块的大小。如果已经不存在空的存储块(即:top块已是列尾),则必须再分配一个新的块(或从parent那继承,见 cvCreateChildMemStorage)并将该块加到列尾上去。于是,存储器(memory storage)就如同栈(Stack)那样, bottom指向栈底,(top, free_space)对指向栈顶。栈顶可通过 cvSaveMemStoragePos保存,通过 cvRestoreMemStoragePos 恢复指向, 通过 cvClearStorage 重置。 
cvCreateMemStorage 
创建内存块 
  1. CvMemStorage* cvCreateMemStorage( int block_size=0 );   
block_size:存储块的大小以字节表示。如果大小是 0 byte, 则将该块设置成默认值 当前默认大小为64k. 
函数 cvCreateMemStorage 创建一内存块并返回指向块首的指针。起初,存储块是空的。头部(即:header)的所有域值都为 0,除了 block_size 外.

cvReleaseMemStorage 
释放内存块 

  1. void cvReleaseMemStorage( CvMemStorage** storage );   

CvSeq* cvCreateSeq(int seq_flags,int header_size,int elem_size,CvMemStorage* storage)
功能:创建一序列
说明:CvSeq本身就是一个可增长的序列,不是固定的序列

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;
}







发布了8 篇原创文章 · 获赞 3 · 访问量 2万+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章