?? edgecontour.cpp
字號(hào):
* HoughDIB()
*
* 參數(shù):
* LPSTR lpDIBBits - 指向源DIB圖像指針
* LONG lWidth - 源圖像寬度(象素?cái)?shù),必須是4的倍數(shù))
* LONG lHeight - 源圖像高度(象素?cái)?shù))
* 返回值:
* BOOL - 運(yùn)算成功返回TRUE,否則返回FALSE。
*
* 說明:
* 該函數(shù)用于對檢測圖像中的平行直線。如果圖像中有兩條平行的直線,則將這兩條平行直線
* 提取出來。
*
* 要求目標(biāo)圖像為只有0和255兩個(gè)灰度值的灰度圖像。
************************************************************************/
BOOL WINAPI HoughDIB(LPSTR lpDIBBits, LONG lWidth, LONG lHeight)
{
// 指向源圖像的指針
LPSTR lpSrc;
// 指向緩存圖像的指針
LPSTR lpDst;
// 指向變換域的指針
LPSTR lpTrans;
// 圖像每行的字節(jié)數(shù)
LONG lLineBytes;
// 指向緩存DIB圖像的指針
LPSTR lpNewDIBBits;
HLOCAL hNewDIBBits;
//指向變換域的指針
LPSTR lpTransArea;
HLOCAL hTransArea;
//變換域的尺寸
int iMaxDist;
int iMaxAngleNumber;
//變換域的坐標(biāo)
int iDist;
int iAngleNumber;
//循環(huán)變量
long i;
long j;
//像素值
unsigned char pixel;
//存儲(chǔ)變換域中的兩個(gè)最大值
MaxValue MaxValue1;
MaxValue MaxValue2;
// 暫時(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);
//計(jì)算變換域的尺寸
//最大距離
iMaxDist = (int) sqrt(lWidth*lWidth + lHeight*lHeight);
//角度從0-180,每格2度
iMaxAngleNumber = 90;
//為變換域分配內(nèi)存
hTransArea = LocalAlloc(LHND, lWidth * lHeight * sizeof(int));
if (hNewDIBBits == NULL)
{
// 分配內(nèi)存失敗
return FALSE;
}
// 鎖定內(nèi)存
lpTransArea = (char * )LocalLock(hTransArea);
// 初始化新分配的內(nèi)存,設(shè)定初始值為0
lpTrans = (char *)lpTransArea;
memset(lpTrans, 0, lWidth * lHeight * sizeof(int));
// 計(jì)算圖像每行的字節(jié)數(shù)
lLineBytes = WIDTHBYTES(lWidth * 8);
for(j = 0; j <lHeight; j++)
{
for(i = 0;i <lWidth; i++)
{
// 指向源圖像倒數(shù)第j行,第i個(gè)象素的指針
lpSrc = (char *)lpDIBBits + lLineBytes * j + i;
//取得當(dāng)前指針處的像素值,注意要轉(zhuǎn)換為unsigned char型
pixel = (unsigned char)*lpSrc;
//目標(biāo)圖像中含有0和255外的其它灰度值
if(pixel != 255 && *lpSrc != 0)
return FALSE;
//如果是黑點(diǎn),則在變換域的對應(yīng)各點(diǎn)上加1
if(pixel == 0)
{
//注意步長是2度
for(iAngleNumber=0; iAngleNumber<iMaxAngleNumber; iAngleNumber++)
{
iDist = (int) fabs(i*cos(iAngleNumber*2*pi/180.0) + \
j*sin(iAngleNumber*2*pi/180.0));
//變換域的對應(yīng)點(diǎn)上加1
*(lpTransArea+iDist*iMaxAngleNumber+iAngleNumber) = \
*(lpTransArea+iDist*iMaxAngleNumber+iAngleNumber) +1;
}
}
}
}
//找到變換域中的兩個(gè)最大值點(diǎn)
MaxValue1.Value=0;
MaxValue2.Value=0;
//找到第一個(gè)最大值點(diǎn)
for (iDist=0; iDist<iMaxDist;iDist++)
{
for(iAngleNumber=0; iAngleNumber<iMaxAngleNumber; iAngleNumber++)
{
if((int)*(lpTransArea+iDist*iMaxAngleNumber+iAngleNumber)>MaxValue1.Value)
{
MaxValue1.Value = (int)*(lpTransArea+iDist*iMaxAngleNumber+iAngleNumber);
MaxValue1.Dist = iDist;
MaxValue1.AngleNumber = iAngleNumber;
}
}
}
//將第一個(gè)最大值點(diǎn)附近清零
for (iDist = -9;iDist < 10;iDist++)
{
for(iAngleNumber=-1; iAngleNumber<2; iAngleNumber++)
{
if(iDist+MaxValue1.Dist>=0 && iDist+MaxValue1.Dist<iMaxDist \
&& iAngleNumber+MaxValue1.AngleNumber>=0 && iAngleNumber+MaxValue1.AngleNumber<=iMaxAngleNumber)
{
*(lpTransArea+(iDist+MaxValue1.Dist)*iMaxAngleNumber+\
(iAngleNumber+MaxValue1.AngleNumber))=0;
}
}
}
//找到第二個(gè)最大值點(diǎn)
for (iDist=0; iDist<iMaxDist;iDist++)
{
for(iAngleNumber=0; iAngleNumber<iMaxAngleNumber; iAngleNumber++)
{
if((int)*(lpTransArea+iDist*iMaxAngleNumber+iAngleNumber)>MaxValue2.Value)
{
MaxValue2.Value = (int)*(lpTransArea+iDist*iMaxAngleNumber+iAngleNumber);
MaxValue2.Dist = iDist;
MaxValue2.AngleNumber = iAngleNumber;
}
}
}
//判斷兩直線是否平行
if(abs(MaxValue1.AngleNumber-MaxValue2.AngleNumber)<=2)
{
//兩直線平行,在緩存圖像中重繪這兩條直線
for(j = 0; j <lHeight; j++)
{
for(i = 0;i <lWidth; i++)
{
// 指向緩存圖像倒數(shù)第j行,第i個(gè)象素的指針
lpDst = (char *)lpNewDIBBits + lLineBytes * j + i;
//如果該點(diǎn)在某一條平行直線上,則在緩存圖像上將該點(diǎn)賦為黑
//在第一條直線上
iDist = (int) fabs(i*cos(MaxValue1.AngleNumber*2*pi/180.0) + \
j*sin(MaxValue1.AngleNumber*2*pi/180.0));
if (iDist == MaxValue1.Dist)
*lpDst = (unsigned char)0;
//在第二條直線上
iDist = (int) fabs(i*cos(MaxValue2.AngleNumber*2*pi/180.0) + \
j*sin(MaxValue2.AngleNumber*2*pi/180.0));
if (iDist == MaxValue2.Dist)
*lpDst = (unsigned char)0;
}
}
}
// 復(fù)制腐蝕后的圖像
memcpy(lpDIBBits, lpNewDIBBits, lWidth * lHeight);
// 釋放內(nèi)存
LocalUnlock(hNewDIBBits);
LocalFree(hNewDIBBits);
// 釋放內(nèi)存
LocalUnlock(hTransArea);
LocalFree(hTransArea);
// 返回
return TRUE;
}
/*************************************************************************
*
* 函數(shù)名稱:
* Fill2DIB()
*
* 參數(shù):
* LPSTR lpDIBBits - 指向源DIB圖像指針
* LONG lWidth - 源圖像寬度(象素?cái)?shù),必須是4的倍數(shù))
* LONG lHeight - 源圖像高度(象素?cái)?shù))
* 返回值:
* BOOL - 種子填充成功返回TRUE,否則返回FALSE。
*
* 說明:
* 該函數(shù)用于對圖像進(jìn)行種子填充運(yùn)算。
*
* 要求目標(biāo)圖像為只有0和255兩個(gè)灰度值的灰度圖像。
************************************************************************/
BOOL WINAPI Fill2DIB(LPSTR lpDIBBits, LONG lWidth, LONG lHeight)
{
// 指向源圖像的指針
LPSTR lpSrc;
//循環(huán)變量
long i;
//像素值
unsigned char pixel;
//左右邊界像素位置
int xl,xr;
//是否已填充至邊界
BOOL bFilll,bFillr;
//種子堆棧及指針
Seed Seeds[10];
int StackPoint;
//當(dāng)前像素位置
int iCurrentPixelx,iCurrentPixely;
int iBufferPixelx,iBufferPixely;
//初始化種子
Seeds[1].Height = lHeight / 2;
Seeds[1].Width = lWidth / 2;
StackPoint = 1;
while( StackPoint != 0)
{
//取出種子
iCurrentPixelx = Seeds[StackPoint].Width;
iCurrentPixely = Seeds[StackPoint].Height;
StackPoint--;
// if(Seed2.Height== 75)
// {
// return true;
// i++;
// }
bFilll = true;
bFillr = true;
//填充種子所在的行
//保存種子像素的位置
iBufferPixelx = iCurrentPixelx;
iBufferPixely = iCurrentPixely;
//先向左填充
while(bFilll)
{
lpSrc = (char *)lpDIBBits + lWidth * iCurrentPixely + iCurrentPixelx;
//取得當(dāng)前指針處的像素值,注意要轉(zhuǎn)換為unsigned char型
pixel = (unsigned char)*lpSrc;
//目標(biāo)圖像中含有0和255外的其它灰度值
if(pixel != 255 && pixel != 0)
return FALSE;
//遇到邊界
if(pixel == 0)
{
bFilll = false;
xl=iCurrentPixelx+1;
}
else
{
*lpSrc = (unsigned char)0;
iCurrentPixelx--;
//防止越界
if(iCurrentPixelx<0)
{
bFilll = false;
iCurrentPixelx = 0;
xl = 0;
}
}
}
//再向右填充
//取回種子像素的位置
iCurrentPixelx = iBufferPixelx+1;
if(iCurrentPixelx>lWidth)
{
bFillr = false;
iCurrentPixelx = lWidth;
xr = lWidth;
}
iCurrentPixely = iBufferPixely;
while(bFillr)
{
lpSrc = (char *)lpDIBBits + lWidth * iCurrentPixely + iCurrentPixelx;
//取得當(dāng)前指針處的像素值,注意要轉(zhuǎn)換為unsigned char型
pixel = (unsigned char)*lpSrc;
//目標(biāo)圖像中含有0和255外的其它灰度值
if(pixel != 255 && pixel != 0)
return FALSE;
//遇到邊界
if(pixel == 0)
{
bFillr = false;
xr=iCurrentPixelx-1;
}
else
{
*lpSrc = (unsigned char)0;
iCurrentPixelx++;
//防止越界
if(iCurrentPixelx>lWidth)
{
bFillr = false;
iCurrentPixelx = lWidth;
xr = lWidth;
}
}
}
//上、下兩條掃描線是否全為邊界象素或已填充過
//先看上面的掃描線
iCurrentPixely--;
if(iCurrentPixely < 0)
{
iCurrentPixely = 0;
}
for (i = xr; i>= xl;i--)
{
lpSrc = (char *)lpDIBBits + lWidth * iCurrentPixely + i;
//取得當(dāng)前指針處的像素值,注意要轉(zhuǎn)換為unsigned char型
pixel = (unsigned char)*lpSrc;
//有未填充的像素,將新的種子壓入堆棧
if (pixel == 255)
{
StackPoint++;
Seeds[StackPoint].Height = iCurrentPixely;
Seeds[StackPoint].Width = i;
break;
}
}
//再看下面的掃描線
iCurrentPixely+=2;
if(iCurrentPixely > lHeight)
{
iCurrentPixely = lHeight;
}
for (i = xr; i>= xl;i--)
{
lpSrc = (char *)lpDIBBits + lWidth * iCurrentPixely + i;
//取得當(dāng)前指針處的像素值,注意要轉(zhuǎn)換為unsigned char型
pixel = (unsigned char)*lpSrc;
//有未填充的像素,將新的種子壓入堆棧
if (pixel == 255)
{
StackPoint++;
Seeds[StackPoint].Height = iCurrentPixely;
Seeds[StackPoint].Width = i;
break;
}
}
}
// 返回
return TRUE;
}
/*************************************************************************
*
* 函數(shù)名稱:
* FillDIB()
*
* 參數(shù):
* LPSTR lpDIBBits - 指向源DIB圖像指針
* LONG lWidth - 源圖像寬度(象素?cái)?shù),必須是4的倍數(shù))
* LONG lHeight - 源圖像高度(象素?cái)?shù))
* 返回值:
* BOOL - 種子填充成功返回TRUE,否則返回FALSE。
*
* 說明:
* 該函數(shù)用于對圖像進(jìn)行種子填充運(yùn)算。
*
* 要求目標(biāo)圖像為只有0和255兩個(gè)灰度值的灰度圖像。
************************************************************************/
BOOL WINAPI FillDIB(LPSTR lpDIBBits, LONG lWidth, LONG lHeight)
{
// 指向源圖像的指針
LPSTR lpSrc;
//像素值
unsigned char pixel;
//種子堆棧及指針
Seed *Seeds;
int StackPoint;
//當(dāng)前像素位置
int iCurrentPixelx,iCurrentPixely;
//初始化種子
Seeds = new Seed[lWidth*lHeight];
Seeds[1].Height = lHeight / 2;
Seeds[1].Width = lWidth / 2;
StackPoint = 1;
while( StackPoint != 0)
{
//取出種子
iCurrentPixelx = Seeds[StackPoint].Width;
iCurrentPixely = Seeds[StackPoint].Height;
StackPoint--;
lpSrc = (char *)lpDIBBits + lWidth * iCurrentPixely + iCurrentPixelx;
//取得當(dāng)前指針處的像素值,注意要轉(zhuǎn)換為unsigned char型
pixel = (unsigned char)*lpSrc;
//目標(biāo)圖像中含有0和255外的其它灰度值
if(pixel != 255 && pixel != 0)
return FALSE;
//將當(dāng)前點(diǎn)涂黑
*lpSrc = (unsigned char)0;
//判斷左面的點(diǎn),如果為白,則壓入堆棧
//注意防止越界
if(iCurrentPixelx > 0)
{
lpSrc = (char *)lpDIBBits + lWidth * iCurrentPixely + iCurrentPixelx - 1;
//取得當(dāng)前指針處的像素值,注意要轉(zhuǎn)換為unsigned char型
pixel = (unsigned char)*lpSrc;
if (pixel == 255)
{
StackPoint++;
Seeds[StackPoint].Height = iCurrentPixely;
Seeds[StackPoint].Width = iCurrentPixelx - 1;
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -