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