///////////大 学
学 生 实 验 报 告
实验课程名称
多媒体技术
开课实验室
学
院
学 生 姓 名
学
号
开 课 时 间
20XX
至
20XX
学年第
一
学期
总 成 绩
教师签名
软件学院制
《多媒体技术》实验报告
开课实验室:
2015 年 X月X 日 学院
年级、专业、班
姓名
成绩
课程 名称 多媒体技术 实验项目 名
称 图像变换动画 指导教师
教师评语
教师签名:
年
月
日 一、实验目的 通过两幅图像的变换,实现动画效果。
二、实验原理 变形动画将一幅图像动态变换为另一幅大小相同、结构相似的图像。其中需要在两幅图像的主体结构中标注对应的变换点,在变换过程中按照对应点进行形状结构的变换。
如果不考虑图像形状结构的变换,则可简单地实现将一幅图像动态变换为另一幅大小相同的图像。其对应变换点就以对应的像素点确定。本实验即按照该方式实现。
对于大小不一样的图像需要更复杂的处理方式。需要一定的插值算法生成图像变换所产生的额外的像素值。最终效果因算法的不同而不同。本实验不考虑该情形。
变换结果先以图像文件方式存储,然后可采用两种方式实现动画展示:使用构造GIF动画的软件将变换过程的图像连接成GIF动画;自行编制软件按照一定的速度打开并展示变换过程的图像,形成动画展示效果。
理论上对于不同格式的图像文件均可以实现图像变换动画。这里为了简化操作,统一使用BMP格式的图像文件。
对于24位真彩色BMP图像构造图像变换动画,主要就是对于图像数据阵列中的各对应像素点的RGB值进行插值变换,实现将一幅图像中的像素点的RGB值变换为另一幅图像中对应像素点的RGB值。
对于使用调色板的BMP索引图像,生成图像变换动画的过程稍稍复杂。其变换方式有三种:(1)保持调色板不变,对于像素点进行颜色变换。将一个像素点变换为其对应的像素点时,根据该像素点索引值所指向的调色板表项的RGB和其对应的像素点索引值所指向的调色板表项的RGB,计算其变换图像的RGB值,然后在调色板中查找与变换过程图像的RGB值最接近的表项,将其对应的索引值作为变换过程图像的像素索引值。该方式不改变原始调色板,但需要将变换过程RGB值映射为调色板中最接近的表项;(2)允许改变调色板,对于像素点进行颜色变换。根据变换图像各像素点的索引值找到其对应的调色板表项,从而获取各像素点的RGB值。然后按照与24位真彩色BMP图像变换相似的方式,通过插值计算变换过程图像各像素点的RGB值。该方式将可能在变换过程图像中生成超过256种RGB组合的颜色。如果再采用索引图像方式存储该图像,则需要自行构造新的调色板,并将变换过程图像各像素点的RGB值映射到调色板中的表项;(3)保持调色板不变,对于像素点进行索引值变换。即,对于变换图像像素点的索引值,通过插值计算变换过程图像的索引值,实现简单的图像变换。
上述三种图像变换方式中,第三种难以实现平滑的图像颜色变换,效果较差;第二种实现过程较为复杂,需要自行生成新的调色板;第一种最为可行。
三、实验内容 打开两幅大小相同的BMP图像,分别指定为起始帧、终止帧,指定变换帧数,实现将图像从起始帧逐步变换到终止帧,将一副图像动态地变换为另一幅图像。
四、实验工具 VS2013 五、实验步骤 如果不在程序中实现图像的打开显示和变换动画显示,可以利用控制台程序读入图像,处理后生成各中间帧。
如果要在程序中实现图像的打开显示和变换动画显示,在这里将关键实验步骤分为c++和c#两个不同的版本叙述,提供实验方法而不阐述具体实现步骤:
1. C#版:
C#对于图像的操作已经封装好了具体的类Bitmap,参阅资料就可以熟悉Bitmap类中所有成员函数及属性的作用,动态效果主要是利用timer控件。
1) MFC布局如下:
Time1的Enabled 设置为True,Interval设置为10000ms相当于10秒
Main.cpp:
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms;
namespace animation {
public partial class Form1 : Form
{
public
Bitmap curBitmap;
Bitmap souBitmap;
Bitmap oriBitmap;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog fileDialog = new OpenFileDialog();
timer1.Stop();
fileDialog.Title = “选择文件“;
fileDialog.Filter = “bmp files (*.bmp)|*.bmp“;
fileDialog.FilterIndex = 1;
fileDialog.RestoreDirectory = true;
if (fileDialog.ShowDialog() == DialogResult.OK)
{
string fileName = fileDialog.FileName;
string name = fileName.Substring(fileName.LastIndexOf(“\\“) + 1);
oriBitmap = new Bitmap(fileName);
pictureBox1.Image = oriBitmap;
curBitmap = new Bitmap(fileName);
int bitmapHeight = oriBitmap.Height;
int bitmapWidth = oriBitmap.Width;
}
}
private void button2_Click(object sender, EventArgs e)
{
OpenFileDialog fileDialog = new OpenFileDialog();
timer1.Stop();
fileDialog.Title = “选择文件“;
fileDialog.Filter = “bmp files(*.bmp)|*.bmp“;
fileDialog.FilterIndex = 1;
fileDialog.RestoreDirectory = true;
if (fileDialog.ShowDialog() == DialogResult.OK)
{
string fileName = fileDialog.FileName;
oriBitmap = new Bitmap(fileName);
souBitmap = new Bitmap(fileName);
pictureBox2.Image = oriBitmap;
}
}
private void timer1_Tick(object sender, EventArgs e)
{
if (curBitmap != null)
{
curBitmap = (Bitmap)pictureBox1.Image;
Rectangle rect = new Rectangle(0, 0, curBitmap.Width, curBitmap.Height);
Rectangle rect1 = new Rectangle(0, 0, souBitmap.Width, souBitmap.Height);
System.Drawing.Imaging.BitmapData bmpData = curBitmap.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadWrite, curBitmap.PixelFormat);
System.Drawing.Imaging.BitmapData bmpData1 = souBitmap.LockBits(rect1, System.Drawing.Imaging.ImageLockMode.ReadWrite, souBitmap.PixelFormat);
//得到首地址
IntPtr ptr = bmpData.Scan0;
IntPtr ptr1 = bmpData1.Scan0;
//24位BMP位图字节
int bytes = curBitmap.Width * curBitmap.Height * 3;
int bytes1 = curBitmap.Width * curBitmap.Height * 3;
//定义位图数组
byte[] rgbValues = new byte[bytes];
byte[] rgbValues1 = new byte[bytes1];
//复制被锁定的位图像素值到该数组内
System.Runtime.InteropServices.Marshal.Copy(ptr, rgbValues, 0, bytes);
System.Runtime.InteropServices.Marshal.Copy(ptr1, rgbValues1, 0, bytes1);
//灰度化
// double colorTemp = 0;
for (int i = 0; i < rgbValues.Length; i += 3)
{
Byte incre1 = (byte)Math.Abs(rgbValues1[i] - rgbValues[i]);
Byte incre2 = (byte)Math.Abs(rgbValues1[i + 1] - rgbValues[i + 1]);
Byte incre3 = (byte)Math.Abs(rgbValues1[i + 2] - rgbValues[i + 2]);
rgbValues[i] += incre1;
rgbValues[i + 1] += incre2;
rgbValues[i + 2] += incre3;
}
//把数组复制回位图
System.Runtime.InteropServices.Marshal.Copy(rgbValues, 0, ptr, bytes);
System.Runtime.InteropServices.Marshal.Copy(rgbValues1, 0, ptr1, bytes1);
//解锁位图像素
curBitmap.UnlockBits(bmpData);
souBitmap.UnlockBits(bmpData1);
pictureBox1.Image = curBitmap;
}
}
private void button3_Click(object sender, EventArgs e)
{
timer1.Start();
}
} }
六、实验结果及分析
通过多媒体实验熟悉了C#编程的简单界面操作