如何在emgu中将非圆形ROI的颜色更改为白色

how to change the colour of non-circular ROI to white in emgu

我正在将 python opencv 项目迁移到 c#。

假设我在源图像中发现了一个圆形的roi,我想把它剪下来。同时,非圆形区域裁剪时必须用白色填充

在 Python 我可以做这样的事情:

mask = np.ones(cv2.cvtColor(input_img, cv2.COLOR_BGR2GRAY).shape)
cv2.circle(mask, circle['centre'], circle['radius'], (0, 0, 255), -1)  # -1 will fill the circle
input_img[mask.astype(np.bool), :] = 255  # Set non-circular area as white

但我很难在 c# 中实现它:

Image<Bgr, Byte> input_img = sourceImg  // source image
Image<Bgr, Byte> mark_temp = new Image<Bgr, Byte>(w, h, new Bgr(0, 0, 0));
Image<Gray, Byte> mask = mark_temp.Convert<Gray, Byte>();
CvInvoke.Circle(mask, System.Drawing.Point.Round(circle.Center), (int)circle.Radius, new Bgr(System.Drawing.Color.White).MCvScalar, -1);
dest = input_img.And(input_img, mask);

现在在我的c#代码中,非圆形区域被黑色填充了。因为圆圈内部有一些黑色像素,所以我不想将黑色像素全局转换为白色。有什么办法可以解决吗?

希望这可能有所帮助。

// Here we set the non-disc area to white colour
for (int x = 0; x < img.Width; x++)
{
    for (int y = 0; y < img.Height; y++)
    {
        // offset to center
        int virtX = x - img.Width / 2;
        int virtY = y - img.Height / 2;

        if (Math.Sqrt(virtX * virtX + virtY * virtY) >= discRadius)
        {
            // A faster way to set the pixel to (255, 255, 255)
            img.Data[y, x, 0] = 255;
            img.Data[y, x, 1] = 255;
            img.Data[y, x, 2] = 255;
        }
    }
}