如何创建反向灰度?

How to create an inverse gray scale?

我有一张黑色背景上有深蓝色斑点的图像我想将其转换为反转灰度。反转,我的意思是,我希望黑底变成白色。

当我将它转换为灰度时,它使所有东西看起来都是黑色的并且很难区分。

  1. 有没有办法在黑色背景采用较浅的阴影的情况下进行反向灰度?
  2. 或者,另一个更可取的选项是将蓝色表示为白色,将黑色表示为黑色。

我目前在 MATLAB 中使用 img = rgb2gray(img);

From mathworks site:

IM2 = imcomplement(IM)
  1. Is there a way to do an inverse gray scale where the black background takes the lighter shades?

根据你的图片描述我制作了一张图片sample.png:

img1 = imread('sample.png');        % Read rgb image from graphics file.
imshow(img1);                       % Display image.

然后,我用了imcomplement function to obtain the complement of the original image (as suggested in ).

img2 = imcomplement(img1);          % Complement image.
imshow(img2);                       % Display image.

这是结果:

  1. Or, another preferable option is to represent the blue as white and the black as black.

在这种情况下,最简单的选择是使用蓝色通道。现在,根据您的需要,您可以使用两种方法:

方法 1:将蓝色通道转换为二值图像 (B&W)

suggests using the logical operation img(:,:,3) > 0,这将 return 蓝色通道的二进制数组,其中每个非零值像素将映射到 1(白色) ,其余像素的值为 0(黑色)。

虽然这种方法简单有效,binary images have the big disadvantage of loosing intensity information。这会改变图像的感知属性。看看代码:

img3 = img1(:, :, 3) > 0;           % Convert blue channel to binary image.
imshow(img3);                       % Display image.

这是结果:

请注意,由于强度信息丢失,原始图像中的圆形斑点在二值图像中变成了八角形。

方法 2:将蓝色通道转换为灰度图像

更好的方法是使用 grayscale image,因为强度信息得以保留。

imshow 函数提供 imshow(I,[low high]) 重载,它调整 color axis scaling of the grayscale image through the DisplayRange 参数。

此重载的一个非常酷的功能是我们可以让 imshow 为我们完成工作。

来自documentation

If you specify an empty matrix ([]), imshow uses [min(I(:)) max(I(:))]. In other words, use the minimum value in I as black, and the maximum value as white.

看看代码:

img4 = img1(:, :, 3);               % Extract blue channel.
imshow(img4, []);                   % Display image.

这是结果:

请注意,圆点的形状与原始图像中的完全一样。