📄 form1.cs
字号:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace 基于伪彩的图像显示
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
/// <summary>
/// //////////////////////////////////////////////////////////////////////////////变量定义
/// </summary>
public int[,] irontable = new int[256, 4]{
//热金属编码表
{ 0, 0, 0,0 }, { 0, 0, 30,0 }, { 0, 0, 34,0 }, { 0, 0, 38,0 }, //4
{ 0, 0, 43,0 }, { 0, 0, 47,0 }, { 0, 0, 51,0 }, { 0, 0, 55,0 }, //8
{ 0, 0, 60,0 }, { 0, 0, 64,0 }, { 0, 0, 68,0 }, { 0, 0, 72,0 }, //12
{ 0, 0, 77,0 }, { 0, 0, 81,0 }, { 0, 0, 85,0 }, { 0, 0, 89,0 }, //16
{ 0, 0, 94,0 }, { 3, 0, 98,0 }, { 6, 0,102,0 }, { 9, 0,106,0 }, //20
{ 12, 0,111,0 }, { 15, 0,115,0 }, { 18, 0,119,0 }, { 21, 0,123,0 }, //24
{ 24, 0,128,0 }, { 27, 0,132,0 }, { 30, 0,136,0 }, { 33, 0,140,0 }, //28
{ 36, 0,145,0 }, { 39, 0,149,0 }, { 42, 0,153,0 }, { 45, 0,157,0 }, //32
{ 48, 0,162,0 }, { 51, 0,166,0 }, { 54, 0,169,0 }, { 57, 0,173,0 }, //36
{ 60, 0,177,0 }, { 63, 0,180,0 }, { 66, 0,184,0 }, { 69, 0,187,0 }, //40
{ 72, 0,190,0 }, { 75, 0,194,0 }, { 78, 0,197,0 }, { 81, 0,200,0 }, //44
{ 84, 0,204,0 }, { 87, 0,207,0 }, { 90, 0,210,0 }, { 93, 0,214,0 }, //48
{ 96, 0,217,0 }, { 99, 0,220,0 }, { 102, 0,223,0 }, { 105, 0,227,0 }, //52
{ 108, 0,230,0 }, { 111, 0,229,0 }, { 114, 0,228,0 }, { 117, 0,228,0 }, //56
{ 120, 0,227,0 }, { 123, 0,226,0 }, { 126, 0,226,0 }, { 129, 0,225,0 }, //60
{ 132, 0,224,0 }, { 135, 0,223,0 }, { 138, 0,219,0 }, { 141, 0,216,0 }, //64
{ 144, 0,212,0 }, { 147, 0,208,0 }, { 150, 0,205,0 }, { 153, 0,201,0 }, //68
{ 156, 0,197,0 }, { 157, 0,194,0 }, { 159, 0,190,0 }, { 160, 0,186,0 }, //72
{ 162, 0,183,0 }, { 163, 0,179,0 }, { 165, 0,175,0 }, { 166, 0,172,0 }, //76
{ 167, 0,168,0 }, { 168, 0,164,0 }, { 170, 0,161,0 }, { 171, 0,157,0 }, //80
{ 173, 0,153,0 }, { 174, 0,150,0 }, { 176, 0,146,0 }, { 177, 0,142,0 }, //84
{ 178, 0,139,0 }, { 179, 0,135,0 }, { 181, 0,131,0 }, { 182, 0,127,0 }, //88
{ 184, 0,124,0 }, { 185, 0,120,0 }, { 187, 0,116,0 }, { 188, 0,113,0 }, //92
{ 189, 0,109,0 }, { 190, 0,105,0 }, { 192, 0,102,0 }, { 193, 0, 98,0 }, //96
{ 195, 0, 94,0 }, { 196, 0, 91,0 }, { 198, 0, 87,0 }, { 199, 0, 83,0 }, //100
{ 200, 0, 80,0 }, { 201, 0, 77,0 }, { 203, 3, 73,0 }, { 204, 6, 69,0 }, //104
{ 206, 8, 66,0 }, { 207, 11, 62,0 }, { 209, 14, 58,0 }, { 210, 17, 55,0 }, //108
{ 211, 19, 51,0 }, { 212, 22, 47,0 }, { 214, 25, 44,0 }, { 215, 28, 40,0 }, //112
{ 217, 30, 37,0 }, { 218, 33, 34,0 }, { 220, 36, 30,0 }, { 221, 39, 26,0 }, //116
{ 222, 41, 22,0 }, { 223, 44, 18,0 }, { 225, 47, 15,0 }, { 226, 50, 11,0 }, //120
{ 228, 52, 7,0 }, { 229, 55, 4,0 }, { 231, 58, 0,0 }, { 232, 61, 0,0 }, //124
{ 233, 63, 0,0 }, { 234, 66, 0,0 }, { 236, 69, 0,0 }, { 237, 72, 0,0 }, //128
{ 239, 74, 0,0 }, { 240, 77, 0,0 }, { 242, 80, 0,0 }, { 243, 83, 0,0 }, //132
{ 244, 85, 0,0 }, { 245, 88, 0,0 }, { 247, 91, 0,0 }, { 248, 94, 0,0 }, //136
{ 250, 96, 0,0 }, { 252, 99, 0,0 }, { 253,102, 0,0 }, { 254,105, 0,0 }, //140
{ 255,107, 0,0 }, { 255,109, 0,0 }, { 255,111, 0,0 }, { 255,112, 0,0 }, //144
{ 255,114, 0,0 }, { 255,116, 0,0 }, { 255,118, 0,0 }, { 255,119, 0,0 }, //148
{ 255,121, 0,0 }, { 255,123, 0,0 }, { 255,125, 0,0 }, { 255,126, 0,0 }, //152
{ 255,128, 0,0 }, { 255,130, 0,0 }, { 255,132, 0,0 }, { 255,133, 0,0 }, //156
{ 255,135, 0,0 }, { 255,137, 0,0 }, { 255,139, 0,0 }, { 255,140, 0,0 }, //160
{ 255,142, 0,0 }, { 255,144, 0,0 }, { 255,146, 0,0 }, { 255,147, 0,0 }, //164
{ 255,149, 0,0 }, { 255,151, 0,0 }, { 255,153, 0,0 }, { 255,154, 0,0 }, //168
{ 255,156, 0,0 }, { 255,158, 0,0 }, { 255,160, 0,0 }, { 255,161, 0,0 }, //172
{ 255,163, 0,0 }, { 255,165, 0,0 }, { 255,167, 0,0 }, { 255,168, 0,0 }, //176
{ 255,170, 0,0 }, { 255,172, 0,0 }, { 255,174, 0,0 }, { 255,175, 0,0 }, //180
{ 255,177, 0,0 }, { 255,179, 0,0 }, { 255,181, 0,0 }, { 255,182, 0,0 }, //184
{ 255,184, 0,0 }, { 255,186, 0,0 }, { 255,188, 0,0 }, { 255,189, 0,0 }, //188
{ 255,191, 0,0 }, { 255,193, 0,0 }, { 255,195, 0,0 }, { 255,196, 0,0 }, //192
{ 255,198, 0,0 }, { 255,200, 0,0 }, { 255,202, 0,0 }, { 255,203, 0,0 }, //196
{ 255,205, 0,0 }, { 255,207, 0,0 }, { 255,209, 0,0 }, { 255,210, 0,0 }, //200
{ 255,212, 0,0 }, { 255,214, 0,0 }, { 255,216, 0,0 }, { 255,217, 0,0 }, //204
{ 255,219, 0,0 }, { 255,221, 0,0 }, { 255,223, 0,0 }, { 255,224, 0,0 }, //208
{ 255,226, 0,0 }, { 255,228, 0,0 }, { 255,230, 0,0 }, { 255,231, 0,0 }, //212
{ 255,233, 0,0 }, { 255,235, 0,0 }, { 255,237, 0,0 }, { 255,238, 6,0 }, //216
{ 255,240, 13,0 }, { 255,242, 19,0 }, { 255,244, 26,0 }, { 255,245, 32,0 }, //220
{ 255,247, 39,0 }, { 255,249, 45,0 }, { 255,251, 52,0 }, { 255,253, 58,0 }, //224
{ 255,255, 65,0 }, { 255,255, 71,0 }, { 255,255, 78,0 }, { 255,255, 84,0 }, //228
{ 255,255, 90,0 }, { 255,255, 96,0 }, { 255,255,102,0 }, { 255,255,108,0 }, //232
{ 255,255,114,0 }, { 255,255,120,0 }, { 255,255,126,0 }, { 255,255,132,0 }, //236
{ 255,255,138,0 }, { 255,255,144,0 }, { 255,255,150,0 }, { 255,255,156,0 }, //240
{ 255,255,162,0 }, { 255,255,168,0 }, { 255,255,174,0 }, { 255,255,182,0 }, //244
{ 255,255,188,0 }, { 255,255,194,0 }, { 255,255,201,0 }, { 255,255,207,0 }, //248
{ 255,255,213,0 }, { 255,255,219,0 }, { 255,255,225,0 }, { 255,255,231,0 }, //252
{ 255,255,237,0 }, { 255,255,243,0 }, { 255,255,249,0 }, { 255,255,255,0 } //256
};
float max, min; //图像温度的最值
TemTable temtable = new TemTable(); //用颜色饱和度临时模拟温度,此用于临时存放温度
/// <summary>
/// //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button1_Click(object sender, EventArgs e) //打开一幅图像
{
openFileDialog1.ShowDialog();
if (openFileDialog1.FileName == "")
return;
this.pictureBox1.Image = Bitmap.FromFile(openFileDialog1.FileName);
}
private void button2_Click(object sender, EventArgs e) //保存显示的图像
{
this.saveFileDialog1.ShowDialog();
if (this.saveFileDialog1.FileName.Trim() == "")
return;
this.pictureBox1.Image.Save(this.saveFileDialog1.FileName);
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
Bitmap Mybitmap = (Bitmap)this.pictureBox1.Image; //要处理的图像
int bytes = Mybitmap.Width * Mybitmap.Height * 3; //数据数目
byte[] rgbvalue = new byte[bytes]; //存放原始图像的颜色值320*240*3
Rectangle rect = new Rectangle(pictureBox1.Location, pictureBox1.Size);
BitmapData bmdata = Mybitmap.LockBits(rect, ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb); //图像颜色锁入内存
IntPtr ptr = bmdata.Scan0; //图像首行地址
Marshal.Copy(ptr, rgbvalue, 0, bytes); //图像颜色值复制到处理数组
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
this.comboBox1.Text = ""; //清空上次显示的信息
string pal = this.comboBox1.SelectedItem.ToString(); //将选定的调色板名称显示在下拉列表里
this.comboBox1.Text = pal;
this.comboBox1.Refresh();
max = temtable.table[0];
min = temtable.table[0]; //图像温度的最大值最小值
for (int i = 1; i < temtable.table.Length; i++) //默认以图象温度最大值,最小值为限,显示图像
{
if (temtable.table[i] > max)
max = temtable.table[i];
if (temtable.table[i] < min)
min = temtable.table[i];
}
label2.Text = max.ToString(); //温度上下限的显示
label3.Text = min.ToString();
label4.Text = "温度上限:" + max.ToString();
label5.Text = "温度下限:" + min.ToString();
trackBar1.Value = 50; //默认最值分别处于trackBar中间,这样可调的空间大些,便于调节
trackBar2.Value = 50; //即trackBar1中间值为图像最大值,trackBar2中间值为图像最小值
switch (pal)
{
case "灰度": //基于灰度伪彩编码的显示
// GreyTable greytable = new GreyTable();
///////////////////////////////////////////////////灰度调色板颜色的显示
Bitmap greypal = new Bitmap(pictureBox2.Width, pictureBox2.Height); //pictureBox2.Height=256
for (int i = 0; i < 256; i++)
{
for (int j = 0; j < pictureBox2.Width; j++)
{
greypal.SetPixel(j, i, Color.FromArgb(255 - i, 255 - i, 255 - i)); //直接赋值而不开辟新空间会快些
}
}
pictureBox2.Image = greypal;
///////////////////////////////////////////////////////数据的处理
for (int i = 0; i < temtable.table.Length; i = i + 1)
{
int t = (int)((temtable.table[i] - min) * 1.0 / (max - min)*256);
if (t >= 255)
t = 255;
else if (t < 0)
t = 0;
rgbvalue[3 * i] = (byte)t; //直接赋值而不开辟新空间会快些
rgbvalue[3 * i + 1] = (byte)t;
rgbvalue[3 * i + 2] = (byte)t;
// Mybitmap.SetPixel(i%640, i / 640, Color.FromArgb(greytable.table[t, 0], greytable.table[t, 1], greytable.table[t,2]));
//用这条语句速度太慢
}
break;
case "逆灰度": //基于逆灰度伪彩编码的显示
///////////////////////////////////////////////////逆灰度调色板颜色的显示
Bitmap congreypal = new Bitmap(pictureBox2.Width, pictureBox2.Height); //pictureBox2.Height=256
for (int i = 0; i < 256; i++)
{
for (int j = 0; j < pictureBox2.Width; j++)
{
congreypal.SetPixel(j, i, Color.FromArgb( i,i,i)); //直接赋值而不开辟新空间会快些
}
}
pictureBox2.Image = congreypal;
///////////////////////////////////////////////////////数据的处理
for (int i = 0; i < temtable.table.Length; i = i + 1)
{
int t = (int)((temtable.table[i] - min) * 1.0 / (max - min) * 256);
if (t >= 255)
t = 255;
else if (t < 0)
t = 0;
rgbvalue[3 * i] = (byte)(255 - t); //直接赋值而不开辟新空间会快些
rgbvalue[3 * i + 1] = (byte)(255 - t);
rgbvalue[3 * i + 2] = (byte)(255 - t);
}
break;
case "黄色": //基于黄色伪彩编码的显示
///////////////////////////////////////////////////黄色饱和度调色板颜色的显示
Bitmap yellowpal = new Bitmap(pictureBox2.Width, pictureBox2.Height); //pictureBox2.Height=256
for (int i = 0; i < 256; i++)
{
for (int j = 0; j < pictureBox2.Width; j++)
{
yellowpal.SetPixel(j, i, Color.FromArgb(255-i,255-i, 0)); //直接赋值而不开辟新空间会快些
}
}
pictureBox2.Image = yellowpal;
///////////////////////////////////////////////////////数据的处理
for (int i = 0; i < temtable.table.Length; i = i + 1)
{
int t = (int)((temtable.table[i] - min) * 1.0 / (max - min) * 256);
if (t >= 255)
t = 255;
else if (t < 0)
t = 0;
rgbvalue[3 * i] = 0; //直接赋值而不开辟新空间会快些
rgbvalue[3 * i + 1] = (byte)t;
rgbvalue[3 * i + 2] = (byte)t;
}
break;
case "热金属": //基于热金属伪彩编码的显示
///////////////////////////////////////////////////热金属调色板颜色的显示
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -