首页 > 解决方案 > 带有透明蒙版的 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);

谢谢你,克里斯

标签: c++opencvroi

解决方案


推荐阅读