Ask Your Question

Blender with transparent background

asked 2017-03-30 05:36:38 -0600

pcardoso gravatar image

Is there a way to use a blender (e.g.: the FeatherBlender) but generating an output image with a transparent background?

I am trying to use (or abuse) the FeatherBlender to copy an image with a mask, but I want to smooth the edges a bit, like the feather tool in Photoshop.

The mask was generated with the grabcut algorithm. I want to smooth the edges a bit, since they are a bit jagged.


edit retag flag offensive close merge delete


maybe this will be helpful

sturkmen gravatar imagesturkmen ( 2017-03-30 05:47:58 -0600 )edit

1 answer

Sort by ยป oldest newest most voted

answered 2017-03-30 10:36:10 -0600

LBerger gravatar image

I'm not sure but I think you can try something like this :

String nomFic = "c:/lib/opencv/samples/data/baboon.jpg";
Mat img1 = imread(nomFic, IMREAD_COLOR);
nomFic = "c:/lib/opencv/samples/data/lena.jpg";
Mat img2 = imread(nomFic, IMREAD_COLOR);
img1.convertTo(img1, CV_16SC3);
img2.convertTo(img2, CV_16SC3);
vector<Mat> x = { img1,img2 };
vector<UMat> ponderation;
vector<UMat> masque;
vector<Point> tl;
vector<Size> taille;

tl.push_back(Point(0, 0));
tl.push_back(Point(0, 0));
masque.push_back(UMat(img1.size(), CV_8UC1, Scalar(255)));
masque.push_back(UMat(img2.size(), CV_8UC1, Scalar(0)));
    Mat xtmp = masque[1].getMat(ACCESS_RW);
    circle(xtmp, Point(256, 256), 100,Scalar(255),-1);
Ptr<cv::detail::Blender> mixeurFeather2 = cv::detail::Blender::createDefault(cv::detail::Blender::FEATHER);
mixeurFeather2->prepare(tl, taille);
for (int i = 0; i < x.size(); i++)
    mixeurFeather2->feed(x[i], masque[i], tl[i]);
mixeurFeather2.dynamicCast<cv::detail::FeatherBlender>()->createWeightMaps(masque, tl, ponderation);
    Mat xtmp = ponderation[0].getMat(ACCESS_RW);
    Rect r = Rect(220, 220, 72, 72);

Then it is not finished but you have in ponderation[0] and ponderation[1] weighted maps for each image. You can modify and do your own blender algorithm... Unfortunately you cannot set new weighted map in FeatherBlender.

edit flag offensive delete link more


Thanks , but I ended up not using FeatherBlender at all.

I used split and merge to set my mask (after a little dilation and blurring) as the alpha channel of the source image. Seems to work well enough.

    cv::Mat kernel = cv::getStructuringElement(cv::MORPH_RECT, cv::Size(5, 5));
    cv::Mat dilatedMask;
    cv::dilate(mask, dilatedMask, kernel);

    GaussianBlur(dilatedMask, mask, cv::Size(11, 11), 0, 0, BORDER_REFLECT);

    vector<Mat> imageChannels(4);
    cv::split(foreground, imageChannels);
    imageChannels[3] = mask;

    cv::Mat res;
    cv::merge(imageChannels, res);
pcardoso gravatar imagepcardoso ( 2017-03-30 10:56:02 -0600 )edit

Question Tools

1 follower


Asked: 2017-03-30 05:36:38 -0600

Seen: 475 times

Last updated: Mar 30 '17