Create a LUT Table by Euclidean Distance

1 次查看(过去 30 天)
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 中查找有关 Get Started with Image Processing Toolbox 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by