ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • 일하는뒙? #1 모션인식 테스트
    c# Winform 개발/일하는뒙? 2021. 12. 21. 17:03

     

    필요한 부분 구글링하였음 (opencvsharp4) / DIffImage 함수는 메무리 누수 생기길래 조금 바꿈.

     

    카메라 캡쳐 -> Gray로 변환 -> diff -> Morphology Open(잔상제거) -> NonZero 카운팅 = 감지된 변화 수

     

    아래 사진은 손움직이며 테스트. diff를 picturebox에 찍어봄

     

     

     

     

     

    private void button2_Click(object sender, EventArgs e)
            {
                VideoCapture vc = new VideoCapture(0, VideoCaptureAPIs.DSHOW);
    
                Mat t0 = new Mat();
                Mat t1 = new Mat();
                Mat t2 = new Mat();
                Mat diff = new Mat();
                Mat diff2 = new Mat();
                Mat element = Cv2.GetStructuringElement(MorphShapes.Rect, new OpenCvSharp.Size(10, 10));
                while (true)
                {
                    vc.Read(t0);
                    Cv2.WaitKey(50);
                    vc.Read(t1);
                    Cv2.WaitKey(50);
                    vc.Read(t2);
                    
                    Cv2.CvtColor(t0, t0, ColorConversionCodes.BGR2GRAY);
                    Cv2.CvtColor(t1, t1, ColorConversionCodes.BGR2GRAY);
                    Cv2.CvtColor(t2, t2, ColorConversionCodes.BGR2GRAY);
                    
                    diff = DiffImage(t0, t1, t2);
                    
                    Cv2.MorphologyEx(diff, diff2, MorphTypes.Open, element);
                    
                    int count = Cv2.CountNonZero(diff2);
    
                    if (count > 0)
                    {
                        Console.WriteLine(DateTime.Now.ToString("HH:mm:ss") + "  :  " + count.ToString());
                        t2.SaveImage(Application.StartupPath + "\\detect\\" + DateTime.Now.ToString("HHmmss") + "_" + count.ToString() + ".jpg");
                        //Cv2.PutText(diff, count.ToString(), new OpenCvSharp.Point(1, 25), HersheyFonts.HersheySimplex, 1, Scalar.Blue, 5);
                    }
    
                    
    
                    //pictureBox1.Image = OpenCvSharp.Extensions.BitmapConverter.ToBitmap(diff);
                    //pictureBox2.Image = OpenCvSharp.Extensions.BitmapConverter.ToBitmap(t2);
    
    				diff.Dispose();
                    Cv2.WaitKey(1);
                }
            }
            
            Mat DiffImage(Mat t0, Mat t1, Mat t2)
            {
                Mat diff = new Mat();
    
                using (Mat d1 = new Mat(), d2 = new Mat())
                {
                    Cv2.Absdiff(t2, t1, d1);
                    Cv2.Absdiff(t1, t0, d2);
    
                    Cv2.BitwiseAnd(d1, d2, diff);
                }
                
                return diff;
            }
    300x250

    댓글

Designed by Tistory.