📄 hough.c
字号:
short WINAPI HoughLineDIB(LPSTR lpDIBBits, LONG lWidth, LONG lHeight,int threshold,int& iAngle,int& iDistance)
{
// 符合要求的直线总数
int allline;
// 指向源图像的指针
LPSTR lpSrc;
// 指向缓存图像的指针
LPSTR lpDst;
// 指向变换域的指针
LPSTR lpTrans;
// 图像每行的字节数
LONG lLineBytes;
// 指向缓存DIB图像的指针
LPSTR lpNewDIBBits;
HLOCAL hNewDIBBits;
//指向变换域的指针
LPSTR lpTransArea;
HLOCAL hTransArea;
//变换域的尺寸
int iMaxDist,iMaxMinusDist;
int iMaxAngleNumber;
//变换域的坐标
int iDist;
int iAngleNumber;
//循环变量
long i;
long j;
//像素值
unsigned char pixel;
//存储变换域中的两个最大值
MaxValue MaxValue1;
//Maxvalue MaxValue2;
// 暂时分配内存,以保存新图像
hNewDIBBits = LocalAlloc(LHND, lWidth * lHeight);
if (hNewDIBBits == NULL)
{
// 分配内存失败
return 0;
}
// 锁定内存
lpNewDIBBits = (char * )LocalLock(hNewDIBBits);
// 初始化新分配的内存,设定初始值为255
lpDst = (char *)lpNewDIBBits;
memset(lpDst, (BYTE)255, lWidth * lHeight);
//计算变换域的尺寸
//最大距离
iMaxDist = (int) sqrt(lWidth*lWidth + lHeight*lHeight);
iMaxMinusDist = lWidth>lHeight? lWidth:lHeight;
//角度从0-180,每格1度
iMaxAngleNumber = 180;
//为变换域分配内存
hTransArea = LocalAlloc(LHND, iMaxDist * iMaxAngleNumber * sizeof(int));
if (hNewDIBBits == NULL)
{
// 分配内存失败
return 0;
}
// 锁定内存
lpTransArea = (char * )LocalLock(hTransArea);
// 初始化新分配的内存,设定初始值为0
lpTrans = (char *)lpTransArea;
memset(lpTrans, 0, iMaxDist * iMaxAngleNumber * sizeof(int));
// 计算图像每行的字节数
lLineBytes = WIDTHBYTES(lWidth * 8);
for(j = 0; j <lHeight; j++)
{
for(i = 0;i <lWidth; i++)
{
// 指向源图像倒数第j行,第i个象素的指针
lpSrc = (char *)lpDIBBits + lLineBytes * j + i;
//取得当前指针处的像素值,注意要转换为unsigned char型
pixel = (unsigned char)*lpSrc;
//目标图像中含有0和255外的其它灰度值
if(pixel != 255 && *lpSrc != 0)
return 0;
//如果是黑点,则在变换域的对应各点上加1
if(pixel == 0)
{
//注意步长是1度
for(iAngleNumber=0; iAngleNumber<iMaxAngleNumber; iAngleNumber++)
{
iDist = (int) (i*cos(iAngleNumber*PI/180.0) + \
j*sin(iAngleNumber*PI/180.0))+iMaxMinusDist;
//变换域的对应点上加1
*(lpTransArea+iDist*iMaxAngleNumber+iAngleNumber) = \
*(lpTransArea+iDist*iMaxAngleNumber+iAngleNumber) +1;
}
}
}
}
//找到变换域中的一个最大值点
MaxValue1.Value=0;
//MaxValue2.Value=0;
allline=0;
//找到第一个最大值点
for (iDist=0; iDist<iMaxMinusDist+iMaxDist;iDist++)
{
for(iAngleNumber=0; iAngleNumber<iMaxAngleNumber; iAngleNumber++)
{
if((int)*(lpTransArea+iDist*iMaxAngleNumber+iAngleNumber)>threshold)
{
allline++;
}
if((int)*(lpTransArea+iDist*iMaxAngleNumber+iAngleNumber)>MaxValue1.Value)
{
MaxValue1.Value = (int)*(lpTransArea+iDist*iMaxAngleNumber+iAngleNumber);
MaxValue1.Dist = iDist-iMaxMinusDist;
MaxValue1.AngleNumber = iAngleNumber;
}
}
}
if(MaxValue1.Value<threshold)
{
LocalUnlock(hNewDIBBits);
LocalFree(hNewDIBBits);
// 释放内存
LocalUnlock(hTransArea);
LocalFree(hTransArea);
return 0;
}
iAngle=MaxValue1.AngleNumber;
iDistance=MaxValue1.Dist;
// 释放内存
LocalUnlock(hNewDIBBits);
LocalFree(hNewDIBBits);
// 释放内存
LocalUnlock(hTransArea);
LocalFree(hTransArea);
return allline;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -