c#数字图像处理(九)图像镜像
private void mirror_Click(object sender, EventArgs e)
{
if (curBitmap!=null)
{
mirror mirForm = new mirror();
if (mirForm.ShowDialog()==DialogResult.OK)
{
Rectangle rect = new Rectangle(, , curBitmap.Width, curBitmap.Height);
BitmapData bmpData = curBitmap.LockBits(rect, ImageLockMode.ReadWrite, curBitmap.PixelFormat);
IntPtr ptr = bmpData.Scan0;
int bytes = ;
////判断是灰度色图像还是彩色图像,给相应的大小
if (curBitmap.PixelFormat==PixelFormat.Format8bppIndexed)
{
bytes= curBitmap.Width * curBitmap.Height;
}
else if (curBitmap.PixelFormat == PixelFormat.Format24bppRgb)
{
bytes = curBitmap.Width * curBitmap.Height * ;
}
byte[] pixelValues = new byte[bytes];
Marshal.Copy(ptr, pixelValues, , bytes); //水平中轴
int halfWidth = curBitmap.Width / ;
//垂直中轴
int halfHeight = curBitmap.Height / ;
byte temp;
byte temp1;
byte temp2;
byte temp3;
if (curBitmap.PixelFormat == PixelFormat.Format8bppIndexed)
{
if (mirForm.GetMirror)
{
for (int i = ; i < curBitmap.Height; i++)
for (int j = ; j < halfWidth; j++)
{
temp = pixelValues[i * curBitmap.Width + j];
pixelValues[i * curBitmap.Width + j] = pixelValues[(i + ) * curBitmap.Width - j - ];
pixelValues[(i + ) * curBitmap.Width - j - ] = temp;
}
}
else
{
for (int j = ; j < curBitmap.Width; j++)
{
for (int i = ; i < halfHeight; i++)
{
temp = pixelValues[i * curBitmap.Width + j];
pixelValues[i * curBitmap.Width + j] = pixelValues[(curBitmap.Height - i - ) * curBitmap.Width + j];
pixelValues[(curBitmap.Height - i - ) * curBitmap.Width + j] = temp;
}
}
}
}
else if (curBitmap.PixelFormat == PixelFormat.Format24bppRgb)
{
if (mirForm.GetMirror)
{
//水平镜像处理
for (int i = ; i < curBitmap.Height; i++)
{
//每个像素的三个字节在水平镜像时顺序不能变,所以这个方法不能用
//for (int j = 0; j < halfWidth; j++)
//{
// //以水平中轴线为对称轴,两边像素值交换
// temp = pixelValues[i * curBitmap.Width * 3 + j * 3];
// pixelValues[i * curBitmap.Width * 3 + j * 3] = pixelValues[(i + 1) * curBitmap.Width * 3 - 1 - j * 3];
// pixelValues[(i + 1) * curBitmap.Width * 3 - 1 - j * 3] = temp;
//}
for (int j = ; j < halfWidth; j++)
{//每三个字节组成一个像素,顺序不能乱
temp = pixelValues[ + i * curBitmap.Width * + j * ];
temp1 = pixelValues[ + i * curBitmap.Width * + j * ];
temp2 = pixelValues[ + i * curBitmap.Width * + j * ];
pixelValues[ + i * curBitmap.Width * + j * ] = pixelValues[ + (i + ) * curBitmap.Width * - (j + ) * ];
pixelValues[ + i * curBitmap.Width * + j * ] = pixelValues[ + (i + ) * curBitmap.Width * - (j + ) * ];
pixelValues[ + i * curBitmap.Width * + j * ] = pixelValues[ + (i + ) * curBitmap.Width * - (j + ) * ];
pixelValues[ + (i + ) * curBitmap.Width * - (j + ) * ] = temp;
pixelValues[ + (i + ) * curBitmap.Width * - (j + ) * ] = temp1;
pixelValues[ + (i + ) * curBitmap.Width * - (j + ) * ] = temp2;
}
}
}
else
{
//垂直镜像处理
for (int i = ; i < curBitmap.Width * ; i++)
{
for (int j = ; j < halfHeight; j++)
{
//以垂直中轴线为对称轴。两边像素值互换
temp = pixelValues[j * curBitmap.Width * + i];
pixelValues[j * curBitmap.Width * + i] = pixelValues[(curBitmap.Height - j - ) * curBitmap.Width * + i];
pixelValues[(curBitmap.Height - j - ) * curBitmap.Width * + i] = temp;
}
}
}
} Marshal.Copy(pixelValues, , ptr, bytes);
curBitmap.UnlockBits(bmpData);
}
Invalidate();
}
}
原图:

水平镜像:

垂直镜像:

c#数字图像处理(九)图像镜像的更多相关文章
- Win8 Metro(C#) 数字图像处理--1 图像打开,保存
原文:Win8 Metro(C#) 数字图像处理--1 图像打开,保存 作为本专栏的第一篇,必不可少的需要介绍一下图像的打开与保存,一便大家后面DEMO的制作. Win8Metro编程中,图像相关 ...
- Win8 Metro(C#)数字图像处理--4图像颜色空间描述
原文:Win8 Metro(C#)数字图像处理--4图像颜色空间描述 图像颜色空间是图像颜色集合的数学表示,本小节将针对几种常见颜色空间做个简单介绍. /// <summary> / ...
- 数字图像处理,图像锐化算法的C++实现
http://blog.csdn.net/ebowtang/article/details/38961399 之前一段我们提到的算法都是和平滑有关, 经过平滑算法之后, 图像锐度降低, 降低到一定程度 ...
- 数字图像处理:图像的灰度变换(Matlab实现)
(1)线性变换:通过建立灰度映射来调整源图像的灰度. k>1增强图像的对比度:k=1调节图像亮度,通过改变d值达到调节亮度目的:0 i = imread('theatre.jpg');i = i ...
- 数字图像处理界标准图像 Lena 后面的故事
熟悉图像处理或者压缩的工程师.研究人员和学生,经常在他们的实验或者项目任务里使用"Lenna"或者"Lena"的图像.Lenna 图像已经成为被广泛使用的测试图 ...
- 【数字图像处理】六.MFC空间几何变换之图像平移、镜像、旋转、缩放具体解释
本文主要讲述基于VC++6.0 MFC图像处理的应用知识,主要结合自己大三所学课程<数字图像处理>及课件进行解说,主要通过MFC单文档视图实现显示BMP图片空间几何变换.包含图像平移.图形 ...
- Win8Metro(C#)数字图像处理--2.19图像水平镜像
原文:Win8Metro(C#)数字图像处理--2.19图像水平镜像 [函数名称] 图像水平镜像函数MirrorXProcess(WriteableBitmap src) [函数代码] ...
- Win8Metro(C#)数字图像处理--2.20图像垂直镜像
原文:Win8Metro(C#)数字图像处理--2.20图像垂直镜像 [函数名称] 图像垂直镜像函数MirrorYProcess(WriteableBitmap src) [函数代码] ...
- 【数字图像处理】五.MFC图像点运算之灰度线性变化、灰度非线性变化、阈值化和均衡化处理具体解释
本文主要讲述基于VC++6.0 MFC图像处理的应用知识,主要结合自己大三所学课程<数字图像处理>及课件进行解说.主要通过MFC单文档视图实现显示BMP图片点运算处理.包含图像灰度线性变换 ...
- Win8 Metro(C#)数字图像处理--3.2图像方差计算
原文:Win8 Metro(C#)数字图像处理--3.2图像方差计算 /// <summary> /// /// </summary>Variance computing. / ...
随机推荐
- 一个vue管理系统的初步搭建总结
ps:目前这个项目只是有一个大致的框架,并没有做完 前期准备工作 前端构建工具:Visual Studio Code后端构建工具:IDEA数据库和服务器构建工具:WampServer (使用的是2.4 ...
- dotnet 使用 lz4net 压缩 Stream 或文件
在 dotnet 可以使用 LZ4 这个无损的压缩算法,这个压缩算法的压缩率不高但是速度很快.这个库支持在 .NET Standard 1.6 .NET Core .NET Framework Mon ...
- 【Docker】安装MySQL彻底解决3306端口占用问题
1.问题闪现: 初次up mysql报3306端口被占用 yunduo@YunDuo:~/Work/Learning/Docker/docker_compose$ docker-compose up ...
- JSPs
简介 Tomcat 8.0 使用 Jasper 2 JSP 引擎去实现 JavaServer Pages 2.3 规范. Jasper 2 经过了重新设计,极大改善了上一版 Jasper 的性能.除了 ...
- 《带你装B,带你飞》pytest修仙之路3 - setup/teardown
1. 简介 学过unittest的都知道里面用前置和后置setup和teardown非常好用,在每次用例开始前和结束后都去执行一次.当然还有更高级一点的setupClass和teardownClass ...
- Java截图小程序源码
Java编写的全屏截图小程序 package cnom.test.testUtils; import java.awt.AWTException; import java.awt.Dimension; ...
- 应届生/社招面试最爱问的几道Java基础问题
本文已经收录自笔者开源的 JavaGuide: https://github.com/Snailclimb ([Java学习+面试指南] 一份涵盖大部分Java程序员所需要掌握的核心知识)如果觉得不错 ...
- linux下安装OpenCV-2.4
OpenCV(Open Source Computer Vision Library),是一个跨平台计算机视觉库,实现了图像处理和计算机视觉方面的很多通用算法. OpenCV由一系列 C 函数和少量 ...
- iSO垂直滑动条VerticalSlider
由于项目需要实现一个垂直的Slider,滑动条使用UIlabel实现,按钮使用UIButton,按钮可以设置背景图片,代码如下 VerticalSlider.h // // VerticalSlide ...
- 初级程序员如何一分钟?解决一个BUG
博主说明 -- 重要.重要.重要的事情说三遍 写这篇文章是主要锻炼写博客的能力以及记录自己的成长经历,要是写的不对欢迎大佬评论指正,同时希望对大家有所帮助.然后我写博客尽量简洁+图片+宏观的方式,便于 ...