图像分类 - 使用颜色图为特征着色?

Image classification - coloring features with a colormap?

我有一张图片,里面有一些 features/regions(上例中的 balls)。我想根据每个球的特性为每个球涂上不同的颜色。例如,这可能是以像素为单位的直径。

虽然我已经完成了特征识别方面的工作,但在显示结果方面却遇到了困难。现在我在做:

my_image = imread(...);

//ball recognition and other stuff

for i = 1:number_of_balls
   ball_diameter(i) = ... //I calculate the diameter of the i-th ball
   ball_indices = ... //I get the linear indices of the i-th ball

   //ball coloring
   my_image(ball_indices) = 255; //color the red channel
   my_image(ball_indices + R*C) = 0; //color the blue channel
   my_image(ball_indices + 2*R*C) = 0; //color the green channel
end

figure
imshow(my_image)
colormap jet(100) //I want 100 different classes
colorbar
caxis([0, 50]) //I assume all balls have a diameter < 50

在上面的代码中,我将所有的球都染成了红色,这绝对不是我想要的。问题是,即使我知道 ball_diameter(i),我也不知道球会进入哪个 colormap class。换句话说,我需要这样的东西:

for i = 1:number_of_balls
   // ...

   if ball_diameter *belongs to class k*
      my_image(ball_indices) = jet(k, 1);
      my_image(ball_indices + R*C) = jet(k,2);
      my_image(ball_indices + 2*R*C) = jet(k,3);
   end
end

如何,主要是,还有其他更合乎逻辑的方法吗?

简单的方法如下:

  1. 制作与原始尺寸相同的二维图像。

  2. 将每个 "ball" 的索引设置为直径或其他相关值

  3. 显示为imagesc

  4. 使用caxiscolormapcolorbar等动态调整类别

例如,

a = randi(200,200); % 200 x 200 image containing values 1 to 200 at random
imagesc(a)
colorbar

上面应该显示一个带有默认颜色图的随机颜色字段。颜色条从 1 到 200。

colormap(jet(5))

颜色条仍然从 1 到 200,但只有 5 种颜色。

caxis([1 100])

颜色栏现在显示从 1 到 100 的五种颜色(所有超过 100 的颜色都在最上面)。

如果您想将充满不同直径的 2D 图像转换为一组指示直径范围的离散标签,一个简单的方法是使用 histc,第二个输出 bin 相同size 作为你的输入图像,设置直径值落入哪个 bin。在这种情况下,第二个输入是 bin 的 edges,而不是中心。

[n bin] = histc(a,0:20:201);

您可以将 类 的像素分配与其着色分开显示:您可以使用 my_image 作为 2D R-by-C 标签矩阵:也就是说,每个对象(球)都被分配了一个从 1 到 100 的不同 index(如果图像中有 100 个对象)。现在,当您想要 显示 结果时,您可以使用 colormap 要求图形为您将索引映射到颜色,或者使用 ind2rgb 显式创建彩色图像.

例如

%// create the index/labeling matrix
my_image = zeros( R, C );
for ii = 1:number_of_balls
    my_image( ball_indices ) = ii; %// assign index to pixels and not atual colors
end

%// color using figure
figure;
imagesc( my_image );axis image; 
colormap rand(100,3); %// map indexes to colors using random mapping

%//explicitly create a color image using ind2rgb
my_color_image = ind2rgb( my_image, rand(100,3) );
figure;
imshow( my_color_image ); % display the color image

备注:
1. 恕我直言,最好使用随机颜色图来显示像素的分类,而不是 jet,后者通常最终会得到与相邻对象非常相似的颜色,从而很难在视觉上欣赏结果。
2. 恕我直言,使用标签矩阵更方便,您还可以将其作为索引图像(png 格式)保存到文件中,从而简单有效地可视化、保存和加载您的结果。