c# optimization of code (bitmap)



  • There's a method that captures the image from the web camera, finds the face and draws a square of it. Is there a way to optimize this?

        //при выборе камеры
        private void webCamChose()
        {
            if (webCamTextBox.Text != "" && choseCamera == true) //проверка, что камера выбрана
            {
                device = new VideoCaptureDevice(filter[webCamComboBox.Items.IndexOf(webCamTextBox.Text)].MonikerString); //получаю индекс камеры
                device.NewFrame += Device_NewFrame;
                device.Start();
            }
        }
    
    //обработка веб-камеры
    Image<Bgr, byte> grayImage;
    Rectangle[] rectangles;
    private void Device_NewFrame(object sender, NewFrameEventArgs eventArgs)
    {
        Bitmap bitmap = (Bitmap)eventArgs.Frame.Clone(); //беру изображение с камеры и в bitmap
        grayImage = new Image<Bgr, byte>(bitmap);
        rectangles = cascadeClassifier.DetectMultiScale(grayImage, 1.4, 1, Size.Empty);
        foreach (Rectangle rectangle in rectangles) //для всех найденных лиц
        {
            using (Graphics graphics = Graphics.FromImage(bitmap))
            {
                using (Pen pen = new Pen(Color.Red, 5)) //цвет квадрата и ширина линий
                {
                    graphics.DrawRectangle(pen, rectangle); //на каждом лице рисую квадрат
                }
            }
        }
        cameraPictureBox.Image = bitmap;
    }
    

    I read on the internet that BitMap is a very hot thing. Are there any options that can be replaced?

    To date, the load of the CPs reaches 60-65%. I tried to hang things up and it helped, but it was a very small increase. I did the same at the end of the GC.Collect(s), but the workload has increased. If Thread.Sleep(1) is used, the load has also decreased, but it is very low (if 100 is delayed, then less than 20, but the picture becomes too dull). As an option, we can try to reduce the resolution of the picture, but this reduces the quality of recognition.

    Are there any other alternatives I haven't tried?

    p.s. is a way to reduce the permit, but the quality of the recognition, as I said, is declining very much.

            Size size = new Size(bitmap.Width / 2, bitmap.Height / 2);
    Bitmap newBitmap = new Bitmap(bitmap, size);

    (not bitmap, but newBitmap)



  • Bitmap isn't stunning, it's stupid.
    The problem with the sketch through the Graphics is that this class draws through the GDI+ through the hell knows what challenges in the system, and that's what the pressure on the pros is.
    If you want a quick sketch, draw directly through WinAPI using the SetDIBitsToDevice function.

    If you don't want to faint, there's a RazorGDIPainter library:
    https://archive.codeplex.com/?p=razorgdipainter
    https://habr.com/ru/post/164705/
    https://habr.com/ru/post/164885/
    https://habr.com/ru/post/165403/




Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2