。
public partial class Form2 : Form { public Form2() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { var srcImage = new Mat(); var openFileDialog = new OpenFileDialog(); openFileDialog.Filter = "Image Files | *"; openFileDialog.RestoreDirectory = true; openFileDialog.FilterIndex = 1; if (openFileDialog.ShowDialog() == DialogResult.OK) { var fName = openFileDialog.FileName; Mat imge = new Mat(fName);// Image.FromFile(fName); srcImage = imge; } string imageFileName = "C:\Users\Administrator\Desktop\tu\2.jpg"; //var srcImage = new Mat(imageFileName); //原图 //Cv2.ImShow("Source", srcImage); //Cv2.WaitKey(1); // do events label1.Text = "原图"; pictureBox1.Image = srcImage.ToBitmap(); var grayImage = new Mat(); Cv2.CvtColor(srcImage, grayImage, ColorConversionCodes.BGRA2GRAY); Cv2.EqualizeHist(grayImage, grayImage); var cascade = new CascadeClassifier(Application.StartupPath + "\haarcascade_frontalface_alt.xml"); var nestedCascade = new CascadeClassifier(Application.StartupPath + "\haarcascade_eye_tree_eyeglasses.xml"); var faces = cascade.DetectMultiScale( image: grayImage, scaleFactor: 1.1, minNeighbors: 2, flags: HaarDetectionType.DoRoughSearch | HaarDetectionType.ScaleImage, minSize: new OpenCvSharp.Size(30, 30) ); // Console.WriteLine("Detected faces: {0}", faces.Length); var rnd = new Random(); var count = 1; foreach (var faceRect in faces) { //人脸图 var detectedFaceImage = new Mat(srcImage, faceRect); //Cv2.ImShow(string.Format("Face {0}", count), detectedFaceImage); //Cv2.WaitKey(0); // do events label2.Text = "人脸图"; pictureBox2.Image = detectedFaceImage.ToBitmap(); var color = Scalar.FromRgb(rnd.Next(0, 255), rnd.Next(0, 255), rnd.Next(0, 255)); Cv2.Rectangle(srcImage, faceRect, color, 3); var detectedFaceGrayImage = new Mat(); Cv2.CvtColor(detectedFaceImage, detectedFaceGrayImage, ColorConversionCodes.BGRA2GRAY); var nestedObjects = nestedCascade.DetectMultiScale( image: detectedFaceGrayImage, scaleFactor: 1.1, minNeighbors: 2, flags: HaarDetectionType.DoRoughSearch | HaarDetectionType.ScaleImage, minSize: new OpenCvSharp.Size(30, 30) ); // Console.WriteLine("Nested Objects[{0}]: {1}", count, nestedObjects.Length); foreach (var nestedObject in nestedObjects) { var center = new OpenCvSharp.Point { X = (int)(Math.Round(nestedObject.X + nestedObject.Width * 0.5, MidpointRounding.ToEven) + faceRect.Left), Y = (int)(Math.Round(nestedObject.Y + nestedObject.Height * 0.5, MidpointRounding.ToEven) + faceRect.Top) }; var radius = Math.Round((nestedObject.Width + nestedObject.Height) * 0.25, MidpointRounding.ToEven); Cv2.Circle(srcImage, center, (int)radius, color, thickness: 3); } count++; } //圈中人脸 //Cv2.ImShow("Haar Detection", srcImage); //Cv2.WaitKey(1); // do events //Cv2.WaitKey(0); label3.Text = "圈中人脸图"; pictureBox3.Image = srcImage.ToBitmap(); Cv2.DestroyAllWindows(); srcImage.Dispose(); } //Mat mat = OpenCvSharp.Extensions.BitmapConverter.ToMat(bmp); //bitmap转 mat //Bitmap bitmap = OpenCvSharp.Extensions.BitmapConverter.ToBitmap(mat); // mat 转 bitmap //PictureBox.image=mat.ToBitmap(); //Image img = this.pictureBox1.Image; //Bitmap map = new Bitmap(img); //Image img = Bitmap; }
。