使用rgb2ind减少matlab中的颜色数 [英] Reduce number of colors in matlab using rgb2ind

查看:437
本文介绍了使用rgb2ind减少matlab中的颜色数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在做一些图像处理,我需要减少图像的颜色数量。我发现 rgb2ind 可以这样做,并写下面的代码片段:

I am doing some image processing and I needed to reduce the number of colors of an image. I found that rgb2ind could do that and wrote the following snippet:

clc
clear all
[X,map] = rgb2ind(RGB,6,'nodither');
X = rgb2ind(RGB, map);
rgb=ind2rgb(X,map);
rgb_uint8=uint8(rgb*255+0.5);
imshow(rgb_uint8);

但是输出看起来像这样,我怀疑它只有6种颜色。

But the output looks like this and I doubt there are only 6 colors in it.

>

推荐答案

它可能感觉看起来像有超过6种颜色,但真正有6种颜色。如果你看看你的 map 变量,它将是一个6 x 3的矩阵。每一行都包含您要将图片量化的颜色。

It may perceptually look like there is more than 6 colours, but there is truly 6 colours. If you take a look at your map variable, it will be a 6 x 3 matrix. Each row contains a colour that you want to quantize your image to.

要仔细检查,请将此图片转换为灰度图片,然后对此图片进行直方图。如果 rgb2ind 工作,您只能在直方图中看到6个尖峰。

To double check, convert this image into a grayscale image, then do a histogram of this image. If rgb2ind worked, you should only see 6 spikes in the histogram.

BTW,问题,你使用了内置在MATLAB系统路径中的 peppers.png 图像。因此,这是我所描述的我在说什么:

BTW, to be able to reconstruct your problem, you used the peppers.png image that is built-in to MATLAB's system path. As such, this is what I did to describe what I'm talking about:

RGB = imread('peppers.png');

%// Your code
[X,map] = rgb2ind(RGB,6,'nodither');
X = rgb2ind(RGB, map);
rgb=ind2rgb(X,map);
rgb_uint8=uint8(rgb*255+0.5);
imshow(rgb_uint8);

%// My code - Double check colour distribution
figure;
imhist(rgb2gray(rgb_uint8));
axis tight;

这是我得到的数字:

,在我们的直方图中有6个尖峰。如果在运行代码时真正有6种独特的颜色,那么当将图像转换为灰度时,应该相当于6个等效灰度强度,上面的直方图验证了我们的发现。

As you can see, there are 6 spikes in our histogram. If there are truly 6 unique colours when you ran your code, then there should be an equivalent of 6 equivalent grayscale intensities when you convert the image into grayscale, and the histogram above verifies our findings.

因此,您将图像量化为6种颜色,但由于图像的量化噪声,它看起来不像。

As such, you are quantizing your image to 6 colours, but it doesn't look like it due to quantization noise of your image.

这篇关于使用rgb2ind减少matlab中的颜色数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆