版权声明:本文为博主原创文章,转载请附上链接地址。 https://blog.csdn.net/ld15102891672/article/details/80628817
数字图像的旋转变换、镜像变换、错切变换以及平移变换等称为数字图像的几何变换。下文是数字图像旋转变换、镜像变换以及错切变换的基本原理的介绍以及C#代码的实现。
(1)数字图像旋转变换。
数字图像旋转变换基本原理:
数字图像旋转变换代码实现:
private void btn_Rotate_Click(object sender, EventArgs e)//旋转变换按钮
{
BmapNew = Rotate(60.0, BmapOld);
this.pictureNew.Image = BmapNew;
}
private Bitmap Rotate(double Angle,Bitmap mapOld)//旋转变换代码
{
double rad=Math.PI/180.0*Angle;
Matrix T = new Matrix(3, 3);
T.setElem(0, 0, Math.Cos(rad));
T.setElem(0, 1, -Math.Sin(rad));
T.setElem(1,0, Math.Sin(rad));
T.setElem(1, 1, Math.Cos(rad));
T.setElem(2, 2,1);
int w = mapOld.Width;
int h = mapOld.Height;
Bitmap mapNew = new Bitmap((int)(w * Math.Cos(rad) + h * Math.Sin(rad)) + 1, (int)(w * Math.Sin(rad) + h * Math.Cos(rad)) + 1);
Matrix map = new Matrix(3, 1);
for (int j = 0; j < h; j++)
{
for (int i = 0; i< w; i++)
{
map.setElem(0, 0, i);
map.setElem(1, 0, j);
map.setElem(2, 0, 1);
map = T.mult(map);
Color pixel=mapOld.GetPixel(i,j);
mapNew.SetPixel((int)map.getElem(0, 0) + (int)(h* Math.Sin(rad)), (int)map.getElem(1, 0), pixel);
}
}
return mapNew;
}
(2)数字图像镜像变换
数字图像镜像变换基本原理:
数字图像镜像变换代码实现:
private void btn_JingXiang_Click(object sender, EventArgs e)//镜像变换按钮
{
BmapNew = Mirror(BmapOld);
this.pictureNew.Image = BmapNew;
}
private Bitmap Mirror(Bitmap mapOld)//图片镜像变换代码
{
int w = mapOld.Width;
int h = mapOld.Height;
Matrix T = new Matrix(3, 3);
T.setElem(0, 0, -1);
T.setElem(0, 2, w);
T.setElem(1, 1, 1);
T.setElem(2, 2, 1);
Bitmap mapNew =new Bitmap(w,h);
Matrix map = new Matrix(3, 1);
for (int j = 0; j < h; j++)
{
for (int i = 0; i < w; i++)
{
map.setElem(0, 0, i);
map.setElem(1, 0, j);
map.setElem(2, 0, 1);
map = T.mult(map);
Color pixel = mapOld.GetPixel(i, j);
mapNew.SetPixel((int)map.getElem(0, 0)-1, (int)map.getElem(1, 0), pixel);
}
}
return mapNew;
}
(3)数字图像错切变换
数字图像错切变换基本原理:
数字图像错切变换代码实现:
private void btn_ShearMapping_Click(object sender, EventArgs e)//错切变换按钮
{
BmapNew = ShearMapping(30.0, BmapOld);
this.pictureNew.Image = BmapNew;
}
private Bitmap ShearMapping(double Angle,Bitmap mapOld)//图片错切变换代码
{
double rad = Math.PI / 180.0 * Angle;
int w = mapOld.Width;
int h = mapOld.Height;
int newW = w + (int)(h * Math.Tan(rad));
Bitmap mapNew = new Bitmap(newW, h);
for (int j = 0; j < h; j++)
{
for (int i = 0; i < w; i++)
{
Color pixel = mapOld.GetPixel(i, j);
mapNew.SetPixel((int)((h-j)*Math.Tan(rad))+i, j, pixel);
}
}
return mapNew;
}