Create a LUT Table by Euclidean Distance

조회 수: 3 (최근 30일)
Royi Avital
Royi Avital 2015년 6월 26일
답변: Royi Avital 2015년 6월 29일
Hello,
I have a list of N vectors in 3D space (Points in RGB color space). We'll call that our Dictionary.
The whole data M is in 3D Space ({0, 1, ..., 255} x {0, 1, ..., 255} x {0, 1, ..., 255}).
I want to create a LUT which works in the following way, each point in the Space will be mapped to the point dictionary according to Euclidean Distance.
This can be also seen as a quantization process of an image. I want to create a LUT Table s.t. is pixel will be quantized into a value of the Code Book which is the most similar to it in the L2 / Euclidean sense.
How can I do it in a vectorized, efficient manner?
This is a sample code I created using loops:
function [ mRgbLut ] = CreateRgbLUT( mCodeBook )
mRgbLut = zeros(256, 256, 256);
numLevels = size(mCodeBook, 1);
for iRValue = 0:255
redIdx = iRValue + 1;
for jGValue = 0:255
grennIdx = jGValue + 1;
for kBValue = 0:255
blueIdx = kBValue + 1;
vCurrColor = [iRValue, jGValue, kBValue];
vRgbDistance = sum(((repmat(vCurrColor, [numLevels, 1]) - mCodeBook) .^ 2), 2);
[minRgbDist, codeBookIdx] = min(vRgbDistance);
mRgbLut(redIdx, grennIdx, blueIdx) = sub2ind([256, 256, 256], mCodeBook(codeBookIdx, 1), mCodeBook(codeBookIdx, 2), mCodeBook(codeBookIdx, 3));
end
end
end
end
Thank You.

채택된 답변

Royi Avital
Royi Avital 2015년 6월 29일
Ok, I created something using the ' allcomb ' function from File Exchange.
Here is the code:
function [ mRgbLut ] = CreateRgbLUT( mCodeBook )
numLevels = size(mCodeBook, 1);
vBaseValues = [0:255];
mCombValues = allcomb(vBaseValues, vBaseValues, vBaseValues);
mCombValues = [mCombValues(:, 3), mCombValues(:, 2), mCombValues(:, 1)];
mCodeBookReshaped = reshape(mCodeBook.', [1, 3, numLevels]);
mRgbDistance = bsxfun(@minus, mCombValues, mCodeBookReshaped) .^ 2;
mRgbDistance = sum(mRgbDistance, 2);
[minRgbDist, codeBookIdx] = min(mRgbDistance, [], 3);
mRgbLut = reshape(codeBookIdx, [256, 256, 256]);
end
I hope this might assist someone (It something like the step in K Means).

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Display and Exploration에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by