資源簡介
這是分水嶺算法的純C實(shí)現(xiàn),針對8位灰度圖做處理。結(jié)果和Matlab自帶分水嶺算法效果類似。
代碼片段和文件信息
#include?“waterthreshold.h“
#include?“malloc.h“
#include?“stdio.h“
#include?“FIFO.H“
#include?“math.h“
#include?“filter.h“
#include?“debug.h“
#define?INIT?-1
#define?MASK?-2
#define?ENFIFOED?-3 //表示該像素標(biāo)號是MASK,并且已經(jīng)被加入到隊(duì)列當(dāng)中了
#define?WSHED?0
#define?FICTITIOUS?0xFFFF
const?QElemType?BREAKPOINT={FICTITIOUSFICTITIOUS};
int?*?PixLabel=0;
PixInfor?*?SortedPixTable=0;
GrayStartPosition?PixGrayStartPosition[256]={0};
#define?PixAmount(x)?(PixGrayStartPosition[x].amount)??//灰度為x的像素的總個(gè)數(shù)
#define?PixStart(x)?(PixGrayStartPosition[x].start)??//SortedPixTable中第一個(gè)灰度為x的像素所處位置
#define?PixSet(x)?(SortedPixTable+PixStart(x))??//灰度為x的像素集合
#define?PixRow(x)?((x).row)
#define?PixColumn(x)?((x).column)
/*******私有變量申明,僅用于本文件內(nèi)私有函數(shù)間參數(shù)傳遞,以減少參數(shù)個(gè)數(shù)*********/
static?Uint16?ImgWidth;
static?Uint16?ImgHeight;
static?FIFO?queue;
static?int?CurLabel;
static?int?ErrorCode;
/*******私有函數(shù)申明,這些函數(shù)僅用于本文件內(nèi)*************************/
static?void?SortPixs(Uint8?*?inImgUint16?WidthUint16?Height);
static?int?GetNumberOfNeighbourWithPositiveLabel(Uint16?rowUint16?column);
static?int?doLabelPix(QElemType?Pix);
static?void?ShowResult(Uint8?*?outImgUint8?*?inImgUint16?WidthUint16?Height);
static?void?WaterThreshold(Uint8?*?inImg?Uint16?WidthUint16?Height);
static?void?WTInit(Uint16?WidthUint16?Height);
static?int?CreateNewBasin(QElemType?CurrentPix);
static?int?Grad(Uint8?*?inImgint?columnint?row);
static?void?QuantizeImage(Uint8?*?inImgUint8?*?outImgUint16?WidthUint16?HeightUint8?Qsize);
static?void?ReverseImage(Uint8?*?inImgUint8?*?outImgUint16?WidthUint16?Height);
void?ReverseImage(Uint8?*?inImgUint8?*?outImgUint16?WidthUint16?Height)
{
int?i=0;
int?imgsize?=?Width*Height;
for(i=0;i {
outImg[i]?=?255-inImg[i];
}
}
void?QuantizeImage(Uint8?*?inImgUint8?*?outImgUint16?WidthUint16?HeightUint8?QSize)
{
int?i=0;
int?imgsize?=?Height*Width;
for(i=0;i {
outImg[i]?=?(inImg[i]/QSize)*QSize;
}
}
void?WTInit(Uint16?WidthUint16?Height)
{
int?*?ptr;
int?ij;
SortedPixTable?=?(PixInfor*)malloc(sizeof(PixInfor)*((Width-2)*(Height-2)));
PixLabel?=?(int*)malloc(sizeof(int)*Width*Height);
ptr?=?PixLabel;
for(i=0;i {
for(j=0;j {
if(?i==0?||?j==0?||?i==Height-1?||?j==Width-1?)
{
*ptr?=?WSHED?;?//將圖像邊緣像素的標(biāo)號設(shè)為WSHED
}
else
{
*ptr?=?INIT;
}
ptr++;
}
}
}
void?WTDeInit()
{
free(SortedPixTable);
free(PixLabel);
}
void?WaterThrdMain(Uint8?*?inImg?Uint8?*?outImgUint16?WidthUint16?Height)
{
WTInit(WidthHeight);
// ReverseImage(inImginImgWidthHeight);
// SmoothImage(inImgWidthHeight);
// FiltNoise(inImgWidthHeight);
// FiltNoise(inImgWidthHeight);
QuantizeImage(inImginImgWidthHeight6);
WaterThreshold(inImg?WidthHeight);
ShowResult(outImginImgWidthHeight);
WTDeInit();
}
void?ShowResult(Uint8?*?outImgUint8?*?inIm
評論
共有 條評論