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;//出现图像的不完全处理的原因是我们打开的24为bmp图像而不是书上说的8为bmp图像
//因此图像的像素点是height*width*3namespace Transfer
{public partial class Form1 : Form{private string curFileName;private System.Drawing.Bitmap curBitmap;public Form1(){InitializeComponent();}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;//使用Image.FromFile创建图像对象try{curBitmap = (Bitmap)Image.FromFile(curFileName);}catch (Exception exp){//抛出异常MessageBox.Show(exp.Message);}}//对窗体进行重新绘制,这将强制执行paint事件处理程序Invalidate();}private void close_Click(object sender, EventArgs e){this.Close();}private void close_Paint(object sender, PaintEventArgs e){}private void Form1_Paint(object sender, PaintEventArgs e){Graphics g = e.Graphics;if (curBitmap != null){//使用DrawImage方法绘制图像//160,20显示在主窗体内,图像左上角的坐标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;//24为bmp位图字节数int bytes = curBitmap.Width * curBitmap.Height * 3;//定义位图数组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];//临时数组初始化为白色255像素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*3);j++){//保证横向平移不出界if((j+x)< (curBitmap.Width*3)&& (j+x)>0){tempArray[(j + x) + (i + y) * curBitmap.Width*3] = grayValues[j + i * curBitmap.Width*3];}}}}grayValues = (byte[])tempArray.Clone();System.Runtime.InteropServices.Marshal.Copy(grayValues, 0, ptr, bytes);curBitmap.UnlockBits(bmpData);}Invalidate();}}private void mirror_Click(object sender, EventArgs e){if(curBitmap!=null){//实例化窗体mirrormirror mirForm = new mirror();if(mirForm.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;//24为bmp位图字节数int bytes = curBitmap.Width * curBitmap.Height * 3;//定义位图数组byte[] grayValues = new byte[bytes];//复制被锁定的位图像素值到该数组内System.Runtime.InteropServices.Marshal.Copy(ptr, grayValues, 0, bytes);//水平中轴int halfWidth = (curBitmap.Width*3) / 2;//垂直中轴int halfHeight = (curBitmap.Height) / 2;byte temp;if(mirForm.GetMirror){//水平镜像处理for(int i=0;i<curBitmap.Height;i++){for(int j=0;j<halfWidth;j++){//以水平中轴线为对称轴,两边像素值交换temp = grayValues[i * curBitmap.Width*3 + j];grayValues[i * curBitmap.Width*3 + j] = grayValues[(i + 1) * curBitmap.Width*3 - 1 - j];grayValues[(i + 1)*curBitmap.Width*3 - 1 - j] = temp;}}}else{//垂直镜像处理for(int i=0;i<curBitmap.Width*3;i++){for(int j=0;j<halfHeight;j++){//以垂直中轴线为对称轴。两边像素值互换temp = grayValues[j * curBitmap.Width*3 + i];grayValues[j * curBitmap.Width*3 + i] = grayValues[(curBitmap.Height - j - 1) * curBitmap.Width*3 + i];grayValues[(curBitmap.Height - j - 1) * curBitmap.Width*3 + i] = temp;}}}System.Runtime.InteropServices.Marshal.Copy(grayValues, 0, ptr, bytes);curBitmap.UnlockBits(bmpData);}Invalidate();}}}
}
//要注意24位灰度图像是1440万色,但是我们书上给的算法是对8为进行处理,但是我们可以采用分割的思想将24位拆开成
//3个8位,由这三个8为所保存的数据组合为24位,在处理的时候就将他们分开处理但是要整体观看
//其中的 curBit.Height*3这个处理是关键

//以上只是主窗体里面的代码和自己的思考,方便以后复习,写的不是很好

C#图像处理初学之平移和镜像相关推荐

  1. 【数字图像处理】六.MFC空间几何变换之图像平移、镜像、旋转、缩放详解

    本文主要讲述基于VC++6.0 MFC图像处理的应用知识,主要结合自己大三所学课程<数字图像处理>及课件进行讲解,主要通过MFC单文档视图实现显示BMP图片空间几何变换,包括图像平移.图形 ...

  2. 【数字图像处理】六.MFC空间几何变换之图像平移、镜像、旋转、缩放具体解释...

           本文主要讲述基于VC++6.0 MFC图像处理的应用知识,主要结合自己大三所学课程<数字图像处理>及课件进行解说,主要通过MFC单文档视图实现显示BMP图片空间几何变换.包含 ...

  3. 数字图像处理(五)几何变换之图像平移、镜像、绕中心点旋转、缩放等

    本文为参考这位https://blog.csdn.net/eastmount/article/details/46345299所做的一些笔记,文字部分复制粘贴,代码部分有所改进,增加了绕中心点旋转等 ...

  4. MFC空间几何变换之图像平移、镜像、旋转、缩放

    本文主要讲述基于VC++6.0 MFC图像处理的应用知识,主要结合自己大三所学课程<数字图像处理>及课件进行讲解,主要通过MFC单文档视图实现显示BMP图片空间几何变换,包括图像平移.图形 ...

  5. 图像算法二:【图像几何变换】平移、镜像、转置、缩放、旋转、插值

    作为一个强大的科学计算软件,MATLAB广泛运用于较多领域,以其简单的编程风格著称.这篇文章便通过matlab语言来讲述如何进行图像的各种几何变换. 图像几何变换又称为图像空间变换,它是将一幅图像中的 ...

  6. 图像几何变换--缩放、平移、镜像、旋转

    目录 一.图像几何变换基础 二.平移 三.缩放 四.旋转 五.镜像 六.复合变换 七.变换矩阵总结 八.完整代码 一.图像几何变换基础 1.图像的几何变换是指原始图像按照需要产生大小.形状和位置的变化 ...

  7. 图片数据增强,包括模糊,亮度,裁剪,旋转,平移,镜像 ,python ,LabelImg,LabelMe工具

    图片数据增强,包括模糊,亮度,裁剪,旋转,平移,镜像 ,python ,LabelImg,LabelMe工具 1 对象检测图片数据增强(使用labelImg工具) 2 对象分割图片数据增强(使用lab ...

  8. python数字图像处理——几何变换(平移、旋转、放缩、镜像)

    一.平移 变换公式如下: 图像平移首先定义平移矩阵M,再调用warpAffine()函数实现平移,python函数如下: M = np.float32([[1, 0, x], [0, 1, y]]) ...

  9. 基于OpenCV做图像数据增强(平移、镜像、缩放、旋转、仿射)

    前言: 基于OpenCV的基本使用,对图像的数据量进行数据增强,使得框架对神经网络进行训练,提高模型的鲁棒性以及准确性. 原图: 1.平移 平移通过自定义平移矩阵以及函数warpAffine实现: 代 ...

  10. opencv图像处理之图像平移

    Hellow,我是jack,今天给大家分享的是图像平移.话不多说,让我们直接进入正题. 平移处理要求: 1)保证图像平移的完整性. 2)图像原始信息可能丢失 接下来让我们看下代码怎么实现: //平移操 ...

最新文章

  1. XML的简单读取与写入
  2. 视频导切台RGBlink 控制软件下载与测试
  3. JavaScript———从setTimeout与setInterval到AJAX异步
  4. 怎么将arcgis新建工具条如何保存_ArcGIS中寻找最短路径的方法
  5. JavaScript-创建日志调试对象(面向对象实例)
  6. 微软向Chromium贡献代码以优化浏览器滚动体验
  7. mysql极客_极客mysql16
  8. jquery如何获取checkbox的值
  9. 熵权法 —— python
  10. 汤姆大叔 深入理解JavaScript系列(20):《你真懂JavaScript吗?》答案详解 后六道题答案...
  11. c++对象回收问题_从垃圾回收解开Golang内存管理的面纱之三垃圾回收
  12. Python多线程编程中使用Barrier对象进行同步
  13. 蒲公英如何正确泡水喝?可以和哪些食物一起搭配?
  14. java swing 帮助文档,在Java Swing应用程序中创建在线帮助-使用pdf用户文档
  15. jmeter解决不同线程组之间传递参数问题
  16. 多元函数(multivariate function)分析(方向导数和梯度)
  17. PHP-FPM 设置多pool、配置文件重写
  18. CheerpJ转换JAVA使用最简教程
  19. MATLAB代码:计及碳排放交易及多种需求响应的微网/虚拟电厂日前优化调度
  20. 计算机网络英文题库(含答案) Chapter 1 Computer Networks and the Internet

热门文章

  1. Ubuntu上,如何成功的安装pygrib
  2. Xamarin University-----Xamarin Mobile Certification Exam考试经历(还没写完)
  3. 20170813xlVBA跨表筛选数据
  4. 设置布局默认为LinearLayout,却成了RelativeLayout
  5. POJ 3687 逆序拓扑
  6. [转]linux下IPTABLES配置详解
  7. 类数组变量定义与初始化
  8. poj 2828 线段树
  9. 关于System.identityHashCode(obj) 与 obj.hashcode()
  10. 如何在C#窗体中定义全局变量