霍夫变换C#代码 [英] Hough Transform C# code

查看:351
本文介绍了霍夫变换C#代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我们看一下此内容C#实现

        ... ...

        // get source image size
        int width       = image.Width;
        int height      = image.Height;
        int halfWidth   = width / 2;
        int halfHeight  = height / 2;

        // make sure the specified rectangle recides with the source image
        rect.Intersect( new Rectangle( 0, 0, width, height ) );

        int startX = -halfWidth  + rect.Left;
        int startY = -halfHeight + rect.Top;
        int stopX  = width  - halfWidth  - ( width  - rect.Right );
        int stopY  = height - halfHeight - ( height - rect.Bottom );

        int offset = image.Stride - rect.Width;

        // calculate Hough map's width
        int halfHoughWidth = (int) Math.Sqrt( halfWidth * halfWidth + halfHeight * halfHeight );
        int houghWidth = halfHoughWidth * 2;

        houghMap = new short[houghHeight, houghWidth];

        // do the job
        unsafe
        {
            byte* src = (byte*) image.ImageData.ToPointer( ) +
                rect.Top * image.Stride + rect.Left;

            // for each row
            for ( int y = startY; y < stopY; y++ )
            {
                // for each pixel
                for ( int x = startX; x < stopX; x++, src++ )
                {
                    if ( *src != 0 )
                    {
                        // for each Theta value
                        for ( int theta = 0; theta < houghHeight; theta++ )
                        {
                            int radius = (int) Math.Round( cosMap[theta] * x - sinMap[theta] * y ) + halfHoughWidth;

                            if ( ( radius < 0 ) || ( radius >= houghWidth ) )
                                continue;

                            houghMap[theta, radius]++;
                        }
                    }
                }
                src += offset;
            }
        }

        ... ... ...

Q.1. rect.Intersect(new Rectangle( 0, 0, width, height));-为什么这行很重要?

Q.1. rect.Intersect(new Rectangle( 0, 0, width, height)); - why is this line important?

Q.2. 为什么在以下代码中使用rect修改值:

Q.2. Why are values modified using rect In the following code:

int startX = -halfWidth  + rect.Left;
int startY = -halfHeight + rect.Top;
int stopX  = width  - halfWidth  - ( width  - rect.Right );
int stopY  = height - halfHeight - ( height - rect.Bottom );

Q.3. 为什么y和x循环从负点开始?

推荐答案

Q.1.

此行仅确保边界矩形完全在图像内部.如果您跳过此操作,并且rect(部分)在图像之外,则最终将超出索引范围.

Q.1.

This line simply ensures that the bounding rectangle is completely inside the image. If you were to skip this, and rect is (partly) outside the image, you'd end up indexing out of bounds.

请注意,像素访问是通过指针完成的,指针每x增量增加1,并且每y增量增加offset.如果rect大于图像,我们将指针增加到图像缓冲区之外.如果rect只是简单地移出了图像范围,但又不是太大,我们将读取与我们使用的坐标不对应的像素.

Note that pixel access is done through a pointer, incrementing the pointer by 1 for every x-increment, and by offset for every y-increment. If rect is larger than the image, we'd increment the pointer past the image buffer. If rect is simply shifted out the image bounds, but not too large, we'd read pixels that do not correspond to the coordinates we're using.

请注意

int stopX  = width  - halfWidth  - ( width  - rect.Right );
int stopY  = height - halfHeight - ( height - rect.Bottom );

可以简化为

int stopX  = - halfWidth  + rect.Right;
int stopY  = - halfHeight + rect.Bottom;

该代码在图像中间定义坐标系的原点.此代码位将边界框(最初在[0,width范围内和[0,height]范围内)定义)移动到新坐标系.

The code defines the origin of the coordinate system in the middle of the image. This bit of code shifts the bounding box (originally defined in the range [0,width) and [0,height) ) to the new coordinate system.

通常,霍夫变换是用左上角像素中的原点定义的.但是原则上,如何定义坐标系并不重要,这只是修改了线的参数化.为每个输入像素绘制的正弦曲线将有所不同,但是对于与图像中的线条相对应的参数集,局部最大值仍将出现.

Usually the Hough transform is defined with the origin in the top-left pixel. But in principle it does not matter how the coordinate system is defined, this just modifies the parametrization of a line. The sinusoid drawn for each input pixel will be different, but the local maxima will still appear for the set of parameters corresponding to lines in the image.

这篇关于霍夫变换C#代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆