進行輪廓提取時要將原圖二值化,因爲除了圖像的輪廓外其餘的都是無用的信息,以減少運算量
所用函數基礎介紹:
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;
}