Esempio n. 1
0
ImageMatrix<1> Segmentation::connectedComponents(ImageMatrix<1> &input)
{
    size_t row_count = input.rows();
    size_t col_count = input.cols();
    m_labels = std::vector<std::vector<int> >(row_count, std::vector<int>(col_count));

    Channel &in = input.grayscale();

    int component = 0;
    for (size_t i = 0; i < row_count; ++i)
    {
        for (size_t j = 0; j < col_count; ++j)
        {
            if (!m_labels[i][j] && in[i][j])
            {
                dfs(i, j, row_count, col_count, ++component, input);
            }
        }
    }

    int delta = 255 / component;

    ImageMatrix<1> output(row_count, col_count);
    Channel &out = output.grayscale();
    for (size_t i = 0; i < row_count; ++i)
    {
        for (size_t j = 0; j < col_count; ++j)
        {
            int comp = m_labels[i][j];
            out[i][j] = comp * delta;
        }
    }

    return output;
}