c++ - OpenCV:创建透明蒙版?

标签 c++ opencv mask mat roi

我目前正在做的是将面部图像加载到垫子中,设置每只眼睛的中心点 X 和 Y 坐标,在每只眼睛周围创建一个圆圈,将 ROI 设置为眼睛周围的圆圈(使用 Rect 并设置 mask ),并降低眼睛图像中的红色值。

我的问题是将校正后的眼睛(红色降低)合并回原始图像,因为校正后的眼睛有黑色 mask 。我不确定如何摆脱黑色面具。

我目前被困在一些 OpenCV 代码上,这让我走到了这一步:

原图:

用黑色面具提取的眼睛:

矫正后的眼睛:

显示我的问题的当前结果:

这是我的主题的延续:Getting ROI from a Circle/Point

我的理解是您无法创建圆形 ROI,因此我选择了矩形 ROI 和黑色 mask 。一个普通的 Rect 是不够的。

非常感谢任何建议!!谢谢。

最佳答案

您可以使用蒙版 roi 图像进行测试像素,但写入您的真实图像:


 cv::Mat plotImage;
    int radius = 15;
    float threshold = 1.8;

    plotImage = cv::imread("C:/temp/debug.jpg", cv::IMREAD_COLOR);

    cv::Point leftEye(person.GetLeftEyePoint().X, person.GetLeftEyePoint().Y);
    cv::Point rightEye(person.GetRightEyePoint().X, person.GetRightEyePoint().Y);

    //get the Rect containing the circle
    cv::Rect r(leftEye.x-radius, leftEye.y-radius, radius*2, radius*2);
    //obtain the image ROI
    cv::Mat roi(plotImage, r);
    //make a black mask, same size
    cv::Mat mask(roi.size(), roi.type(), cv::Scalar::all(0));
    //with a white filled circle in it
    cv::circle(mask, cv::Point(radius, radius), radius, cv::Scalar::all(255), -1);
    //combine roi & mask
    cv::Mat croppedEye = roi&mask;

    //conduct red eye detection/removal on croppedEye
    int count = 0;
    for(int y=0;y<croppedEye.rows;y++)
    {
            for(int x=0;x<croppedEye.cols;x++)
            {
                    double b = croppedEye.at<cv::Vec3b>(y, x)[0];
                    double g = croppedEye.at<cv::Vec3b>(y, x)[1];
                    double r = croppedEye.at<cv::Vec3b>(y, x)[2];

                    double redIntensity = r / ((g + b) / 2);

                    //currently causes issues with non-red-eye images
                    if (redIntensity >= threshold)
                    {
                            double newRedValue = (g + b) / 2;
                            cv::Vec3b pixelColor(newRedValue,g,b);
                            //
                            // here's the trick now, just write back to the original image ;)
                            //
                            roi.at<cv::Vec3b>(cv::Point(x,y)) = pixelColor;
                            count++;
                    }
            }
    }

    cv::imwrite("C:\\temp\\test.jpg", plotImage);

enter image description here

关于c++ - OpenCV:创建透明蒙版?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26335299/

相关文章:

c++ - 有什么方法可以获取 "current function"的返回类型

c++ - 将 std::vector<int> 项传递给可变参数函数

c++ - 浮点运算中的整数转换

python - 如何在opencv python中的图像周围添加边框

css - React maskImage 内联不起作用 - 与 backgroundImage 相同的图像

java - 索贝尔算子 MaskX

tensorflow - 如何获得填充的 bool 掩码

c++ - 清除 unordered_map 时对 max_load_factor 和 bucket 计数的影响

opencv - 上半身骨骼检测

python - Tkinter 窗口中 cv2 图像上的鼠标事件