First time here? Check out the FAQ!

Ask Your Question
2

Blurred mask on image

asked Oct 23 '16

Nirmalan gravatar image

Hello,

i want to blur specific regions (ellipses) of an image with smooth edges. What i have done so far is:

  1. create mask for non blurring area (ellipse)
  2. create inverted mask for blurred area
  3. copy the original image and using bitwise_and with the mask get the specific areas
  4. blurred the image and using bitwise_and with inverted mask
  5. combined the to images

The edges are "hard" and i wanted to use a blur on the mask to feather the edges. But when i combine the blurred mask the edges become weired.

Is there a way to achieve this?

If code is needed ill provide.

Preview: (hide)

2 answers

Sort by » oldest newest most voted
1

answered Oct 25 '16

updated Oct 25 '16

Hi @Nirmalan! Here is my Simple algorithm to smooth with Mask!

bool MaskedSmooth(const Mat mSrc,const Mat mMask,Mat &mDst, double Sigma, double MaskRadius) 
{
    if(mSrc.empty())
    {
        return 0;
    }
    if(MaskRadius<1)
        MaskRadius=1.0;
    if(Sigma<1)
        Sigma=1.0;

    Mat mGSmooth;
    mDst = Mat(mSrc.size(),mSrc.type());

    GaussianBlur(mSrc,mGSmooth,Size(0,0),Sigma); //You can add any Smoothing algorithm here e.g Median, Billateral, etc...
    GaussianBlur(mMask,mMask,Size(0,0),MaskRadius);         

    for (size_t Rows = 0; Rows < mSrc.rows; Rows++)
    {
        for (size_t Cols = 0; Cols < mSrc.cols; Cols++)
        {
            Vec3b pSrc = mSrc.at<Vec3b>(Rows, Cols);
            Vec3b pSSrc = mGSmooth.at<Vec3b>(Rows, Cols);
            Vec3b &pDest = mDst.at<Vec3b>(Rows, Cols);
            uchar Mask = mMask.at<uchar>(Rows, Cols);

            float Percent = Mask / 255.0;
            pDest = (1 - Percent)* pSrc + (pSSrc * Percent);            
        }
    }

    return true;
}

bool MaskedSmoothOptimised(Mat mSrc,Mat mMask,Mat &mDst, double Sigma, double MaskRadius) 
{
    if(mSrc.empty())
    {
        return 0;
    }
    if(MaskRadius<1)
        MaskRadius=1.0;
    if(Sigma<1)
        Sigma=1.0;

    Mat mGSmooth;   
    cvtColor(mMask,mMask,COLOR_GRAY2BGR);

    mDst = Mat(mSrc.size(),CV_32FC3);   
    mMask.convertTo(mMask,CV_32FC3,1.0/255.0);
    mSrc.convertTo(mSrc,CV_32FC3,1.0/255.0);

    GaussianBlur(mSrc,mGSmooth,Size(0,0),Sigma); 
    GaussianBlur(mMask,mMask,Size(0,0),MaskRadius);     

    Mat M1,M2,M3;

    subtract(Scalar::all(1.0),mMask,M1);
    multiply(M1,mSrc,M2);
    multiply(mMask,mGSmooth,M3);        
    add(M2,M3,mDst);

    return true;
}

int _tmain(int argc, _TCHAR* argv[])
{
    string sSFileNameEx = "Test.png";
    string sMFileNameEx = "Test_mask.bmp";

    Mat mSrc, mSrc_Mask,mResult;

    mSrc = imread(sSFileNameEx, 1);
    mSrc_Mask = imread(sMFileNameEx, 0);
    if (mSrc_Mask.empty() || mSrc.empty())
    {
        cout << "[Error]! Invalid Input Image";
        return 0;
    }

    imshow("mSrc", mSrc);
    imshow("mSrc_Gray", mSrc_Mask); 

    MaskedSmooth(mSrc,mSrc_Mask,mResult,5.0,25.0);

    imshow("Masked Smooth Results", mResult);
    waitKey(0);
    return 0;
}

Source Image:

image description

Mask:

image description

Source Smoothed

image description

Mask Smoothed

image description

Output Masked Smoothed:

image description

Preview: (hide)

Comments

Nice but this only works if a mask is easy definable. You would be amazed how many applications do not offer that.

StevenPuttemans gravatar imageStevenPuttemans (Oct 25 '16)edit

@StevenPuttemans Yes agreed! But isn't that the OP question?

Balaji R gravatar imageBalaji R (Oct 25 '16)edit

Ah well, after re-reading it, that might indeed be the case and I might mis-interpret the question...

StevenPuttemans gravatar imageStevenPuttemans (Oct 25 '16)edit
1

answered Oct 24 '16

updated Oct 24 '16

In one of my publications on pedestrian detection in cycloramic images, together with a co-author/collegue, I propose a technique of softblurring. The idea is basically to relate the power of the blurring to the position related to the center of the object/region your are blurring.

This was programmed in OpenCV, so basically it should be doable.

To make a longer story short:

image description

Preview: (hide)

Comments

paper is a 21mb download ;(

berak gravatar imageberak (Oct 24 '16)edit

high resolution images :D

StevenPuttemans gravatar imageStevenPuttemans (Oct 24 '16)edit

Question Tools

1 follower

Stats

Asked: Oct 23 '16

Seen: 7,120 times

Last updated: Oct 24 '16