从像素标签创建RGB图像 [英] Create a RGB image from pixel labels

查看:111
本文介绍了从像素标签创建RGB图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给定 CV_32SC1 cv :: Mat 图像,其中包含每个像素的标签(其中标签只是 0..N中的索引-1 ),在OpenCV中生成 CV_8UC3 图像的最干净的代码是什么,它显示每个连接的组件具有不同的任意颜色?如果我不必手动指定颜色,如 cv :: floodFill ,则更好。

Given a CV_32SC1 cv::Mat image that contains a label for each pixel (where a label is just an index in 0..N-1), what is the cleanest code in OpenCV to generate a CV_8UC3 image that shows each connected component with a different arbitrary color? If I don't have to specify the colors manually, as with cv::floodFill, the better.

推荐答案

如果最大标签数为256,则可以使用 applyColorMap ,将图像转换为 CV_8U

If the max number of labels is 256, you can use applyColorMap, converting the image to CV_8U:

Mat1i img = ...

// Convert to CV_8U
Mat1b img2;
img.convertTo(img2, CV_8U);

// Apply color map
Mat3b out;
applyColorMap(img2, out, COLORMAP_JET);

如果标签数量高于256,则需要自己完成。下面是一个生成JET色图的示例(它基于 jet 函数的Matlab实现)。然后你可以为矩阵的每个元素应用色彩映射。

If the number of labels is higher than 256, you need to do it yourself. Below there is an example that generates a JET colormap (it's based on Matlab implementation of the jet function). Then you can apply the colormap for each element of your matrix.

请注意,如果你想要一个不同的色图或随机颜色,你只需要修改 //创建JET色彩映射 part:

Please note that if you want a different colormap, or random colors, you just need to modify the //Create JET colormap part:

#include <opencv2/opencv.hpp>
#include <algorithm>
using namespace std;
using namespace cv;

void applyCustomColormap(const Mat1i& src, Mat3b& dst)
{
    // Create JET colormap

    double m;
    minMaxLoc(src, nullptr, &m);
    m++;

    int n = ceil(m / 4);
    Mat1d u(n*3-1, 1, double(1.0));

    for (int i = 1; i <= n; ++i) { 
        u(i-1) = double(i) / n; 
        u((n*3-1) - i) = double(i) / n;
    }

    vector<double> g(n * 3 - 1, 1);
    vector<double> r(n * 3 - 1, 1);
    vector<double> b(n * 3 - 1, 1);
    for (int i = 0; i < g.size(); ++i)
    {
        g[i] = ceil(double(n) / 2) - (int(m)%4 == 1 ? 1 : 0) + i + 1;
        r[i] = g[i] + n;
        b[i] = g[i] - n;
    }

    g.erase(remove_if(g.begin(), g.end(), [m](double v){ return v > m;}), g.end());
    r.erase(remove_if(r.begin(), r.end(), [m](double v){ return v > m; }), r.end());
    b.erase(remove_if(b.begin(), b.end(), [](double v){ return v < 1.0; }), b.end());

    Mat1d cmap(m, 3, double(0.0));
    for (int i = 0; i < r.size(); ++i) { cmap(int(r[i])-1, 2) = u(i); }
    for (int i = 0; i < g.size(); ++i) { cmap(int(g[i])-1, 1) = u(i); }
    for (int i = 0; i < b.size(); ++i) { cmap(int(b[i])-1, 0) = u(u.rows - b.size() + i); }

    Mat3d cmap3 = cmap.reshape(3);

    Mat3b colormap;
    cmap3.convertTo(colormap, CV_8U, 255.0);


    // Apply color mapping
    dst = Mat3b(src.rows, src.cols, Vec3b(0,0,0));
    for (int r = 0; r < src.rows; ++r)
    {
        for (int c = 0; c < src.cols; ++c)
        {
            dst(r, c) = colormap(src(r,c));
        }
    }
}

int main()
{
    Mat1i img(1000,1000);
    randu(img, Scalar(0), Scalar(10));

    Mat3b out;
    applyCustomColormap(img, out);

    imshow("Result", out);
    waitKey();

    return 0;
}

这篇关于从像素标签创建RGB图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆