如何使用MATLAB对图像中的对象进行颜色编码?

问题描述 投票:1回答:1

[我有一个图像:我对其进行了分割以接收二进制图像的Image。我想用不同的颜色标记图像中的每个对象。到目前为止,我有以下代码:

img = imread('lab5a.tif');
BW = imbinarize(img,graythresh(img));
figure; imshowpair(img,BW,'montage')
title ('Opening Operation on Image');
se = strel ('disk', 3);
rem = imclose(BW,se);
figure; imshow (rem, []);
title ('Removed Undesired Features');
CC = bwconncomp(rem);
L = labelmatrix(CC);
RGB = label2rgb(L, spring, 'c', 'shuffle');
figure; imshow(RGB, []);

输出是此图像:Output这不是我想要的。它为背景着色,并且对象为白色。我只希望这些物体具有不同的颜色。

任何形式的帮助将不胜感激!

image matlab label processing
1个回答
1
投票

在您的示例中,图像的背景和前景与您认为的相反。 matlab命令的默认设置是假定较高值的像素(白色)是前景或感兴趣的项目,而较低值的像素(黑色)是背景。因此,当您运行示例代码时,对象CC仅包含1个对象(图像中为蓝色的“背景”):

CC = 

  struct with fields:

    Connectivity: 8
       ImageSize: [256 256]
      NumObjects: 1
    PixelIdxList: {[43341×1 double]}

解决此问题的任何简便方法都只是使用imcomplement命令将清理后的图像反转。将此行添加到您的代码中:

imcomplement

现在% invert the image so that the background is black rem = imcomplement(rem); 结构包含62个已标识的对象:

CC

您将获得此图像:

CC = struct with fields: Connectivity: 8 ImageSize: [256 256] NumObjects: 62 PixelIdxList: {1×62 cell}

如果要更改用于项目的颜色,请查看Colored Items命令的colormap属性。

© www.soinside.com 2019 - 2024. All rights reserved.