How to blur a part of an image ?

Ask question on using Emgu CV with C#
Post Reply
cconte
Posts: 4
Joined: Tue Feb 03, 2015 7:25 am

How to blur a part of an image ?

Post by cconte » Tue Feb 03, 2015 7:42 am

Hi folk,

I'm trying to blur the face from pictures or webcam.
So far, by following the tuto I have been able to detect faces but i still struggling to blur the detected faces.
I'm wondering if someone could advice me about how to blur a specific part of an image ?
below, you will find the code used to detected the faces.

Code: Select all

  private void ProcessFrame(object sender, EventArgs arg)
        {
            //fetch the frame captured by web camera
            Image<Bgr, Byte> ImageFrame = capture.QueryFrame();

            //Detect Faces from the ImageFrame
            if (ImageFrame != null)   // confirm that image is valid
            {
                //convert the image to gray scale
                Image<Gray, byte> grayframe = ImageFrame.Convert<Gray, byte>();

                //detect faces from the gray-scale image and store into an array of type 'var',i.e 'MCvAvgComp[]'
                var faces = grayframe.DetectHaarCascade(haar, 1.20, 2,
                                        HAAR_DETECTION_TYPE.DO_CANNY_PRUNING,
                                        new Size(25, 25))[0];

                //draw a green rectangle on each detected face in image
                foreach (var face in faces)
                {
                    ImageFrame.Draw(face.rect, new Bgr(Color.Green), 3);     
                   //how to blur the specific picture zone ?
                }
            }
            //show the image in the EmguCV ImageBox
            CamImageBox.Image = ImageFrame;
        }
thx in advance for your help,

Cedric

Chris_Johnson
Posts: 573
Joined: Tue Sep 20, 2011 3:29 pm

Re: How to blur a part of an image ?

Post by Chris_Johnson » Tue Feb 03, 2015 8:53 am

Hi Cedric,

I would suggest something like the following, it might not work straight of the bat as I haven't had chance to fully test it but it should give you the right idea.

Code: Select all

 foreach (var face in faces)
      {
                    Image<Bgr, Byte> temp = ImageFrame.Copy(face.rect); //copy the image data from the face
                    temp = temp.PyrDown().PyrDown().PyrDown().PyrUp().PyrUp().PyrUp(); //very simple blurring effect
                    ImageFrame.ROI = face.rect; //set the ROI to the same size as temp
                    ///if the program hangs here check to make sure the 
                    ///ImageFrame.ROI is the same size of temp.
                    CvInvoke.cvCopy(temp, ImageFrame, new IntPtr(0)); //copy the temp to the frame 
                    ImageFrame.ROI = Rectangle.Empty; //set ROI to NULL rectangle to reset
                    
     }
     CamImageBox.Image = ImageFrame;
Cheers,
Chris

cconte
Posts: 4
Joined: Tue Feb 03, 2015 7:25 am

Re: How to blur a part of an image ?

Post by cconte » Tue Feb 03, 2015 10:10 am

Thx a lot Chris,

Your are right, if i do not the control on the size of temp image (temp image must have the same size of ImageFrame.ROI) I got the following error :
{"OpenCV: src.depth() == dst.depth() && src.size == dst.size"}
I dont know that it's a good practice but i resize the temp image to fit the ImageFrame.ROI
The demo app seems to works well but i'm wondering if it is the right way to do it ?

Below the code sample.

Code: Select all

 foreach (var face in faces)
                {
                    try
                    {
                        Image<Bgr, Byte> temp = ImageFrame.Copy(face.rect); //copy the image data from the face
                        temp = temp.PyrDown().PyrDown().PyrDown().PyrUp().PyrUp().PyrUp(); //very simple blurring effect
                        ImageFrame.ROI = face.rect; //set the ROI to the same size as temp
                        ///if the program hangs here check to make sure the 
                        ///ImageFrame.ROI is the same size of temp.
                        if (ImageFrame.ROI.Size.Equals(temp.Size))
                        {
                            CvInvoke.cvCopy(temp, ImageFrame, new IntPtr(0)); //copy the temp to the frame 
                        }
                        else
                        {
                            temp = temp.Resize(ImageFrame.ROI.Size.Width, ImageFrame.ROI.Size.Height, INTER.CV_INTER_AREA);
                            CvInvoke.cvCopy(temp, ImageFrame, new IntPtr(0)); //copy the temp to the frame 
                        }
                        ImageFrame.ROI = Rectangle.Empty; //set ROI to NULL rectangle to reset
                        ImageFrame.Draw(face.rect, new Bgr(Color.Green), 2);
                    }
                    catch (Exception ex)
                    {
                        // {"OpenCV: src.depth() == dst.depth() && src.size == dst.size"}
                        throw ex;
                    }
Best regards,

Cedric

Chris_Johnson
Posts: 573
Joined: Tue Sep 20, 2011 3:29 pm

Re: How to blur a part of an image ?

Post by Chris_Johnson » Tue Feb 03, 2015 11:58 am

Hi,

Its a completely valid method the alternative is to loop through the face rect of the image and apply a large gaussian filter (very slow) or add random values to the pixels (quickest).

Try something like this as an alternative to resizing temp to match the ROI.

Code: Select all

ImageFrame.ROI = new Rectangle(face.rect.X, face.rect.Y, temp.width, temp.height); //set the ROI to the same size as temp
Cheers,
Chris

cconte
Posts: 4
Joined: Tue Feb 03, 2015 7:25 am

Re: How to blur a part of an image ?

Post by cconte » Wed Feb 04, 2015 2:44 am

Thanks Chris. I really appreciate your help and advice.

Post Reply