c++ - 带有透明蒙版的 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);
谢谢你,克里斯
解决方案
推荐阅读
- python - 如果一个指标需要参数规范,如何为多个机器学习指标编写一个 for 循环?
- node.js - 使用 ReactJS 的 chrome 浏览器的认证错误
- javascript - 在终端中输入 npm start 后,我不断收到此错误
- r - 将边缘着色为与 igraph 中的节点相同的颜色
- apache-camel - 找不到端点 - Camel Http 集成
- python - 用python压缩文件
- reactjs - React Native 中的 CSV 文件存在问题。未定义无法解析模块`./assets/race.csv`
- javascript - 时区名称近似于纬度/经度?
- jenkins - Sonarqube 服务器无法访问
- google-kubernetes-engine - 云构建部署到 gke 集群