1 | initial version |
another way (but in c++) with an unknown mask :
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 disk
circle(mask, Point(200, 200), 100, Scalar(255), -1);
Mat negMask;
// neg mask
bitwise_not(mask, negMask);
circle(mask, Point(200, 200), 100, Scalar(255), -1);
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);
imshow("Blur with mask", mc);
waitKey();
2 | No.2 Revision |
another way (but in c++) with an unknown mask :
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 disk
circle(mask, Point(200, 200), 100, 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);
circle(mask, Point(200, 200), 100, Scalar(255), -1);
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);
imshow("Blur with mask", mc);
waitKey();