图像分类 - 使用色彩图着色功能?

时间:2015-03-24 15:43:42

标签: matlab image-processing

enter image description here

我的图像中包含一些功能/区域(上例中的)。我想根据其属性为每个球着色不同的颜色。例如,可能是它的直径(以像素为单位)。

虽然我已经完成了功能识别方面的工作,但在显示结果时我却陷入了困境。现在我正在做:

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类。换句话说,我需要类似的东西:

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

如何,而且大多数情况下,还有其他更合乎逻辑的方法吗?

2 个答案:

答案 0 :(得分:0)

一种简单的方法如下:

  1. 制作与原始尺寸相同的2D图像。

  2. 设置每个&#34;球的指数&#34;直径或其他相关值

  3. 显示imagesc

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

  5. 例如,

    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的大小与输入图像,设置直径值落入哪个bin。在这种情况下,第二个输入是箱的,而不是中心。

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

答案 1 :(得分:0)

您可以将像素的分配与其着色的类别分开显示:您可以使用my_image作为2D R - by - C标记矩阵:即每个对象( ball)从1到100分配了不同的索引(如果图像中有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格式),从而可以简单有效地显示,保存和加载结果。

相关问题