【问题标题】:Image segmentation by background color - OpenCV Android按背景颜色分割图像 - OpenCV Android
【发布时间】:2017-03-01 18:12:10
【问题描述】:

我正在尝试对名片进行细分,并按背景颜色对其进行分割,以将它们视为不同的兴趣区域。

例如这种卡片:

应该能够被分成两个图像,因为有 2 种背景颜色。关于如何解决这个问题有什么建议吗?我试过做一些轮廓分析,但结果不太成功。

其他示例卡片:

这张卡片应该给出 3 个分段,因为它有 3 个部分,即使它只有 2 种颜色(虽然 2 种颜色也可以)。

上面的卡片应该只给出一个分割,因为它只是一种背景颜色。

我还没有尝试考虑渐变背景。

【问题讨论】:

标签: android opencv


【解决方案1】:

这取决于其他卡片的外观,但如果图像都具有那么高的质量,那应该不会太难。

在您发布的示例中,您可以只收集边框像素的颜色(最左列、最右列、第一行、最后一行)并将您找到的内容视为可能的背景颜色。也许检查是否有足够的像素具有大致相同的颜色。你需要某种距离测量。一种简单的解决方案是在 RGB 颜色空间中使用欧几里得距离。

一个更通用的解决方案是在整个图像的颜色直方图中找到簇,并将具有超过总像素量 x% 的每种颜色(同样具有容差)视为背景颜色。但是,您对背景的定义取决于您想要实现的目标以及图像的外观。

如果您需要进一步的建议,您可以发布更多图像并标记您希望将图像的哪些部分检测为背景色,哪些部分不。

-

编辑:您的两张新图像也显示相同的模式。背景颜色占据了图像的很大一部分,没有噪点,也没有颜色渐变。所以一个简单的方法可能如下所示:

如果您有不适用于此方法的示例,请发布它们。

【讨论】:

  • 我又添加了一些示例卡片。如果可能的话,您能否将您的答案更多地与 OpenCV 联系起来?
  • 谢谢@Dobi,我会试试这个并告诉你:)
  • 您对使用哪种阈值有什么建议吗?我正在思考如何隔离图像直方图上的每个峰值,以便获得与该峰值相关的轮廓
  • 如果您的图像都像您的示例一样清晰,那么您的直方图峰值将非常窄且高。只是尝试一下。我的第一个猜测是:在直方图中每个维度使用 16 个 bin。使用值高于 0.3*number_of_pixels_in_image 的每个 bin(您的 16^3)作为 bg 颜色,并使用容差为 16 个颜色值差异的阈值进行分割。
  • 第一张图片的背景好像有轻微的渐变,并给出了几个 bin 的峰值......对此有什么建议吗?
【解决方案2】:

作为一种同时查找具有颜色渐变的背景的方法,可以使用 canny。以下代码(是的,不是 android,我知道,但如果移植它,结果应该是相同的)适用于您迄今为止发布的三个示例图像。如果您有其他不适用于此的图像,请告诉我。

#include <opencv2/opencv.hpp>

using namespace cv;
using namespace std;

Mat src;
Mat src_gray;
int canny_thresh = 100;
int max_canny_thresh = 255;
int size_per_mill = 120;
int max_size_per_mill = 1000;
RNG rng(12345);

bool cmp_contour_area_less(const vector<Point>& lhs, const vector<Point>& rhs)
{
    return contourArea(lhs) < contourArea(rhs);
}

void Segment()
{
    Mat canny_output;
    vector<vector<Point> > contours;
    vector<Vec4i> hierarchy;

    Canny(src_gray, canny_output, canny_thresh, canny_thresh*2, 3);

    // Draw rectangle around canny image to also get regions touching the edges.
    rectangle(canny_output, Point(1, 1), Point(src.cols-2, src.rows-2), Scalar(255));
    namedWindow("Canny", CV_WINDOW_AUTOSIZE);
    imshow("Canny", canny_output);

    // Find the contours.
    findContours(canny_output, contours, hierarchy, CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE, Point(0, 0));

    // Remove largest Contour, because it represents always the whole image.
    sort(contours.begin(), contours.end(), cmp_contour_area_less);
    contours.resize(contours.size()-1);
    reverse(contours.begin(), contours.end());

    // Maximum contour size.
    int image_pixels(src.cols * src.rows);
    cout << "image_pixels: " << image_pixels << "\n";

    // Filter the contours, leaving just large enough ones.
    vector<vector<Point> > background_contours;
    for(size_t i(0); i < contours.size(); ++i)
    {
        double area(contourArea(contours[i]));
        double min_size((size_per_mill / 1000.0) * image_pixels);
        if (area >= min_size)
        {
            cout << "Background contour " << i << ") area: " << area << "\n";
            background_contours.push_back(contours[i]);
        }
    }

    // Draw large contours.
    Mat drawing = Mat::zeros(canny_output.size(), CV_8UC3);
    for(size_t i(0); i < background_contours.size(); ++i)
    {
        Scalar color = Scalar(rng.uniform(0, 255), rng.uniform(0,255), rng.uniform(0,255));
        drawContours(drawing, background_contours, i, color, 1, 8, hierarchy, 0, Point());
    }

    namedWindow("Contours", CV_WINDOW_AUTOSIZE);
    imshow("Contours", drawing);
}

void size_callback(int, void*)
{
    Segment();
}

void thresh_callback(int, void*)
{
    Segment();   
}

int main(int argc, char* argv[])
{
    if (argc != 2)
    {
        cout << "Please provide an image file.\n";
        return -1;
    }

    src = imread(argv[1]);

    cvtColor(src, src_gray, CV_BGR2GRAY);
    blur(src_gray, src_gray, Size(3,3));

    namedWindow("Source", CV_WINDOW_AUTOSIZE);
    imshow("Source", src);

    if (!src.data)
    {
        cout << "Unable to load " << argv[1] << ".\n";
        return -2;
    }

    createTrackbar("Canny thresh:", "Source", &canny_thresh, max_canny_thresh, thresh_callback);
    createTrackbar("Size thresh:", "Source", &size_per_mill, max_size_per_mill, thresh_callback);

    Segment();
    waitKey(0);
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-09-04
    • 2016-01-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-03
    相关资源
    最近更新 更多