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.
Asked
Active
Viewed 1,745 times
2

ChronoTrigger
- 8,459
- 1
- 36
- 57
-
You have more than 256 labels? If not, you can just use [applyColorMap](http://docs.opencv.org/2.4/modules/contrib/doc/facerec/colormaps.html). _Arbitrary_ colors means _random_, or something like a colormap? – Miki Mar 14 '16 at 17:32
-
Both random and a colormap are ok. – ChronoTrigger Mar 14 '16 at 17:38
-
Are you sure about `CV_32UC1` (which doesn't exists)? Or you meant `CV_32SC1`? – Miki Mar 14 '16 at 17:56
-
`CV_32SC1`, you are right. – ChronoTrigger Mar 14 '16 at 18:18
1 Answers
3
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);
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.
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;
}

Miki
- 40,887
- 13
- 123
- 202