这可能是一种黑客行为,但由于这是一个“假日项目”,所以我无论如何都会发布它:)
您是否尝试过隔离背景,然后反转蒙版(这会假设任何非背景的东西都是对象,但它可能适合您想要的效果)。
Below is the result I got using the OpenCV inRange function:
您可能想要平滑图像(预处理)高斯模糊消除一些锯齿。我使用了比侵蚀内核更大的膨胀内核(5x5 与 3x3)来消除一些噪声像素。平滑可能会有所帮助,调整阈值也可能使侵蚀变得不必要。但是,这应该可以帮助您开始。
最后,这是我用来查找此范围的快速小代码片段:
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <vector>
using namespace cv;
using namespace std;
int main(int argc, char** argv)
{
Mat src = imread("test.jpg");
int rh = 255, rl = 100, gh = 255, gl = 0, bh = 70, bl = 0;
string windowName = "background";
namedWindow(windowName);
createTrackbar("rh", windowName, &rh, 255);
createTrackbar("rl", windowName, &rl, 255);
createTrackbar("gh", windowName, &gh, 255);
createTrackbar("gl", windowName, &gl, 255);
createTrackbar("bh", windowName, &bh, 255);
createTrackbar("bl", windowName, &bl, 255);
// for dilation
Mat element = getStructuringElement(MORPH_RECT, Size(5, 5));
Mat bgIsolation;
int key = 0;
do
{
inRange(src, Scalar(bl, gl, rl), Scalar(bh, gh, rh), bgIsolation);
bitwise_not(bgIsolation, bgIsolation);
erode(bgIsolation, bgIsolation, Mat());
dilate(bgIsolation, bgIsolation, element);
imshow(windowName, bgIsolation);
key = waitKey(33);
} while((char)key != 27);
waitKey();
return 0;
}
享受假期项目!挺有趣的 :)