Skip to content

Instantly share code, notes, and snippets.

@LaurentBerger
Created December 4, 2018 07:55
Show Gist options
  • Save LaurentBerger/578078a433bb4587b543bcf56b3459f1 to your computer and use it in GitHub Desktop.
Save LaurentBerger/578078a433bb4587b543bcf56b3459f1 to your computer and use it in GitHub Desktop.
blur with mak
Mat m = imread("g:/lib/opencv/samples/data/lena.jpg", IMREAD_GRAYSCALE);
Mat mask = Mat::zeros(m.size(), CV_8UC1), maskBlur, mc;
// mask is a contour
vector<vector<Point>> c = { {Point(200,100),Point(250,100),Point(350,250),Point(350,300),Point(150,350) } };
drawContours(mask, c, 0, Scalar(255), -1);
Mat negMask;
// neg mask
bitwise_not(mask, negMask);
Mat md, mdBlur, mdint;
m.copyTo(md);
// All pixels outside mask set to 0
md.setTo(0, negMask);
imshow("mask image", md);
// Convert image to int
md.convertTo(mdint, CV_32S);
Size fxy(41, 41);
blur(mdint, mdBlur, fxy);
mask.convertTo(maskBlur, CV_32S);
blur(maskBlur, maskBlur, fxy);
Mat mskB;
mskB.setTo(1, negMask);
Mat m1, m2, maskFloat,mweight;
maskBlur.convertTo(maskFloat, CV_32F,1/255.);
pow(maskFloat,4, maskFloat);
normalize(maskFloat, mweight, 1, NORM_MINMAX);
multiply(mdBlur, 1 - maskFloat, m1, 1, CV_32F);
multiply(m, maskFloat, m2,1, CV_32F);
mc = (m1 + m2);
mc.convertTo(mc, CV_8U);
// resize(mc, mc, Size(), 0.5, 0.5);
imshow("Blur with mask", mc);
imwrite("testBlur.png", mc);
waitKey();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment