实验一彩色图像转灰度
2014-04-17 13:10
204 查看
使用VS2008 开发工具,c# 编程语言条件下,进行图像显示,将真彩色图转化为灰度图。创建一个基本窗体应用程序,在窗体上添加一个openFileDialog 和pictureBox 控件,两个button 控件,一个label 标签,在窗体代码中添加变量声明:
效果图如下:
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Runtime.InteropServices; using System.IO; using System.Drawing.Imaging; using System.Threading; namespace TextOne { public partial class Form1 : Form { public Form1() { InitializeComponent(); } public static Bitmap bp_1; public static Bitmap bp_grey; public static MemoryStream ms_bmp; public static long bmp_file_len;//文件长度变量 public static IntPtr main_wnd_handle;//主窗体句柄 //定义消息常数 public const int TRAN_FINISHED = 0x500; //动态链接库引入 [DllImport("User32.dll", EntryPoint = "SendMessage")] private static extern int SendMessage( IntPtr hWnd, // handle to destination window int Msg, // message int wParam, // first message parameter int lParam // second message parameter ); private void Form1_Load(object sender, EventArgs e) { ms_bmp = new MemoryStream(5000000); main_wnd_handle = this.Handle; } protected override void DefWndProc(ref Message m) { switch (m.Msg) { case TRAN_FINISHED: label1.Text = " 图片灰度处理完成"; pictureBox1.Image = bp_grey; break; default: base.DefWndProc(ref m); break; } } private void button1_Click(object sender, EventArgs e) { if (openFileDialog1.ShowDialog() == DialogResult.OK) {//选择文件 bp_1 = new Bitmap(openFileDialog1.FileName); pictureBox1.Image = bp_1; } } static void thread_bmp_2_grey() { //线程流程 if (bp_1!=null) { ms_bmp.Seek(0, SeekOrigin.Begin); bp_1.Save(ms_bmp, System.Drawing.Imaging.ImageFormat.Bmp); bmp_file_len = ms_bmp.Position; //RGB2GRAY(r,g,b) (((b)*117 + (g)*601 + (r)*306) >> 10) byte[] buf_ms = ms_bmp.GetBuffer(); byte r_1, g_1, b_1, grey_val; int scan_line_len; switch (bp_1.PixelFormat) { case PixelFormat.Format32bppRgb: { scan_line_len = bp_1.Width * 4; for (int i_height = 0; i_height < bp_1.Height; i_height++) { for (int i_width = 0; i_width < bp_1.Width; i_width++) { b_1 = buf_ms[54 + i_height * scan_line_len + i_width * 4]; g_1 = buf_ms[54 + i_height * scan_line_len + i_width * 4 + 1]; r_1 = buf_ms[54 + i_height * scan_line_len + i_width * 4 + 2]; grey_val = (byte)(((b_1) * 117 + (g_1) * 601 + (r_1) * 306) >> 10); buf_ms[54 + i_height * scan_line_len + i_width * 4] = grey_val; buf_ms[54 + i_height * scan_line_len + i_width * 4 + 1] = grey_val; buf_ms[54 + i_height * scan_line_len + i_width * 4 + 2] = grey_val; } } break; } case PixelFormat.Format24bppRgb: { int i_bmp_width = bp_1.Width; int line_byte_count; //line_byte_count=i_bmp_width*3;24 位需要处理 line_byte_count = i_bmp_width * 3;//32 位直接计算 if ((line_byte_count % 4) == 0) { scan_line_len = line_byte_count; } else { scan_line_len = (line_byte_count / 4) * 4 + 4; } //24 位位图,因此每像素使用24 字节,没有alpha 字节 for (int i_height = 0; i_height < bp_1.Height; i_height++) { for (int i_width = 0; i_width < bp_1.Width; i_width++) { b_1 = buf_ms[54 + i_height * scan_line_len + i_width * 3]; g_1 = buf_ms[54 + i_height * scan_line_len + i_width * 3 + 1]; r_1 = buf_ms[54 + i_height * scan_line_len + i_width * 3 + 2]; grey_val = (byte)(((b_1) * 117 + (g_1) * 601 + (r_1) * 306) >> 10); buf_ms[54 + i_height * scan_line_len + i_width * 3] = grey_val; buf_ms[54 + i_height * scan_line_len + i_width * 3 + 1] = grey_val; buf_ms[54 + i_height * scan_line_len + i_width * 3 + 2] = grey_val; }//each pixel in one line } break; } } bp_grey = (Bitmap)Bitmap.FromStream(ms_bmp); SendMessage(main_wnd_handle, TRAN_FINISHED, 100, 100); } } private void button2_Click(object sender, EventArgs e) { Thread workThread = new Thread(new ThreadStart(thread_bmp_2_grey)); workThread.IsBackground = true; workThread.Start(); } } }
效果图如下:
相关文章推荐
- (实验一) --- 彩色图像变换成灰度图像---matlab实现
- JAVA实现彩色图像转黑白(灰度)
- 数字图像处理基础:教你如何区分单色图像、灰度图像、伪彩色图像、真彩色图像
- C++ OpenCV 实现RGB彩色图像转化成灰度图像再转换成二值图像
- 实验二 灰度图像的线性变换
- 彩色图像,二值图像,灰度图像,不要傻傻分不清~
- 彩色图像转灰度图像
- [OPENCV learning] 获取图像的灰度和一维RGB彩色直方图
- 基于matlab的彩色图像添加噪声实验
- 彩色图像、灰度图像、二值图像和索引图像区别
- MATLAB中彩色图像->灰度图像 的三种方式
- 二值图像、灰度图像、彩色图像
- 如果将彩色图像和灰度图像一起放进 CNN 中去,会是什么结果?
- openCV彩色图像转换为灰度图像
- 灰度图像和彩色图像互相转换
- VTK修炼之道23:图像基本操作_灰度图像映射成伪彩色图像(查表法)
- pytorch 彩色图像转灰度图像
- 图像边缘检测—sobel算子(灰度图像,彩色图像)
- opencv彩色图像转灰度图像
- RGB彩色图像与灰度图像转换 opencv实现 代码及分析