?? edgecontour.cpp
字號:
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);
// 設置Kirsch模板1參數
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;
// 調用Template()函數
if (!Template(lpNewDIBBits1, lWidth, lHeight,
iTempH, iTempW, iTempMX, iTempMY, aTemplate, fTempC))
{
return FALSE;
}
// 設置Kirsch模板2參數
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;
// 調用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(lpNewDIBBits2, lpDIBBits, lWidth * lHeight);
// 設置Kirsch模板3參數
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;
// 調用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(lpNewDIBBits2, lpDIBBits, lWidth * lHeight);
// 設置Kirsch模板4參數
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;
// 調用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(lpNewDIBBits2, lpDIBBits, lWidth * lHeight);
// 設置Kirsch模板5參數
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;
// 調用Template()函數
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倒數第j行,第i個象素的指針
lpDst1 = (char *)lpNewDIBBits1 + lWidth * j + i;
// 指向緩存圖像2倒數第j行,第i個象素的指針
lpDst2 = (char *)lpNewDIBBits2 + lWidth * j + i;
if(*lpDst2 > *lpDst1)
*lpDst1 = *lpDst2;
}
}
// 拷貝源圖像到緩存圖像中
memcpy(lpNewDIBBits2, lpDIBBits, lWidth * lHeight);
// 設置Kirsch模板6參數
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;
// 調用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(lpNewDIBBits2, lpDIBBits, lWidth * lHeight);
// 設置Kirsch模板7參數
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;
// 調用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(lpNewDIBBits2, lpDIBBits, lWidth * lHeight);
// 設置Kirsch模板8參數
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;
// 調用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;
}
/*************************************************************************
*
* 函數名稱:
* GaussDIB()
*
* 參數:
* LPSTR lpDIBBits - 指向源DIB圖像指針
* LONG lWidth - 源圖像寬度(象素數,必須是4的倍數)
* LONG lHeight - 源圖像高度(象素數)
* 返回值:
* BOOL - 邊緣檢測成功返回TRUE,否則返回FALSE。
*
* 說明:
* 該函數用高斯拉普拉斯邊緣檢測算子對圖像進行邊緣檢測運算。
*
* 要求目標圖像為灰度圖像。
************************************************************************/
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;
// 模板系數
FLOAT fTempC;
// 模板中心元素X坐標
int iTempMX;
// 模板中心元素Y坐標
int iTempMY;
//模板數組
FLOAT aTemplate[25];
// 暫時分配內存,以保存新圖像
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);
// 設置Gauss模板參數
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;
// 調用Template()函數
if (!Template(lpNewDIBBits1, lWidth, lHeight,
iTempH, iTempW, iTempMX, iTempMY, aTemplate, fTempC))
{
return FALSE;
}
// 復制經過模板運算后的圖像到源圖像
memcpy(lpDIBBits, lpNewDIBBits1, lWidth * lHeight);
// 釋放內存
LocalUnlock(hNewDIBBits1);
LocalFree(hNewDIBBits1);
LocalUnlock(hNewDIBBits2);
LocalFree(hNewDIBBits2);
// 返回
return TRUE;
}
/*************************************************************************
*
* 函數名稱:
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -