为了发论文,做嵌入式图像处理,基于stm32f407平台, 趁兼职休息时候写点算法 不知对否 晚上回宿舍才能验证一下。
输入为100*100的二值图(背景为0,前景为255),存放在矩阵中,iThreshold为判断为直线的域值。输出pR为原点到直线的距离,pTh为直线的角度。
void hough_conversion()
{
int iRMax = (int)sqrt(100 * 100 + 100 * 100) + 1;
int iThMax = 361;
int iTh = 0;
int iR;
int iMax = -1;
int iThMaxIndex = -1;
int iRMaxIndex = -1;
float fRate = (float)(3.14159/180);
char pArray[145 * 361];
char height =100;
char width =100;
char x,y;
for (y = 0; y < height; y++)
{
for (x = 0; x < width; x++)
{
if(gray_img[x][y] == 255)
{
for(iTh = 0; iTh < iThMax; iTh += 1)
{
iR = (int)(x * cos(iTh * fRate) + y * sin(iTh * fRate));
if(iR > 0)
{
pArray[iR/1 * iThMax + iTh]++;
}
}
}
}
}
for(iR = 0; iR < iRMax; iR++)
{
for(iTh = 0; iTh < iThMax; iTh++)
{
int iCount = pArray[iR * iThMax + iTh];
if(iCount > iMax)
{
iMax = iCount;
iRMaxIndex = iR;
iThMaxIndex = iTh;
}
}
}
if(iMax >= iThreshold)
{
*pR = iRMaxIndex;
*pTh = iThMaxIndex;
}
}