# How are the indices computed in gray2ind(...)?

조회 수: 2(최근 30일)
Harsha Yogeshappa 2021년 10월 16일
편집: Harsha Yogeshappa 2021년 10월 18일
In the below code, I would like to know the mathematics that was involved behind generating an index for a pixel. Does it consider the distribution of intensity values of all the pixels in the image? Or, does it just consider pixel by pixel independently, and generate an index?
size(camera)
ans = 1×2
256 256
class(camera)
ans = 'uint8'
[x, map] = gray2ind(camera, 32);
class(x)
ans = 'uint8'
camera(110:115, 110:115)
ans = 6×6
11 11 11 11 12 12 12 12 13 13 13 14 13 12 12 12 12 13 13 12 12 12 13 15 12 11 12 13 16 15 12 12 13 14 16 15
x(110:115, 110:115)
ans = 6×6
1 1 1 1 1 1 1 1 2 2 2 2 2 1 1 1 1 2 2 1 1 1 2 2 1 1 1 2 2 2 1 1 2 2 2 2

댓글을 달려면 로그인하십시오.

### 채택된 답변

DGM 2021년 10월 16일
Here's an example for a uint8 image:
maplength = 32;
[B bmap] = gray2ind(A,maplength);
% assuming uint8 input
C = uint8((maplength-1)/255 * double(A));
cmap = gray(maplength);
immse(B,C) % images are the same
ans = 0
immse(bmap,cmap) % maps are the same
ans = 0
You can also just look at how it's done by opening gray2ind.m.
So it appears that it pays no attention to the actual image content either locally or globally. It merely does a crude uniform quantization based on the standard data range defined by the numeric class.
This is in contrast to rgb2ind() which is both more complicated in its available palette reduction methods, but also wrapped up in mex, so figuring out how it works isn't as easy.
##### 댓글 수: 3표시숨기기 이전 댓글 수: 2
Harsha Yogeshappa 2021년 10월 18일
Yes, kmeans clustering is very very slow for me. I have an image stack that amounts to 55M pixels in total. And, clustering them to 256 different clusters was time consuming.
However, I was able to get what I wanted as output but, surely, kmeans seems not to be the way to do it. I will try understanding what rgb2ind(...) does, and see if it fits my need.
Thanks,

댓글을 달려면 로그인하십시오.

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by