如何在 OpenCV 中实现颜色分割和前景检测?

How can I implement colour segmentation along with foreground detection in OpenCV?

我的问题是,FG 检测输出的是二值图像(使用 MoG 方法)。如何将此输出恢复为 3 通道 (RGB) 图像,以便可以使用像均值偏移这样的颜色分割算法。我的最终目标是通过有效地省略 BG 来检测 FG 中 'largest coloured segment' 的斑点。

谢谢。

假设 img 是您的 3 通道图像,thresh 是您的二值图像。据我了解,您希望 thresh 的白色部分应保留其 RGB 值,而黑色部分应保持黑色。你可以这样做,如图所示:

    Mat mask;
    cvtColor(thresh,thresh,CV_GRAY2BGR);//change thresh to a 3 channel image
    absdiff(img,img,mask);//initialize mask as a black image of img.size()
    subtract(thresh,img,mask);
    subtract(thresh,mask,mask);

现在 mask 将保留过滤后的图像及其 RGB 值。