如何使用 OpenCV 从图像中删除局部平均颜色

问题描述:

我有一张我想要检测的具有柔和渐变背景和清晰前景特征的图像.我希望在图像中找到绿色弧线.然而,绿色弧线相对于背景来说只是绿色的(它们是半透明的).绿色弧线也只有一两个像素宽.

I have an image with a gentle gradient background and sharp foreground features that I want to detect. I wish to find green arcs in the image. However, the green arcs are only green relative to the background (they are semitransparent). The green arcs are also only one or two pixels wide.

因此,对于图像的每个像素,我想减去周围像素的平均颜色.周围的 5x5 或 10x10 像素就足够了.这应该使前景特征相对保持不变,但去除柔和的背景,然后我可以选择绿色通道进行进一步处理.

As a result, for each pixel of the image, I want to subtract the average color of the surrounding pixels. The surrounding 5x5 or 10x10 pixels would be sufficient. This should leave the foreground features relatively untouched but remove the soft background, and I can then select the green channel for further processing.

有什么方法可以有效地做到这一点吗?它不必特别准确,但我想在 1080p 图像上以 30Hz 运行它.

Is there any way to do this efficiently? It doesn't have to be particularly accurate, but I want to run it at 30Hz on a 1080p image.

你可以通过在图像上做一个简单的框模糊,然后从原始图像中减去它来实现:

You can achieve this by doing a simple box blur on the image and then subtracting that from the original image:

blur(img,blurred,Size(15,15));
diff=img-blurred;

我在 1920x1080 的图像上尝试了这个,在一个规格不错的 iMac 上,模糊需要 25 毫秒,减法需要 15 毫秒.

I tried this on a 1920x1080 image and the blur took 25ms and the subtract took 15ms on a decent spec iMac.

如果您的背景没有快速变化,您可以在第二个线程中计算几帧空间的模糊,并继续重复使用它,直到几帧后再次重新计算它,那么您将只有 15 毫秒的减法为每个 30fps 而不是 45ms 的处理做.

If your background is not changing fast, you could calculate the blur over the space of a few frames in a second thread and keep re-using it till you recalculate it again a few frames later then you would only have 15ms subtraction to do for each of your 30fps rather than 45ms of processing.