简体   繁体   English

将RGB图像转换为索引图像并保存

[英]Convert RGB image into indexed image and save it

I have an RGB image.我有一个 RGB 图像。 So when I do [image2, map] = imread('image.png') in Matlab I get [] for map as expected.因此,当我在 Matlab 中执行[image2, map] = imread('image.png')时,我得到[] for map 正如预期的那样。 I want to convert that RGB image to an indexed image.我想将该 RGB 图像转换为索引图像。 I want to save it as an indexed image with one channel and see colours like here .我想将其保存为具有一个通道的索引图像并查看此处的颜色。

Referring here I used the following code in Matlab.参考这里,我在 Matlab 中使用了以下代码。

image2 = imread('image.png');
IND = rgb2ind(image2,256);
imwrite(IND, 'imageIndexed.png')

But what is saved is a grey-scale image.但是保存的是灰度图像。 And when I read it back still the map is [] .当我读回来时, map 仍然是[] I want to write it so that it will be a colour image and when I use [image2, map] = imread('image.png') next time, map won't be [] .我想把它写成彩色图像,当我下次使用[image2, map] = imread('image.png')时, map 不会是[] Can someone please help?有人可以帮忙吗?

You can do the following:您可以执行以下操作:

  • Convert image from RGB to indexed image with 2 color:将图像从 RGB 转换为具有 2 种颜色的索引图像:

     [X, cmap] = rgb2ind(RGB, 2);
  • Replace indices of color map to black and white:将颜色 map 的索引替换为黑白:

     cmap(1, 1:3) = [0, 0, 0]; %Fist color is black cmap(2, 1:3) = [1, 1, 1]; %Second color is white
  • Write indexed image (and map) to PNG file:将索引图像(和地图)写入 PNG 文件:

     imwrite(X, cmap, 'K.png');

Pay attention: you need to write the matrix and the color map, when writing and indexed image to file.注意:在写入和索引图像到文件时,您需要写入矩阵和颜色 map。

  • Read image (and map) from PNG file, and convert it to RGB image:从 PNG 文件中读取图像(和地图),并将其转换为 RGB 图像:

     [I, cmap2] = imread('K.png'); L = ind2rgb(I, cmap2);

Here is a code sample:这是一个代码示例:

RGB = imresize(imread('peppers.png'), 0.5);   %Read input RGB image.

%Convert image to indexed image with 2 color.
[X, cmap] = rgb2ind(RGB, 2);
J = ind2rgb(X, cmap);

%Replace indices of color map:
cmap(1, 1:3) = [0, 0, 0]; %Fist color is black
cmap(2, 1:3) = [1, 1, 1]; %Second color is white

K = ind2rgb(X, cmap);

figure;imshow(RGB);
figure;imshow(J);
figure;imshow(K);

imwrite(X, cmap, 'K.png');

[I, cmap2] = imread('K.png');
L = ind2rgb(I, cmap2);
figure;imshow(L);

For completion, here is an example using your reference :为了完成,这是一个使用您的参考的示例:

[webX, web_cmap] = imread('https://i.stack.imgur.com/zuIra.png');   %Read input image and color map.
RGB = ind2rgb(webX, web_cmap);

%Convert image to indexed image with 4 colors.
[X, cmap] = rgb2ind(RGB, 4);

%Collect histogram
H = histogram(X, 4);
H = H.Values';

%Replace indices of color map: set the color with minimal pixels to white, and other to black.
cmap(H == min(H), :) = 1; %Fist color is black
cmap(H ~= min(H), :) = 0; %Set other three colors to black

%Convert to RGB
bwRGB = ind2rgb(X, cmap);

%Convert to indexed image with only 2 colors:
[X, cmap] = rgb2ind(bwRGB, 2);

imwrite(X, cmap, 'K.png');

[I, cmap2] = imread('K.png');
L = ind2rgb(I, cmap2);
figure;imshow(L);

Result:结果:
在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM