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 Emgu.CV; using Emgu.Util; using Emgu.CV.Structure; namespace zhifangtu { public partial class Form1 : Form { Rectangle rc; public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { OpenFileDialog open = new OpenFileDialog(); if (open.ShowDialog() == DialogResult.OK) { this.pictureBox1.Load(open.FileName); IntPtr mm= CvInvoke.cvLoadImage(open.FileName,Emgu.CV.CvEnum.LOAD_IMAGE_TYPE.CV_LOAD_IMAGE_COLOR); Image<Bgr,byte> ptr =new Image<Bgr,byte>(open.FileName); rc = CvInvoke.cvGetImageROI(ptr); IntPtr m1=CvInvoke.cvCreateImage(rc.Size,Emgu.CV.CvEnum.IPL_DEPTH.IPL_DEPTH_8U,1); IntPtr m2=CvInvoke.cvCreateImage(rc.Size,Emgu.CV.CvEnum.IPL_DEPTH.IPL_DEPTH_8U,1); IntPtr m3=CvInvoke.cvCreateImage(rc.Size,Emgu.CV.CvEnum.IPL_DEPTH.IPL_DEPTH_8U,1); CvInvoke.cvSplit(ptr,m1,m2,m3,IntPtr.Zero);//分割图像B,G,R rc.Width = 512; rc.Height = 150; IntPtr n1 = CvInvoke.cvCreateImage(rc.Size, Emgu.CV.CvEnum.IPL_DEPTH.IPL_DEPTH_8U, 1); IntPtr n2 = CvInvoke.cvCreateImage(rc.Size, Emgu.CV.CvEnum.IPL_DEPTH.IPL_DEPTH_8U, 1); IntPtr n3 = CvInvoke.cvCreateImage(rc.Size, Emgu.CV.CvEnum.IPL_DEPTH.IPL_DEPTH_8U, 1); zhifangtu(m1, n1);//直方图函数,对单通道图像进行操作 zhifangtu(m2, n2); zhifangtu(m3, n3); Image<Gray,byte> nnn=new Image<Gray,byte>(rc.Width,rc.Height); CvInvoke.cvCopy(n1,nnn,IntPtr.Zero); pictureBox2.Image = nnn.ToBitmap();//显示到picturebox上 CvInvoke.cvCopy(n2, nnn, IntPtr.Zero); pictureBox3.Image = nnn.ToBitmap(); CvInvoke.cvCopy(n3, nnn, IntPtr.Zero); pictureBox4.Image = nnn.ToBitmap(); } } private void zhifangtu( IntPtr yy ,IntPtr nn) { int[] hist_size = new int[1] { 256 };//建一个数组来存放直方图数据 IntPtr HistImg = CvInvoke.cvCreateHist(1, hist_size, Emgu.CV.CvEnum.HIST_TYPE.CV_HIST_ARRAY, null, 1);//创建了一个空的直方图 IntPtr[] inPtr1 = new IntPtr[1] { yy}; CvInvoke.cvCalcHist(inPtr1, HistImg, false, System.IntPtr.Zero);//计算inPtr1指向图像的数据,并传入Histimg中,其中IntPtr[] inPtr1 = new IntPtr[1] { SubImg}。 int i; float min = 0, max = 0; int[] idx = new int[256]; int[] idy = new int[256]; CvInvoke.cvGetMinMaxHistValue(HistImg, ref min, ref max, idy, idx); double[] temphist = new double[256]; for (i = 0; i < 256; i++) { temphist[i] = CvInvoke.cvQueryHistValue_1D(HistImg, i); } //nn = CvInvoke.cvCreateImage(rc.Size, Emgu.CV.CvEnum.IPL_DEPTH.IPL_DEPTH_8U, 1); CvInvoke.cvZero(nn); int scale = 2; for (i = 0; i < 255; i++) { Point pt1 = new Point(); Point pt2 = new Point(); Point pt3 = new Point(); Point pt4 = new Point(); pt1.X = i * scale; pt1.Y = 64 * scale; pt2.X = (i + 1) * scale; pt2.Y = 64 * scale; pt3.X = (i + 1) * scale; pt3.Y = Convert.ToInt16((64 - (temphist[i + 1] / max) * 64) * scale); pt4.X = i * scale; pt4.Y = Convert.ToInt16((64 - (temphist[i] / max) * 64) * scale); int numPts = 5; Point[] pts = new Point[5]; pts[0] = pt1; pts[1] = pt2; pts[2] = pt3; pts[3] = pt4; pts[4] = pt1; Rgb mmmm = new Rgb(); mmmm.Red = 255; mmmm.Blue = 0; mmmm.Green = 0; int uu = 1; CvInvoke.cvFillConvexPoly(nn, pts, numPts, mmmm.MCvScalar, Emgu.CV.CvEnum.LINE_TYPE.CV_AA, uu); } } } }