Ask Your Question
2

Blurred mask on image

asked 2016-10-23 13:51:09 -0600

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.

edit retag flag offensive close merge delete

2 answers

Sort by ยป oldest newest most voted
1

answered 2016-10-24 22:28:28 -0600

updated 2016-10-24 23:12:15 -0600

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

edit flag offensive delete link more

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 ( 2016-10-25 02:42:41 -0600 )edit

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

Balaji R gravatar imageBalaji R ( 2016-10-25 02:45:23 -0600 )edit

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

StevenPuttemans gravatar imageStevenPuttemans ( 2016-10-25 02:46:38 -0600 )edit
1

answered 2016-10-24 06:28:14 -0600

updated 2016-10-24 06:29:58 -0600

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

edit flag offensive delete link more

Comments

paper is a 21mb download ;(

berak gravatar imageberak ( 2016-10-24 07:12:37 -0600 )edit

high resolution images :D

StevenPuttemans gravatar imageStevenPuttemans ( 2016-10-24 08:03:37 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2016-10-23 13:51:09 -0600

Seen: 6,772 times

Last updated: Oct 24 '16