C#图像镜像
来源:互联网 发布:阿里云上传api 编辑:程序博客网 时间:2024/06/11 08:08
C#水平垂直图像镜像---C#数字图像处理算法典型实例.赵春江
原理:
变换前
灰度图像--->像素序号=c+r*w(当前列+当前行*列数)
彩色图像--->像素序号=0+c*3+r*w*3
像素序号=1+c*3+r*w*3
像素序号=2+c*3+r*w*3
变换后参考代码
private void start_Click(object sender, EventArgs e) { Rectangle rect= new Rectangle(0,0,_bmp.Width,_bmp.Height); BitmapData bmpdata = _bmp.LockBits(rect, ImageLockMode.ReadWrite, _bmp.PixelFormat); IntPtr ptr = bmpdata.Scan0; int bytes=0; if(_bmp.PixelFormat==PixelFormat.Format8bppIndexed)//判断是灰度色图像还是彩色图像,给相应的大小 { bytes=_bmp.Width*_bmp.Height; } else if(_bmp.PixelFormat==PixelFormat.Format24bppRgb) { bytes = _bmp.Width * _bmp.Height * 3; } byte[] pixelValues=new byte[bytes]; System.Runtime.InteropServices.Marshal.Copy(ptr, pixelValues, 0, bytes);//内存法,从内存中将像素复制到pixelValues数组 int halfWidth = _bmp.Width / 2; int halfheigth= _bmp.Height/2; byte temp; byte temp1; byte temp2; //灰度图像 if (_bmp.PixelFormat==PixelFormat.Format8bppIndexed) { if (HorV) { for (int r = 0; r < _bmp.Height; r++) for (int c = 0; c < halfWidth; c++) { temp = pixelValues[r * _bmp.Width + c]; pixelValues[r * _bmp.Width + c] = pixelValues[(r + 1) * _bmp.Width - c - 1]; pixelValues[(r + 1) * _bmp.Width - c - 1] = temp; } } else { for (int c = 0; c < _bmp.Width; c++) { for (int r = 0; r < halfheigth; r++) { temp = pixelValues[r * _bmp.Width + c]; pixelValues[r * _bmp.Width + c] = pixelValues[(_bmp.Height - r - 1) * _bmp.Width + c]; pixelValues[(_bmp.Height - r - 1) * _bmp.Width + c] = temp; } } } } else if (_bmp.PixelFormat == PixelFormat.Format24bppRgb)//彩色图像 { if (HorV) { for (int r = 0; r < _bmp.Height; r++) for (int c = 0; c < halfWidth; c++) { temp = pixelValues[0 + r * _bmp.Width * 3 + c * 3]; temp1 = pixelValues[1 + r * _bmp.Width * 3 + c * 3]; temp2 = pixelValues[2 + r * _bmp.Width * 3 + c * 3]; pixelValues[0 + r * _bmp.Width * 3 + c * 3] = pixelValues[0 + (r + 1) * _bmp.Width * 3 - (c + 1) * 3]; pixelValues[1 + r * _bmp.Width * 3 + c * 3] = pixelValues[1 + (r + 1) * _bmp.Width * 3 - (c + 1) * 3]; pixelValues[2 + r * _bmp.Width * 3 + c * 3] = pixelValues[2 + (r + 1) * _bmp.Width * 3 - (c + 1) * 3]; pixelValues[0 + (r + 1) * _bmp.Width * 3 - (c + 1) * 3]=temp; pixelValues[1 + (r + 1) * _bmp.Width * 3 - (c + 1) * 3]=temp1; pixelValues[2 + (r + 1) * _bmp.Width * 3 - (c + 1) * 3] = temp2; } } else { for (int c = 0; c < _bmp.Width; c++) { for (int r = 0; r < halfheigth; r++) { temp = pixelValues[0 + r * _bmp.Width * 3 + c * 3]; temp1 = pixelValues[1 + r * _bmp.Width * 3 + c * 3]; temp2 = pixelValues[2 + r * _bmp.Width * 3 + c * 3]; pixelValues[0 + r * _bmp.Width * 3 + c * 3] = pixelValues[0 + (_bmp.Height - r - 1) * _bmp.Width * 3 + c * 3]; pixelValues[1 + r * _bmp.Width * 3 + c * 3] = pixelValues[1 + (_bmp.Height - r - 1) * _bmp.Width * 3 + c * 3]; pixelValues[2 + r * _bmp.Width * 3 + c * 3] = pixelValues[2 + (_bmp.Height - r - 1) * _bmp.Width * 3 + c * 3]; pixelValues[0 + (_bmp.Height - r - 1) * _bmp.Width * 3 + c * 3] = temp; pixelValues[1 + (_bmp.Height - r - 1) * _bmp.Width * 3 + c * 3] = temp1; pixelValues[2 + (_bmp.Height - r - 1) * _bmp.Width * 3 + c * 3] = temp2; } } } } System.Runtime.InteropServices.Marshal.Copy(pixelValues, 0, ptr, bytes); _bmp.UnlockBits(bmpdata); _pictureBox.Image = _bmp;水平镜像效果
垂直镜像效果
解决方案已上传:http://download.csdn.net/download/saw009/10169694点击打开链接
阅读全文
0 0
- C#图像镜像
- C#图像处理初学之平移和镜像
- 图像镜像翻转
- 图像镜像翻转
- 图像镜像
- C++ 图像镜像
- 图像镜像
- 图像镜像
- RGB图像镜像
- 图像镜像
- 图像镜像变换
- matlab图像镜像
- 图像镜像_matlab
- 图像镜像之水平镜像
- 图像镜像之竖直镜像
- 图像镜像与图像转置
- 图像镜像变换
- 图像处理之镜像
- maven使用Nexus私有仓库进行管理
- 如何用String字符串生成JSONObject和JSONArray数据
- man命令配置的注意事项
- 简述configure,pkg-config,pkg_config_path三者的关系
- 关于ftp服务器的配置与启动
- C#图像镜像
- js解决回车事件不兼容火狐、IE的问题
- Android 仿今日头条的视频播放控件(使用开源库灰常简单)
- firefox浏览器集成selenium插件,myeclipse集成TestNG,以及selenium相应jar包
- idea安装activiti插件
- spl_autoload_register比__aotuload的好处:
- multer使用示例
- linux系统添加管理员的方法
- Mmseg分词算法及实现