LookUp Table code to convert uint8 image to uint16 image using LUT

I have an image and a look up table that I want to use to transform the image pixel values. My original image (A) is uint8. My LUT has 256 rows with values between 0 and 4095. I want the output image (B) to have values between 0 and 4095.
I tried using B = intlut(A, LUT), but it does not work because it enforces the A, LUT, and B all have same class (either Uint8 or 16).
If I convert A to uint16 and try B = intlut(A, LUT), I will have this error because LUT is only 256 rows:
_ Error using coder.internal.errorIf (line 8) LUT must contain 65536 elements if it is uint16 or int16._
Here is a sample code:
A = imread('cameraman.tif');
LUT = uint16(repmat([0 150 255 4095],1,64));
B = intlut(A, LUT)
Any suggestions? By the way, I can't use the simulink. Also no for loop because it is slow. I want this vectorized.
Thanks

2 件のコメント

Luca
Luca 2017 年 3 月 6 日
I figured it out:
A = imread('cameraman.tif');
LUT = uint16(repmat([0 150 255 4095],1,64));
B = LUT(A+1);
Walter Roberson
Walter Roberson 2017 年 3 月 6 日
B = LUT(double(A)+1);

サインインしてコメントする。

 採用された回答

Walter Roberson
Walter Roberson 2017 年 3 月 6 日

0 投票

B = LUT(double(A) + 1);
The double(A) is there because A is uint8, maximum 255, and uint8(255)+1 is uint8(255) rather than the 256 needed to act as the correct index.

3 件のコメント

Luca
Luca 2017 年 3 月 6 日
This does not work because the output image (B) is an unit8 with all values of 255! As I mentioned, B needs to have values between 0 and 4095.
Walter Roberson
Walter Roberson 2017 年 3 月 6 日
A = randi([0 255], 3, 5, 'uint8');
LUT = randi([0 4095], 1, 256, 'uint16');
B = LUT(double(A) + 1);
class(B)
max(B(:))
Luca
Luca 2017 年 3 月 7 日
Thanks for the heads up on the MATLAB's weird way of indexing and conversion! Accepted your answer.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeConvert Image Type についてさらに検索

質問済み:

2017 年 3 月 6 日

コメント済み:

2017 年 3 月 7 日

Community Treasure Hunt

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

Start Hunting!

Translated by