带有透明面膜的OpenCV圆ROI?

问题描述

我正在尝试用修改过的人脸替换图像中检测到的人脸。我没有问题,使用常规的矩形roi。但是,更确切地说,我正在尝试使用圆形ROI。我知道您必须创建一个矩形蒙版才能执行此操作。在这里,我正在使用黑色面具。也许有一种方法可以使面罩透明?我想我已经接近了,但是当我将增强的脸部合并到图像上时,修改过的脸部部分将包含正方形的黑色边框(来自我假设的蒙版)。如何消除黑色方形边框?这可能吗?谢谢!

查看问题:https://pasteboard.co/JscU9re.png

这是相关的代码...让我知道是否需要全部代码

int radius = faces[ic].width / 2;
Mat mask(Size(faces[ic].width,faces[ic].height),CV_8U,Scalar(0)); // all black
Rect region = Rect(faces[ic].x,faces[ic].y,faces[ic].width,faces[ic].height);
Mat circ_roi;

Mat roi(img,region);
Mat insetimage(img,region);


circle(mask,Point(radius,radius),radius,Scalar(255),-1);
bitwise_and(roi,roi,circ_roi,mask); // retain only pixels inside the circle

// create a mat to store the modified mat from the gpu
Mat h_result (circ_roi.size(),circ_roi.type());

// create GPU/device images,same size and type as original host image
cuda::GpuMat d_crop(circ_roi);
cuda::GpuMat d_result;

// create the gaussian filter
cv::Ptr<cuda::Filter> gauss = cv::cuda::createGaussianFilter(d_crop.type(),d_result.type(),Size(ksize,ksize),6.0,6.0);

// apply the gaussian filter to our cropped image
gauss->apply(d_crop,d_result);

// download the result image from device to host
d_result.download(h_result);

// leaves the black border around circle :(
h_result.copyTo(insetimage);

谢谢, 克里斯

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)