VC++用Hough变换检测直线?

shanlingzhi123 2009-05-12 02:15:09
谁有Hough变换检测直线VC++的完整源码(要能够检测多条直线,交叉直线的)?

会写的或者有程序的给我一个,谢谢!!!

QQ:75215049

邮箱:shanling111111123@163.com
...全文
1720 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
wrhwww 2009-06-01
  • 打赏
  • 举报
回复
//========================================================
// 函数名:HoughDIB
// 功能:对图像进行Hough变换,找到图像的倾斜角
// 参数:
// int tAngle;图像的倾斜角
// int step; 角度步长,目的是减少Hough变换的计算量
// LPSTR lpDIBBits;要变换的图像指针
// LONG lWidth;图像的宽度
// LONG lHeight;图像的高度
// 返回值:图像的倾斜角
//=========================================================
int CxxxView::HoughDIB(int fAngle,int tAngle, int step, LPSTR lpDIBBits, LONG lWidth, LONG lHeight)
{
LPSTR lpSrc; // 指向源图像的指针
LPSTR lpDst; // 指向缓存图像的指针
LPSTR lpTrans; // 指向变换域的指针
LONG lLineBytes; // 图像每行的字节数
LPSTR lpNewDIBBits; // 指向缓存DIB图像的指针
HLOCAL hNewDIBBits;
LPSTR lpTransArea; // 指向变换域的指针
HLOCAL hTransArea;

int iMaxDist; // 变换域的尺寸
int iMaxAngleNumber;
int iDist; // 变换域的坐标
int iAngleNumber;
long i,j; // 循环变量
unsigned char pixel; // 像素值
MaxValue MaxValue1; // 存储变换域中的两个最大值

// 暂时分配内存,以保存新图像
hNewDIBBits = LocalAlloc(LHND, lWidth * lHeight);
if (hNewDIBBits == NULL)
{
return FALSE;
}

lpNewDIBBits = (char * )LocalLock(hNewDIBBits);

// 初始化新分配的内存,设定初始值为255
lpDst = (char *)lpNewDIBBits;
memset(lpDst, (BYTE)255, lWidth * lHeight);

//计算变换域的尺寸 最大距离
iMaxDist = (int) sqrt(lWidth*lWidth + lHeight*lHeight);
//角度从0-tAngle,每格step度
//
iMaxAngleNumber = (int)((tAngle-fAngle)/step);//90;

//为变换域分配内存
hTransArea = LocalAlloc(LHND, lWidth * lHeight * sizeof(int));
if (hNewDIBBits == NULL)
{
return FALSE;
}

lpTransArea = (char * )LocalLock(hTransArea);

// 初始化新分配的内存,设定初始值为0
lpTrans = (char *)lpTransArea;
memset(lpTrans, 0, lWidth * lHeight * 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)
{
AfxMessageBox("非二值图像!");
return 0;
}

//如果是黑点,则在变换域的对应各点上加1
if(pixel == 0)
{
//注意步长是2度
for(iAngleNumber=0; iAngleNumber<iMaxAngleNumber; iAngleNumber++)
{
iDist = (int) fabs(i*cos(iAngleNumber*step*PI/180.0) + \
j*sin(iAngleNumber*2*PI/180.0));

//变换域的对应点上加1
*(lpTransArea+iDist*iMaxAngleNumber+iAngleNumber) = \
*(lpTransArea+iDist*iMaxAngleNumber+iAngleNumber) +1;
}
}
}
}

//找到变换域中的两个最大值点
MaxValue1.Value=0;
//找到第一个最大值点
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;
}
}
}

return MaxValue1.AngleNumber;

}
YMM861223 2009-05-31
  • 打赏
  • 举报
回复
请问楼主你检测直线的源码还有找到啊?已经有的话麻烦发我一下553966310@qq.com
byxdaz 2009-05-31
  • 打赏
  • 举报
回复
/*
函数功能:通过hough变换检测直线
参数说明:imgBinaryIn,表示二值图象
width,表示图象宽
height,表示图象高
houghBuf,表示hough变换需要的缓冲区指针
houghWidth,表示hough变换需要的缓冲区的宽
houghHeight,表示hough变换需要的缓冲区的高
radiusResolution,表示hough变换的极坐标半径的检测分辨率
angleResolution,表示hough变换的角度检测分辨率
radius,表示返回hough变换检测出来的最长直线的极半径
angle,表示返回hough变换检测出来的最长直线的角度
*/
void ImageAlgorithm::HoughTransForm(unsigned char * imgBinaryIn,int width,int height,int *houghBuf,int houghWidth,int houghHeight,float radiusResolution,float angleResolution,float *radius,float *angle)
{
int i,j;
for(i=0;i<houghHeight;i++)
{
for (j=0;j<houghWidth;j++)
{
*(houghBuf+i*houghWidth+j) = 0;
}
}

int r,a;
float tempR,tempA;
//遍历图象数据
for(i=0;i<height;i++)
{
for(j=0;j<width;j++)
{
if(*(imgBinaryIn+i*width+j)==1)
{
//a代表角度的循环变量,在变换空间累加数组的垂直方向上
for(a=0;a<houghHeight;a++)
{
//按照给定变换角度的分辨率,求取角度
tempA = (a-houghHeight/2)*angleResolution;
//根据当前遍历的角度及x,y值求取对应极坐标
tempR = (j-width/2)*cos(tempA*2*PI/360) + (i-height/2)*sin(tempA*2*PI/360);
r = tempR/radiusResolution;
//累加数组
*(houghBuf+a*houghWidth+r+houghWidth/2)+=1;
}
}
}
}

//求累加数组的极大值,并记录此时的数组坐标
int max,maxR,maxA;
max = *(houghBuf+0*houghWidth+0);
maxR = 0;
maxA = 0;
for(a=0;a<houghHeight;a++)
{
for(r=0;r<houghWidth;r++)
{
if(max<=*(houghBuf+a*houghWidth+r))
{
max = *(houghBuf+a*houghWidth+r);
maxR = r;
maxA = a;
}
}
}

//将极大值位置转换成极坐标半径和角度,并通过参数返回
*radius = (maxR-houghWidth/2)*radiusResolution;
*angle = (maxA-houghHeight/2)*angleResolution;

}

Seth 2009-05-31
  • 打赏
  • 举报
回复
推荐LZ看章毓晋的《数字图像处理》,老版的我看过,有Hough变换的内容。新版的还没看

不过,只看这些书上的内容肯定不够。这些书上的Hough变换都只是介绍了最原始的Hough变化算法,也就是“狂算”法,速度奇慢。
要更快速的算法,就只能去查一下学术期刊上的论文了。
2hope 2009-05-31
  • 打赏
  • 举报
回复
UP
aaaa3105563 2009-05-31
  • 打赏
  • 举报
回复
UP
梅文海 2009-05-13
  • 打赏
  • 举报
回复
楼主去搜一下《Visual C++ 数字图像处理实例与解析 代码》,这里面有你要的代码,并且还有很多其他转频域的代码,你可以参考一下。
cau228charm 2009-05-13
  • 打赏
  • 举报
回复
我有306150820@qq。com
chiwa737 2009-05-12
  • 打赏
  • 举报
回复
http://slash-directx.blogspot.com/2009/04/huff-transformation.html
chiwa737 2009-05-12
  • 打赏
  • 举报
回复
http://slash-directx.blogspot.com/2009/04/huff-transformation.html

19,464

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 图形处理/算法
社区管理员
  • 图形处理/算法社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧