数字图像处理 几何运算

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

实验3 图像的几何运算

实验名称:图像的几何运算

实验内容:

图像的几何运算与点运算相对立,几何运算的目的在于改变像素之间的空间位置和空间关系,但没有改变灰度等级值,包括两个独立的算法:空间变换和灰度插值。其中空间变换又包括平移和镜像处理,应用空间变换和灰度插值算法可以实现对图像的缩放和旋转处理。

图像的几何中心作为坐标原点,x轴由左向右递增,Y轴由上至下递增。因此,在进行图像旋转时,是以图像的几何中心为基准进行旋转的;在进行图像缩放时,也是以图像的几何中心为基准,其上下左右均等地向内收缩或向外扩大的。这种坐标转换会使图像变换更自然。另外,在进行几何运算的时候,保持原图像的尺寸大小不变,如果变换后的图像超出该尺寸,超出部分会被截断,而不足部分会以白色像素填充。

本此实验内容包括:图像平移、图像镜像、图像缩放、图像旋转。

实验步骤:

1、界面设计如下:

打开图像代码:

private void open_Click(object sender, EventArgs e)

{

OpenFileDialog opnDlg = new OpenFileDialog();

opnDlg.Filter = "所有图像文件 | *.bmp; *.pcx; *.png; *.jpg; *.gif;" +

"*.tif; *.ico; *.dxf; *.cgm; *.cdr; *.wmf; *.eps; *.emf|" +

"位图( *.bmp; *.jpg; *.png;...) | *.bmp; *.pcx; *.png; *.jpg; *.gif; *.tif; *.ico|" + "矢量图( *.wmf; *.eps; *.emf;...) | *.dxf; *.cgm; *.cdr; *.wmf; *.eps; *.emf";

opnDlg.Title = "打开图像文件";

opnDlg.ShowHelp = true;

if (opnDlg.ShowDialog() == DialogResult.OK)

{

curFileName = opnDlg.FileName;

try

{

curBitmap = (Bitmap)Image.FromFile(curFileName);

}

catch (Exception exp)

{

MessageBox.Show(exp.Message);

}

}

Invalidate();

}

关闭程序代码:

private void close_Click(object sender, EventArgs e)

{

this.Close();

}

Paint事件代码:

private void Form1_Paint(object sender, PaintEventArgs e)

{

Graphics g = e.Graphics;

if (curBitmap != null)

{

g.DrawImage(curBitmap, 160, 20, curBitmap.Width, curBitmap.Height);

}

}

图像平移代码:

private void translation_Click(object sender, EventArgs e)

{

if (curBitmap != null)

{

Translation traForm = new translation();

if (traForm.ShowDialog() == DialogResult.OK)

{

Rectangle rect = new Rectangle(0, 0, curBitmap.Width, curBitmap.Height); System.Drawing.Imaging.BitmapData bmpData = curBitmap.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadWrite, curBitmap.PixelFormat);

IntPtr ptr = bmpData.Scan0;

int bytes = curBitmap.Width * curBitmap.Height;

byte[] grayValues = new byte[bytes];

System.Runtime.InteropServices.Marshal.Copy(ptr, grayValues, 0, bytes);

int x = Convert.ToInt32(traForm.GetXOffset);

int y = Convert.ToInt32(traForm.GetYOffset);

byte[] tempArray = new byte[bytes];

//Array.Clear(tempArray, 0, bytes);

for (int i = 0; i < bytes; i++)

{

tempArray[i] = 255;

}

for (int i = 0; i < curBitmap.Height; i++)

{

if ((i + y) < curBitmap.Height && (i + y) > 0)

{

for (int j = 0; j < curBitmap.Width; j++)

{

if ((j + x) < curBitmap.Width && (j + x) > 0)

{

tempArray[(j + x) + (i + y) * curBitmap.Width] = grayValues[j + i * curBitmap.Width];

}

}

}

}

grayValues = (byte[])tempArray.Clone();

System.Runtime.InteropServices.Marshal.Copy(grayValues, 0, ptr, bytes); curBitmap.UnlockBits(bmpData);

}

Invalidate();

}

}

相关文档
最新文档