?? edgecontour.cpp
字號(hà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è)置Kirsch模板1參數(shù)
iTempW = 3;
iTempH = 3;
fTempC = 1.0;
iTempMX = 1;
iTempMY = 1;
aTemplate[0] = 5.0;
aTemplate[1] = 5.0;
aTemplate[2] = 5.0;
aTemplate[3] = -3.0;
aTemplate[4] = 0.0;
aTemplate[5] = -3.0;
aTemplate[6] = -3.0;
aTemplate[7] = -3.0;
aTemplate[8] = -3.0;
// 調(diào)用Template()函數(shù)
if (!Template(lpNewDIBBits1, lWidth, lHeight,
iTempH, iTempW, iTempMX, iTempMY, aTemplate, fTempC))
{
return FALSE;
}
// 設(shè)置Kirsch模板2參數(shù)
aTemplate[0] = -3.0;
aTemplate[1] = 5.0;
aTemplate[2] = 5.0;
aTemplate[3] = -3.0;
aTemplate[4] = 0.0;
aTemplate[5] = 5.0;
aTemplate[6] = -3.0;
aTemplate[7] = -3.0;
aTemplate[8] = -3.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;
}
}
// 拷貝源圖像到緩存圖像中
memcpy(lpNewDIBBits2, lpDIBBits, lWidth * lHeight);
// 設(shè)置Kirsch模板3參數(shù)
aTemplate[0] = -3.0;
aTemplate[1] = -3.0;
aTemplate[2] = 5.0;
aTemplate[3] = -3.0;
aTemplate[4] = 0.0;
aTemplate[5] = 5.0;
aTemplate[6] = -3.0;
aTemplate[7] = -3.0;
aTemplate[8] = 5.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;
}
}
// 拷貝源圖像到緩存圖像中
memcpy(lpNewDIBBits2, lpDIBBits, lWidth * lHeight);
// 設(shè)置Kirsch模板4參數(shù)
aTemplate[0] = -3.0;
aTemplate[1] = -3.0;
aTemplate[2] = -3.0;
aTemplate[3] = -3.0;
aTemplate[4] = 0.0;
aTemplate[5] = 5.0;
aTemplate[6] = -3.0;
aTemplate[7] = 5.0;
aTemplate[8] = 5.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;
}
}
// 拷貝源圖像到緩存圖像中
memcpy(lpNewDIBBits2, lpDIBBits, lWidth * lHeight);
// 設(shè)置Kirsch模板5參數(shù)
aTemplate[0] = -3.0;
aTemplate[1] = -3.0;
aTemplate[2] = -3.0;
aTemplate[3] = -3.0;
aTemplate[4] = 0.0;
aTemplate[5] = -3.0;
aTemplate[6] = 5.0;
aTemplate[7] = 5.0;
aTemplate[8] = 5.0;
// 調(diào)用Template()函數(shù)
if (!Template(lpNewDIBBits2, lWidth, lHeight,
iTempH, iTempW, iTempMX, iTempMY, aTemplate, fTempC))
{
return FALSE;
}
// 拷貝源圖像到緩存圖像中
memcpy(lpNewDIBBits2, lpDIBBits, lWidth * lHeight);
//求兩幅緩存圖像的最大值
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;
}
}
// 拷貝源圖像到緩存圖像中
memcpy(lpNewDIBBits2, lpDIBBits, lWidth * lHeight);
// 設(shè)置Kirsch模板6參數(shù)
aTemplate[0] = -3.0;
aTemplate[1] = -3.0;
aTemplate[2] = -3.0;
aTemplate[3] = 5.0;
aTemplate[4] = 0.0;
aTemplate[5] = -3.0;
aTemplate[6] = 5.0;
aTemplate[7] = 5.0;
aTemplate[8] = -3.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;
}
}
// 拷貝源圖像到緩存圖像中
memcpy(lpNewDIBBits2, lpDIBBits, lWidth * lHeight);
// 設(shè)置Kirsch模板7參數(shù)
aTemplate[0] = 5.0;
aTemplate[1] = -3.0;
aTemplate[2] = -3.0;
aTemplate[3] = 5.0;
aTemplate[4] = 0.0;
aTemplate[5] = -3.0;
aTemplate[6] = 5.0;
aTemplate[7] = -3.0;
aTemplate[8] = -3.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;
}
}
// 拷貝源圖像到緩存圖像中
memcpy(lpNewDIBBits2, lpDIBBits, lWidth * lHeight);
// 設(shè)置Kirsch模板8參數(shù)
aTemplate[0] = 5.0;
aTemplate[1] = 5.0;
aTemplate[2] = -3.0;
aTemplate[3] = 5.0;
aTemplate[4] = 0.0;
aTemplate[5] = -3.0;
aTemplate[6] = -3.0;
aTemplate[7] = -3.0;
aTemplate[8] = -3.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)過(guò)模板運(yùn)算后的圖像到源圖像
memcpy(lpDIBBits, lpNewDIBBits1, lWidth * lHeight);
// 釋放內(nèi)存
LocalUnlock(hNewDIBBits1);
LocalFree(hNewDIBBits1);
LocalUnlock(hNewDIBBits2);
LocalFree(hNewDIBBits2);
// 返回
return TRUE;
}
/*************************************************************************
*
* 函數(shù)名稱:
* GaussDIB()
*
* 參數(shù):
* LPSTR lpDIBBits - 指向源DIB圖像指針
* LONG lWidth - 源圖像寬度(象素?cái)?shù),必須是4的倍數(shù))
* LONG lHeight - 源圖像高度(象素?cái)?shù))
* 返回值:
* BOOL - 邊緣檢測(cè)成功返回TRUE,否則返回FALSE。
*
* 說(shuō)明:
* 該函數(shù)用高斯拉普拉斯邊緣檢測(cè)算子對(duì)圖像進(jìn)行邊緣檢測(cè)運(yùn)算。
*
* 要求目標(biāo)圖像為灰度圖像。
************************************************************************/
BOOL WINAPI GaussDIB(LPSTR lpDIBBits, LONG lWidth, LONG lHeight)
{
// 指向緩存圖像的指針
LPSTR lpDst1;
LPSTR lpDst2;
// 指向緩存DIB圖像的指針
LPSTR lpNewDIBBits1;
HLOCAL hNewDIBBits1;
LPSTR lpNewDIBBits2;
HLOCAL hNewDIBBits2;
// 模板高度
int iTempH;
// 模板寬度
int iTempW;
// 模板系數(shù)
FLOAT fTempC;
// 模板中心元素X坐標(biāo)
int iTempMX;
// 模板中心元素Y坐標(biāo)
int iTempMY;
//模板數(shù)組
FLOAT aTemplate[25];
// 暫時(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è)置Gauss模板參數(shù)
iTempW = 5;
iTempH = 5;
fTempC = 1.0;
iTempMX = 3;
iTempMY = 3;
aTemplate[0] = -2.0;
aTemplate[1] = -4.0;
aTemplate[2] = -4.0;
aTemplate[3] = -4.0;
aTemplate[4] = -2.0;
aTemplate[5] = -4.0;
aTemplate[6] = 0.0;
aTemplate[7] = 8.0;
aTemplate[8] = 0.0;
aTemplate[9] = -4.0;
aTemplate[10] = -4.0;
aTemplate[11] = 8.0;
aTemplate[12] = 24.0;
aTemplate[13] = 8.0;
aTemplate[14] = -4.0;
aTemplate[15] = -4.0;
aTemplate[16] = 0.0;
aTemplate[17] = 8.0;
aTemplate[18] = 0.0;
aTemplate[19] = -4.0;
aTemplate[20] = -2.0;
aTemplate[21] = -4.0;
aTemplate[22] = -4.0;
aTemplate[23] = -4.0;
aTemplate[24] = -2.0;
// 調(diào)用Template()函數(shù)
if (!Template(lpNewDIBBits1, lWidth, lHeight,
iTempH, iTempW, iTempMX, iTempMY, aTemplate, fTempC))
{
return FALSE;
}
// 復(fù)制經(jīng)過(guò)模板運(yùn)算后的圖像到源圖像
memcpy(lpDIBBits, lpNewDIBBits1, lWidth * lHeight);
// 釋放內(nèi)存
LocalUnlock(hNewDIBBits1);
LocalFree(hNewDIBBits1);
LocalUnlock(hNewDIBBits2);
LocalFree(hNewDIBBits2);
// 返回
return TRUE;
}
/*************************************************************************
*
* 函數(shù)名稱:
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -