private void Count_timer_Tick(object sender, EventArgs e) { Countbox.Visible = true; Countbox.Image = timePhotos[c_flag]; c_flag++; if (c_flag > 3) { face_image = camera.QueryFrame(); faces = gray.DetectHaarCascade(haar, 1.2, 10, HAAR_DETECTION_TYPE.DO_CANNY_PRUNING, new Size(25, 25))[0]; Count_timer.Stop(); Camera_timer.Start(); } }
//啟動動畫timer private void Animation_timer_Tick(object sender, EventArgs e) { //定義距離每格數的(X,Y) int new_x = pictureBoxCenter.Location.X - vx; int new_y = pictureBoxCenter.Location.Y - vy; pictureBoxCenter.Location = new Point(new_x, new_y); //定義每次移動一格,box大小的變化量 int new_w = pictureBoxCenter.Width - v_size; int new_h = pictureBoxCenter.Height - v_size; pictureBoxCenter.Size = new Size(new_w, new_h); ani_count++;//每移動一格,計數器+1(移動一格timer執行一次) //設定影像紅框閃爍,以2的餘數做判斷 if ((ani_count % 2) == 1) { Redbox.Visible = true; } else { Redbox.Visible = false; } //若做完動畫後,pictureBoxCenter回復原來大小與位置,等待下一次執行 if (ani_count > (fps - 1)) { ani_count = 0; pictureBoxCenter.Size = new Size(b_size * scale, b_size * scale); //計算pictureBoxCenter大小 pictureBoxCenter.Location = new Point(rx - (b_size * scale), ry - (b_size * scale)); //計算pictureBoxCenter位置 pictureBoxCenter.Visible = false; Animation_timer.Stop(); //動畫結束 Camera_timer.Start(); //重新啟動偵測人臉 } }
private void face_detect() { gray = face_image.Convert <Gray, byte>();//將camera輸出影像轉為灰階(EmguCV 語法) //人臉抓取語法 faces = gray.DetectHaarCascade(haar, 1.2, 10, HAAR_DETECTION_TYPE.DO_CANNY_PRUNING, new Size(25, 25))[0]; Image Input = face_image.ToBitmap();//轉檔camera輸出影像為Bitmap //判斷人臉抓取是否為空 if (faces.Length > 0) { if (c_flag == 0) { Camera_timer.Stop(); //抓取到人臉後camera_timer停止 Count_timer.Start(); //計數圖片顯示 } else { //Array.Clear(pic, 0, pic.Length); Countbox.Image = timePhotos[c_flag]; c_flag = 0; //Array.Clear(pic, 0, pic.Length); Camera_timer.Stop(); //抓取到人臉後camera_timer停止 MCvAvgComp face = faces[0]; //宣告取得的人臉為face //face_image.Draw(face.rect, new Bgr(Color.Red), 3);//將camera輸出影像對應抓取到的人臉,標記紅框 //將取得的人臉分割出來並另存為Bitmap ExtractedFace = new Bitmap(face.rect.Width, face.rect.Height); FaceCanvas = Graphics.FromImage(ExtractedFace); FaceCanvas.DrawImage(Input, 0, 0, face.rect, GraphicsUnit.Pixel); ExtractedFace.RotateFlip(RotateFlipType.Rotate270FlipNone); //旋轉影像 pictureBoxCenter.Visible = true; //顯示大圖影像 pictureBoxCenter.Image = ExtractedFace; //將分割的人臉載入當前的picturebox中 pictureBoxCenter.SizeMode = PictureBoxSizeMode.Zoom; Invalidate();//清空box,重新繪製圖片 //半透明處理 matrix = new ColorMatrix(nArray); attributes.SetColorMatrix(matrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap); Image srcImage = (Image)ExtractedFace; Bitmap resultImage = new Bitmap(srcImage.Width, srcImage.Height); Graphics g = Graphics.FromImage(resultImage); g.DrawImage(srcImage, new Rectangle(0, 0, srcImage.Width, srcImage.Height), 0, 0, srcImage.Width, srcImage.Height, GraphicsUnit.Pixel, attributes); ExtFaces = resultImage; in_taiwan(); //分割動畫距離格數 vx = (pictureBoxCenter.Location.X - pic[faceNo].Location.X) / fps; vy = (pictureBoxCenter.Location.Y - pic[faceNo].Location.Y) / fps; picout(); faceNo = (faceNo + 1) % (cnt_x * cnt_y); //faceNO++,限制圖片載量(台灣外部) faces = null; //抓取一次人臉,清空一次,釋放記憶體 Countbox.Visible = false; } } //攝影機影像顯示 pictureBoxCamera.Image = face_image.ToBitmap(); pictureBoxCamera.SizeMode = PictureBoxSizeMode.Zoom; }