?? edgecontour.cpp
字號(hào):
// ************************************************************************
// 文件名:edgecontour.cpp
//
// 圖像邊緣與輪廓運(yùn)算API函數(shù)庫(kù):
//
// RobertDIB() - robert邊緣檢測(cè)運(yùn)算
// SobelDIB() - sobel邊緣檢測(cè)運(yùn)算
// PrewittDIB() - prewitt邊緣檢測(cè)運(yùn)算
// KirschDIB() - kirsch邊緣檢測(cè)運(yùn)算
// GaussDIB() - gauss邊緣檢測(cè)運(yùn)算
// HoughDIB() - 利用Hough變換檢測(cè)平行直線
// ContourDIB() - 輪廓提取
// TraceDIB() - 輪廓跟蹤
// FillDIB() - 種子填充算法1
// Fill2DIB() - 種子填充算法2
// 添加水平和垂直邊緣檢測(cè)
// spDIB() - 水平
// czDIB() - 垂直
//
// ************************************************************************
#include "stdafx.h"
#include "edgecontour.h"
#include "TemplateTrans.h"
#include "DIBAPI.h"
#include <math.h>
#include <direct.h>
/*************************************************************************
*
* 函數(shù)名稱:
* RobertDIB()
*
* 參數(shù):
* LPSTR lpDIBBits - 指向源DIB圖像指針
* LONG lWidth - 源圖像寬度(象素?cái)?shù),必須是4的倍數(shù))
* LONG lHeight - 源圖像高度(象素?cái)?shù))
* 返回值:
* BOOL - 邊緣檢測(cè)成功返回TRUE,否則返回FALSE。
*
* 說明:
* 該函數(shù)用Robert邊緣檢測(cè)算子對(duì)圖像進(jìn)行邊緣檢測(cè)運(yùn)算。
*
* 要求目標(biāo)圖像為灰度圖像。
************************************************************************/
BOOL WINAPI RobertDIB(LPSTR lpDIBBits, LONG lWidth, LONG lHeight)
{
// 指向源圖像的指針
LPSTR lpSrc;
// 指向緩存圖像的指針
LPSTR lpDst;
// 指向緩存DIB圖像的指針
LPSTR lpNewDIBBits;
HLOCAL hNewDIBBits;
//循環(huán)變量
long i;
long j;
//像素值
double result;
unsigned char pixel[4];
// 暫時(shí)分配內(nèi)存,以保存新圖像
hNewDIBBits = LocalAlloc(LHND, lWidth * lHeight);
if (hNewDIBBits == NULL)
{
// 分配內(nèi)存失敗
return FALSE;
}
// 鎖定內(nèi)存
lpNewDIBBits = (char * )LocalLock(hNewDIBBits);
// 初始化新分配的內(nèi)存,設(shè)定初始值為255
lpDst = (char *)lpNewDIBBits;
memset(lpDst, (BYTE)255, lWidth * lHeight);
//使用水平方向的結(jié)構(gòu)元素進(jìn)行腐蝕
for(j = lHeight-1; j > 0; j--)
{
for(i = 0;i <lWidth-1; i++)
{
//由于使用2×2的模板,為防止越界,所以不處理最下邊和最右邊的兩列像素
// 指向源圖像第j行,第i個(gè)象素的指針
lpSrc = (char *)lpDIBBits + lWidth * j + i;
// 指向目標(biāo)圖像第j行,第i個(gè)象素的指針
lpDst = (char *)lpNewDIBBits + lWidth * j + i;
//取得當(dāng)前指針處2*2區(qū)域的像素值,注意要轉(zhuǎn)換為unsigned char型
pixel[0] = (unsigned char)*lpSrc;
pixel[1] = (unsigned char)*(lpSrc + 1);
pixel[2] = (unsigned char)*(lpSrc - lWidth);
pixel[3] = (unsigned char)*(lpSrc - lWidth + 1);
//計(jì)算目標(biāo)圖像中的當(dāng)前點(diǎn)
result = sqrt(( pixel[0] - pixel[3] )*( pixel[0] - pixel[3] ) + \
( pixel[1] - pixel[2] )*( pixel[1] - pixel[2] ));
*lpDst = (unsigned char)result;
}
}
// 復(fù)制腐蝕后的圖像
memcpy(lpDIBBits, lpNewDIBBits, lWidth * lHeight);
// 釋放內(nèi)存
LocalUnlock(hNewDIBBits);
LocalFree(hNewDIBBits);
// 返回
return TRUE;
}
/*************************************************************************
*
* 函數(shù)名稱:
* SobelDIB()
*
* 參數(shù):
* LPSTR lpDIBBits - 指向源DIB圖像指針
* LONG lWidth - 源圖像寬度(象素?cái)?shù),必須是4的倍數(shù))
* LONG lHeight - 源圖像高度(象素?cái)?shù))
* 返回值:
* BOOL - 邊緣檢測(cè)成功返回TRUE,否則返回FALSE。
*
* 說明:
* 該函數(shù)用Sobel邊緣檢測(cè)算子對(duì)圖像進(jìn)行邊緣檢測(cè)運(yùn)算。
*
* 要求目標(biāo)圖像為灰度圖像。
************************************************************************/
BOOL WINAPI SobelDIB(LPSTR lpDIBBits, LONG lWidth, LONG lHeight)
{
// 指向緩存圖像的指針
LPSTR lpDst1;
LPSTR lpDst2;
// 指向緩存DIB圖像的指針
LPSTR lpNewDIBBits1;
HLOCAL hNewDIBBits1;
LPSTR lpNewDIBBits2;
HLOCAL hNewDIBBits2;
//循環(huán)變量
long i;
long j;
// 模板高度
int iTempH;
// 模板寬度
int iTempW;
// 模板系數(shù)
FLOAT fTempC;
// 模板中心元素X坐標(biāo)
int iTempMX;
// 模板中心元素Y坐標(biāo)
int iTempMY;
//模板數(shù)組
FLOAT aTemplate[9];
// 暫時(shí)分配內(nèi)存,以保存新圖像
hNewDIBBits1 = LocalAlloc(LHND, lWidth * lHeight);
if (hNewDIBBits1 == NULL)
{
// 分配內(nèi)存失敗
return FALSE;
}
// 鎖定內(nèi)存
lpNewDIBBits1 = (char * )LocalLock(hNewDIBBits1);
// 暫時(shí)分配內(nèi)存,以保存新圖像
hNewDIBBits2 = LocalAlloc(LHND, lWidth * lHeight);
if (hNewDIBBits2 == NULL)
{
// 分配內(nèi)存失敗
return FALSE;
}
// 鎖定內(nèi)存
lpNewDIBBits2 = (char * )LocalLock(hNewDIBBits2);
// 拷貝源圖像到緩存圖像中
lpDst1 = (char *)lpNewDIBBits1;
memcpy(lpNewDIBBits1, lpDIBBits, lWidth * lHeight);
lpDst2 = (char *)lpNewDIBBits2;
memcpy(lpNewDIBBits2, lpDIBBits, lWidth * lHeight);
// 設(shè)置Sobel模板參數(shù)
iTempW = 3;
iTempH = 3;
fTempC = 1.0;
iTempMX = 1;
iTempMY = 1;
aTemplate[0] = -1.0;
aTemplate[1] = -2.0;
aTemplate[2] = -1.0;
aTemplate[3] = 0.0;
aTemplate[4] = 0.0;
aTemplate[5] = 0.0;
aTemplate[6] = 1.0;
aTemplate[7] = 2.0;
aTemplate[8] = 1.0;
// 調(diào)用Template()函數(shù)
if (!Template(lpNewDIBBits1, lWidth, lHeight,
iTempH, iTempW, iTempMX, iTempMY, aTemplate, fTempC))
{
return FALSE;
}
// 設(shè)置Sobel模板參數(shù)
aTemplate[0] = -1.0;
aTemplate[1] = 0.0;
aTemplate[2] = 1.0;
aTemplate[3] = -2.0;
aTemplate[4] = 0.0;
aTemplate[5] = 2.0;
aTemplate[6] = -1.0;
aTemplate[7] = 0.0;
aTemplate[8] = 1.0;
// 調(diào)用Template()函數(shù)
if (!Template(lpNewDIBBits2, lWidth, lHeight,
iTempH, iTempW, iTempMX, iTempMY, aTemplate, fTempC))
{
return FALSE;
}
//求兩幅緩存圖像的最大值
for(j = 0; j <lHeight; j++)
{
for(i = 0;i <lWidth-1; i++)
{
// 指向緩存圖像1倒數(shù)第j行,第i個(gè)象素的指針
lpDst1 = (char *)lpNewDIBBits1 + lWidth * j + i;
// 指向緩存圖像2倒數(shù)第j行,第i個(gè)象素的指針
lpDst2 = (char *)lpNewDIBBits2 + lWidth * j + i;
if(*lpDst2 > *lpDst1)
*lpDst1 = *lpDst2;
}
}
// 復(fù)制經(jīng)過模板運(yùn)算后的圖像到源圖像
memcpy(lpDIBBits, lpNewDIBBits1, lWidth * lHeight);
// 釋放內(nèi)存
LocalUnlock(hNewDIBBits1);
LocalFree(hNewDIBBits1);
LocalUnlock(hNewDIBBits2);
LocalFree(hNewDIBBits2);
// 返回
return TRUE;
}
/*************************************************************************
*
* 函數(shù)名稱:
* PrewittDIB()
*
* 參數(shù):
* LPSTR lpDIBBits - 指向源DIB圖像指針
* LONG lWidth - 源圖像寬度(象素?cái)?shù),必須是4的倍數(shù))
* LONG lHeight - 源圖像高度(象素?cái)?shù))
* 返回值:
* BOOL - 邊緣檢測(cè)成功返回TRUE,否則返回FALSE。
*
* 說明:
* 該函數(shù)用Prewitt邊緣檢測(cè)算子對(duì)圖像進(jìn)行邊緣檢測(cè)運(yùn)算。
*
* 要求目標(biāo)圖像為灰度圖像。
************************************************************************/
BOOL WINAPI PrewittDIB(LPSTR lpDIBBits, LONG lWidth, LONG lHeight)
{
// 指向緩存圖像的指針
LPSTR lpDst1;
LPSTR lpDst2;
// 指向緩存DIB圖像的指針
LPSTR lpNewDIBBits1;
HLOCAL hNewDIBBits1;
LPSTR lpNewDIBBits2;
HLOCAL hNewDIBBits2;
//循環(huán)變量
long i;
long j;
// 模板高度
int iTempH;
// 模板寬度
int iTempW;
// 模板系數(shù)
FLOAT fTempC;
// 模板中心元素X坐標(biāo)
int iTempMX;
// 模板中心元素Y坐標(biāo)
int iTempMY;
//模板數(shù)組
FLOAT aTemplate[9];
// 暫時(shí)分配內(nèi)存,以保存新圖像
hNewDIBBits1 = LocalAlloc(LHND, lWidth * lHeight);
if (hNewDIBBits1 == NULL)
{
// 分配內(nèi)存失敗
return FALSE;
}
// 鎖定內(nèi)存
lpNewDIBBits1 = (char * )LocalLock(hNewDIBBits1);
// 暫時(shí)分配內(nèi)存,以保存新圖像
hNewDIBBits2 = LocalAlloc(LHND, lWidth * lHeight);
if (hNewDIBBits2 == NULL)
{
// 分配內(nèi)存失敗
return FALSE;
}
// 鎖定內(nèi)存
lpNewDIBBits2 = (char * )LocalLock(hNewDIBBits2);
// 拷貝源圖像到緩存圖像中
lpDst1 = (char *)lpNewDIBBits1;
memcpy(lpNewDIBBits1, lpDIBBits, lWidth * lHeight);
lpDst2 = (char *)lpNewDIBBits2;
memcpy(lpNewDIBBits2, lpDIBBits, lWidth * lHeight);
// 設(shè)置Prewitt模板參數(shù)
iTempW = 3;
iTempH = 3;
fTempC = 1.0;
iTempMX = 1;
iTempMY = 1;
aTemplate[0] = -1.0;
aTemplate[1] = -1.0;
aTemplate[2] = -1.0;
aTemplate[3] = 0.0;
aTemplate[4] = 0.0;
aTemplate[5] = 0.0;
aTemplate[6] = 1.0;
aTemplate[7] = 1.0;
aTemplate[8] = 1.0;
// 調(diào)用Template()函數(shù)
if (!Template(lpNewDIBBits1, lWidth, lHeight,
iTempH, iTempW, iTempMX, iTempMY, aTemplate, fTempC))
{
return FALSE;
}
// 設(shè)置Prewitt模板參數(shù)
aTemplate[0] = 1.0;
aTemplate[1] = 0.0;
aTemplate[2] = -1.0;
aTemplate[3] = 1.0;
aTemplate[4] = 0.0;
aTemplate[5] = -1.0;
aTemplate[6] = 1.0;
aTemplate[7] = 0.0;
aTemplate[8] = -1.0;
// 調(diào)用Template()函數(shù)
if (!Template(lpNewDIBBits2, lWidth, lHeight,
iTempH, iTempW, iTempMX, iTempMY, aTemplate, fTempC))
{
return FALSE;
}
//求兩幅緩存圖像的最大值
for(j = 0; j <lHeight; j++)
{
for(i = 0;i <lWidth-1; i++)
{
// 指向緩存圖像1倒數(shù)第j行,第i個(gè)象素的指針
lpDst1 = (char *)lpNewDIBBits1 + lWidth * j + i;
// 指向緩存圖像2倒數(shù)第j行,第i個(gè)象素的指針
lpDst2 = (char *)lpNewDIBBits2 + lWidth * j + i;
if(*lpDst2 > *lpDst1)
*lpDst1 = *lpDst2;
}
}
// 復(fù)制經(jīng)過模板運(yùn)算后的圖像到源圖像
memcpy(lpDIBBits, lpNewDIBBits1, lWidth * lHeight);
// 釋放內(nèi)存
LocalUnlock(hNewDIBBits1);
LocalFree(hNewDIBBits1);
LocalUnlock(hNewDIBBits2);
LocalFree(hNewDIBBits2);
// 返回
return TRUE;
}
/*************************************************************************
*
* 函數(shù)名稱:
* KirschDIB()
*
* 參數(shù):
* LPSTR lpDIBBits - 指向源DIB圖像指針
* LONG lWidth - 源圖像寬度(象素?cái)?shù),必須是4的倍數(shù))
* LONG lHeight - 源圖像高度(象素?cái)?shù))
* 返回值:
* BOOL - 邊緣檢測(cè)成功返回TRUE,否則返回FALSE。
*
* 說明:
* 該函數(shù)用kirsch邊緣檢測(cè)算子對(duì)圖像進(jìn)行邊緣檢測(cè)運(yùn)算。
*
* 要求目標(biāo)圖像為灰度圖像。
************************************************************************/
BOOL WINAPI KirschDIB(LPSTR lpDIBBits, LONG lWidth, LONG lHeight)
{
// 指向緩存圖像的指針
LPSTR lpDst1;
LPSTR lpDst2;
// 指向緩存DIB圖像的指針
LPSTR lpNewDIBBits1;
HLOCAL hNewDIBBits1;
LPSTR lpNewDIBBits2;
HLOCAL hNewDIBBits2;
//循環(huán)變量
long i;
long j;
// 模板高度
int iTempH;
// 模板寬度
int iTempW;
// 模板系數(shù)
FLOAT fTempC;
// 模板中心元素X坐標(biāo)
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -