c# - 霍夫变换C#代码

标签 c# image-processing hough-transform

让我们看看this C# implementation ,

        ... ...

        // 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.2。 为什么使用 rect 修改值在下面的代码中:

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 循环从负点开始?

最佳答案

问题1。

这条线只是确保边界矩形完全位于图像内部。如果您要跳过此步骤,并且 rect (部分)位于图像之外,那么您最终会超出索引范围。

请注意,像素访问是通过指针完成的,每个 x 增量将指针增加 1,每个 y 增量将指针增加 offset。如果 rect 大于图像,我们会将指针增加到图像缓冲区之外。如果 rect 只是移出图像边界,但不是太大,我们会读取与我们正在使用的坐标不对应的像素。

问题 2。

请注意

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) 范围内定义)移动到新的坐标系。

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

关于c# - 霍夫变换C#代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51863258/

相关文章:

c# - gridView.DataSource as DataTable 在 asp.net 中设置为空

c++ - 图像下采样算法

python - 使用sobel滤波器进行边缘检测后提取每个对象

java - HoughCircles找错圈子(opencv)

python - Python 中的 Hough 变换 - 结果偏移不正确 - 索引错误?

matlab - 霍夫变换 : Converted polar coordinates back to Cartesian, 但仍然无法绘制它们

c# - 如何将文本框的边框颜色重置为默认系统颜色

c# - GetComInterfaceForObject 是否固定对象?

c# - 找不到类型或命名空间名称 'Description'

python - 使用opencv和python清理图像背景后如何正确提取字母?