65.9K
CodeProject 正在变化。 阅读更多。
Home

图像的极坐标视图

starIconstarIconstarIcon
emptyStarIcon
starIcon
emptyStarIcon

3.50/5 (4投票s)

2009年2月24日

CPOL

2分钟阅读

viewsIcon

60310

downloadIcon

1665

一篇文章,描述如何创建图像的极坐标视图 (C#)。

LenaPolar.PNG

引言

图像扭曲可以产生有趣的结果。许多特殊效果都可以通过图像扭曲来实现。扭曲是一个过程,图像以某种方式被变换,从而得到另一幅图像。这就像将图像打印在橡胶片上,然后以非均匀的方式拉伸该橡胶片。极坐标映射是图像扭曲的一个例子。这类似于图像处理中可用的标准对数极坐标映射。我们眼睛视网膜中的感受器分布类似于对数极坐标阵列。对数极坐标映射和极坐标映射之间的区别在于,对数极坐标映射中的同心圆间距是非均匀的,而极坐标映射中的同心圆间距是均匀的。在本文中,我们展示了执行图像极坐标映射的代码。

极坐标映射

极坐标映射的基本几何形状如图所示。以图像中心为中心绘制等间距的同心圆,并绘制多个等间距的扇区。将这些圆和径线的交点处的像素绘制在矩形网格上,得到的图像就是极坐标视图。在对数极坐标映射中,同心圆的半径按对数比例变化。

PolarMap1.png

Using the Code

代码的开发过程如下:

  1. 打开输入图像进行读取。
  2. 创建映射参数:在此步骤中,我们为正弦和余弦值创建查找表。
  3. 使用极坐标映射创建位图。以下代码片段填充位图。
static Bitmap CreatePolarImage()
{
    // In the Polar bitmap, 
    // Width = number of sectors
    // Height = number of rings
    Bitmap bmpOut = new Bitmap(noSectors, noRings, 
                               PixelFormat.Format24bppRgb);
    int i, j, j1, pixelsize = 3;
    int x, y;
    byte red = 0, green = 0, blue = 0;
    double f1 = 0.0, f2 = 0.0;
    float r = dr;
    x = Convert.ToInt32(Math.Round(cx));
    y = Convert.ToInt32(Math.Round(cy));
    GetRGBVals(x, y, ref red, ref green, ref blue);
    // Map the centre point 
    for (i = 0; i < noSectors; ++i)
    {
        Color c = Color.FromArgb(red, green, blue);
        bmpOut.SetPixel(i, 0, c);
    }
    // Map the rest of the image

    Rectangle rectOut = new Rectangle(0, 0, bmpOut.Width, bmpOut.Height);
    BitmapData bmdOut = bmpOut.LockBits(rectOut, ImageLockMode.ReadWrite,
                                        bmpOut.PixelFormat);
    unsafe
    {
        for (j = 0; j < noRings; ++j, r += dr)
        {
            byte* row = (byte*)bmdOut.Scan0 + (j * bmdOut.Stride);
            for (i = 0; i < noSectors; ++i)
            {
                f1 = (double)(cosValues[i]);
                f2 = (double)(sinValues[i]);

                x = Convert.ToInt32(Math.Round(cx + r * f1));
                y = Convert.ToInt32(Math.Round(cy + r * f2));
                if (srcRect.Contains(x, y))
                {
                    GetRGBVals(x, y, ref red, ref green, ref blue);
                }
                else
                {
                    red = green = blue = 0;
                }
                j1 = i * pixelsize;

                row[j1] = blue;
                row[j1 + 1] = green;
                row[j1 + 2] = red;
            }
        }
    }
    bmpOut.UnlockBits(bmdOut);
    return bmpOut;
}

static void GetRGBVals(int x, int y, ref byte red, 
            ref byte green, ref byte blue)
{
    Color c = bmpIn.GetPixel(x, y);
    red = c.R;
    green = c.G;
    blue = c.B;
}

此代码是在 Visual Studio 2008 上使用 C# 开发的命令行应用程序。此代码的用法是:

PolarView.exe <infile> <outfile.png> <noSectors> <noRings>

它生成一个 PNG 文件作为输出。

闭包

以上解释了一个简单的程序,用于生成一种有趣的图像扭曲形式,即极坐标视图。需要注意的是,图像的旋转在极坐标视图中只是沿水平轴的循环移位。此页面的顶部显示了一张图像及其极坐标映射。该程序受到 **Nick Efford 的《数字图像处理》**一书的启发。此代码可以进一步增强,以使用更好的插值函数来确定源像素。

© . All rights reserved.