c#图片处理绘制直方图
-绘制直方图、线性点运算、灰度拉伸、直方图均衡、直方图匹配
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Drawing2D;
namespace point_operation
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void histogram_Click(object nder, EventArgs e)//绘制直方图
{
if (curBitmap != null)
{
histForm histoGram = new histForm(curBitmap);
histoGram.ShowDialog();
histoGram.Clo();
}
}
private void clo_Click(object nder, EventArgs e)
{
this.Clo();
}
private void open_Click(object nder, 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;
try
{
curBitmap = (Bitmap)Image.FromFile(curFileName);
}孔子登山
catch (Exception exp)
{
MessageBox.Show(exp.Message);
}
}
Invalidate();
特别好看的手机壁纸
}
private void Form1_Paint(object nder, PaintEventArgs e)
{
Graphics g = e.Graphics;
if (curBitmap != null)
{
g.DrawImage(curBitmap, 160, 20, curBitmap.Width, curBitmap.Height);
}
}
private void linearPO_Click(object nder, EventArgs e)//线性点运算
{
if (curBitmap != null)
{
linearPOForm linearForm = new linearPOForm();
if (linearForm.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;
int bytes = curBitmap.Width * curBitmap.Height;
byte[] grayValues = new byte[bytes];
System.Runtime.InteropServices.Marshal.Copy(
ptr, grayValues, 0, bytes);
int temp = 0;
double a = Convert.ToDouble(linearForm.GetScaling);
double b = Convert.ToDouble(linearForm.GetOfft);
for (int i = 0; i < bytes; i++)
{
temp = (int)(a * grayValues[i] + b + 0.5);
if (temp > 255)
{
grayValues[i] = 255;
}
el if (temp < 0)
{
grayValues[i] = 0;
}
el
grayValues[i] = (byte)temp;
}
System.Runtime.InteropServices.Marshal.Copy(grayValues, 0, ptr, bytes);
curBitmap.UnlockBits(bmpData);
}
linearForm.Clo();
Invalidate();
}
}
private void stretch_Click(object nder, EventArgs e)//灰度拉伸
{
if (curBitmap != null)
{
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;
int bytes = curBitmap.Width * curBitmap.Height;
byte[] grayValues = new byte[bytes];
System.Runtime.InteropServices.Marshal.Copy(ptr, grayValues, 0, bytes);
byte a = 255, b = 0;
double p;
for (int i = 0; i < bytes; i++)
{
if (a > grayValues[i])
{
a = grayValues[i];
}
if (b < grayValues[i])
{
b = grayValues[i];
}
}
p = 255.0 / (b - a);
for (int i = 0; i < bytes; i++)
{
grayValues[i] = (byte)(p * (grayValues[i] - a) + 0.5);
}
System.Runtime.InteropServices.Marshal.Copy(grayValues, 0, ptr, bytes);
curBitmap.UnlockBits(bmpData);
Invalidate();
}
}
private void equalization_Click(object nder, EventArgs e)//直方图均衡
{
if (curBitmap != null)
{
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;
int bytes = curBitmap.Width * curBitmap
.Height;
byte[] grayValues = new byte[bytes];
System.Runtime.InteropServices.Marshal.Copy(ptr, grayValues, 0, bytes);
byte temp;
int[] countPixel = new int[256];
int[] tempArray = new int[256];
//Array.Clear(tempArray, 0, 256);
byte[] pixelMap = new byte[256];
for (int i = 0; i < bytes; i++)
{
temp = grayValues[i];凤凰的寓意
countPixel[temp]++;
}
for (int i = 0; i < 256; i++)
{
if (i != 0)
{
tempArray[i] = tempArray[i - 1] + countPixel[i];
}
el
{
tempArray[0] = countPixel[0];
}
pixelMap[i] = (byte)(255.0 * tempArray[i] / bytes + 0.5);
}
for (int i = 0; i < bytes; i++)
{
temp = grayValues[i];
grayValues[i] = pixelMap[temp];
}
System.Runtime.InteropServices.Marshal.Copy(grayValues, 0, ptr, bytes);
curBitmap.UnlockBits(bmpData);
Invalidate();
}
}
private void shaping_Click(object nder, EventArgs e)//直方图匹配
{
if (curBitmap != null)
{
shapingForm sForm = new shapingForm();
if (sForm.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;
21xxx
int bytes = curBitmap.Width * curBitmap.Height;
byte[] grayValues = new byte[bytes];
System.Runtime.InteropServices.Marshal.Copy(ptr, grayValues, 0, bytes);防晒霜和隔离霜先用哪个
byte temp = 0;
double[] PPixel = new double[256];
double[] QPixel = new double[256];
int[] qPixel = new int[256];
int[] tempArray = new int[256];
for (int i = 0; i < grayValues.Length; i++)
{
temp = grayValues[i];
qPixel[temp]++;
}
for (int i = 0; i < 256; i++)
{
if (i != 0)
{
tempArray[i] = tempArray[i - 1] + qPixel[i];
}
el
{
tempArray[0] = qPixel[0];
}
香槟的花语QPixel[i] = (double)tempArray[i] / (double)bytes;
}
PPixel = sForm.ApplicationP;
double diffA, diffB;
byte k = 0;
byte[] mapPixel = new byte[256];
for (int i = 0; i < 256; i++)
{
diffB = 1;
for (int j = k; j < 256; j++)
{
diffA = Math.Abs(QPixel[i] - PPixel[j]);
if (diffA - diffB < 1.0E-08)
{
diffB = diffA;
k = (byte)j;
}
el
{
k = (byte)(j - 1);
break;
}
}
if (k == 255)
{
for (int l = i; l < 256; l++)
{
mapPixel[l] = k;
}
break;
}
mapPixel[i] = k;
}
for (int i = 0; i < bytes; i++)
{
temp = grayValues[i];
grayValues[i] = mapPixel[temp];
}
System.Runtime.InteropServices.Marshal.Copy(grayValues, 0, ptr, bytes);
curBitmap.UnlockBits(bmpData);
}
Invalidate();
}
}
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace point_operation
{
public partial class histForm : Form
{
public histForm(Bitmap bmp)
{
InitializeComponent();
bmpHist = bmp;
countPixel = new int[256];
}
private void clo_Click(object nder, EventArgs e)
{
this.Clo();
}
private void histForm_Paint(object nder, PaintEventArgs e)
{
Pen curPen = new Pen(Brushes.Black, 1);
Graphics g = e.Graphics;
g.DrawLine(curPen, 50, 240, 320, 240);
g.DrawLine(curPen, 50, 240, 50, 30);
g.DrawLine(curPen, 100, 240, 100, 242);
g.DrawLine(curPen, 150, 240, 150, 242);
g.DrawLine(curPen, 200, 240, 200, 242);
g.DrawLine(curPen, 250, 240, 250, 242);
g.DrawLine(curPen, 300, 240, 300, 242);
g.DrawString("0", new Font("New Timer", 8), Brushes.Black, new PointF(46, 2
42));
g.DrawString("50", new Font("New Timer", 8), Brushes.Black, new PointF(92, 242));
g.DrawString("100", new Font("New Timer", 8), Brushes.Black, new PointF(139, 242));
小鸟怎么画简单又好看g.DrawString("150", new Font("New Timer", 8), Brushes.Black, new PointF(189, 242));
g.DrawString("200", new Font("New Timer", 8), Brushes.Black, new PointF(239, 242));
g.DrawString("250", new Font("New Timer", 8), Brushes.Black, new PointF(289, 242));
g.DrawLine(curPen, 48, 40, 50, 40);
g.DrawString("0", new Font("New Timer", 8), Brushes.Black, new PointF(34, 234));
g.DrawString(maxPixel.ToString(), new Font("New Timer", 8), Brushes.Black, new PointF(18, 34));
double temp = 0;
for (int i = 0; i < 256; i++)
{
temp = 200.0 * countPixel[i] / maxPixel;
g.DrawLine(curPen, 50 + i, 240, 50 + i, 240 - (int)temp);
}
curPen.Dispo();
}
private void histForm_Load(object nder, EventArgs e)
{
Rectangle rect = new Rectangle(0, 0, bmpHist.Width, bmpHist.Height);
System.Drawing.Imaging.BitmapData bmpData = bmpHist.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadWrite, bmpHist.PixelFormat);
IntPtr ptr = bmpData.Scan0;
int bytes = bmpHist.Width * bmpHist.Height;
byte[] grayValues = new byte[bytes];
System.Runtime.InteropServices.Marshal.Copy(ptr, grayValues, 0, bytes);
byte temp = 0;
maxPixel = 0;
Array.Clear(countPixel, 0, 256);
for (int i = 0; i < bytes; i++)
{
temp = grayValues[i];
countPixel[temp]++;
if (countPixel[temp] > maxPixel)
{
maxPixel = countPixel[temp];
}
}
System.Runtime.InteropServices.Marshal.Copy(grayValues, 0, ptr, bytes);
bmpHist.UnlockBits(bmpData);
}
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace point_operation
{
public partial class linearPOForm : Form
田径
{
public linearPOForm()
{
InitializeComponent();
}
private void startLinear_Click(object nder, EventArgs e)
{
this.DialogResult = DialogResult.OK;
}
private void clo_Click(object nder, EventArgs e)
{
this.Clo();
}
public string GetScaling
{
get
{
return scaling.Text;
}
}
public string GetOfft
{
get
{
return offt.Text;
}
}
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace point_operation
{
public partial class shapingForm : Form
{
public shapingForm()
{
InitializeComponent();
shapingPixel = new int[256];
cumHist = new double[256];
}
private void open_Click(object nder, 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)
{
shapingFileName = opnDlg.FileName;
try
{
shapingBitmap = (Bitmap)Image.FromFile(shapingFileName);
}
catch (Exception exp)
{
MessageBox.Show(exp.Message);
}
Rectangle rect = new Rectangle(0, 0, shapingBitmap.Width, shapingBitmap.Height);
System.Drawing.Imaging.BitmapData bmpData = shapingBitmap.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadWrite, shapingBitmap.PixelFormat);
IntPtr ptr = bmpData.Scan0;
shapingSize = shapingBitmap.Width * shapingBitmap.Height;
byte[] grayValues = new byte[shapingSize];
System.Runtime.InteropServices.Marshal.Copy(ptr, grayValues, 0, shapingSize);
byte temp = 0;
maxPixel = 0;
Array.Clear(shapingPixel,0,256);
for (int i = 0; i < shapingSize; i++)
{
temp = grayValues[i];
shapingPixel[temp]++;
if (shapingPixel[temp] > maxPixel)
{
maxPixel = shapingPixel[temp];
}
}
System.Runtime.InteropServices.Marshal.Copy(grayValues, 0, ptr, shapingSize);
shapingBitmap.UnlockBits(bmpData);
}
Invalidate();
}
private void startShaping_Click(object nder, EventArgs e)
{
this.DialogResult = DialogResult.OK;
}
private void clo_Click(object nder, EventArgs e)
{
this.Clo();
}
private void shapingForm_Paint(object nder, PaintEventArgs e)
{
if (shapingBitmap != null