Lets say that we have a 256x256 double image, how can we convert it into a 256x256 uint8?

5 件のコメント

Khairul Bashar Syed
Khairul Bashar Syed 2014 年 10 月 26 日
Say the image matrix is im. So just use im=uint8(im);
Satavisha
Satavisha 2024 年 5 月 11 日
But when I am using uint8 on my image the output is totally black then what to do?my image is (626x696 double).
Stephen23
Stephen23 2024 年 5 月 11 日
Image Analyst
Image Analyst 2024 年 5 月 11 日
@Satavisha you may be able to leave it as double, depending on what you want to do. If your double image values are all less than 1, then using uint8() will make them all zero. If you leave them as double, then they will have their original values. Whenever I use double images, I display them with [] in the call to imshow so that the image scales properly for display.
imshow(doubleImage, []);
That way you can both keep your original values, AND see it when you display it.
If you use im2uint8, it somehow scales and then clips the data. It does not seem to "shift" the data to place your min at 0 (or some fraction of 255) and place your max at 255. Just try some examples:
im2uint8([0.5, 0.8])
ans = 1x2
128 204
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
im2uint8([91.2, 2345.6])
ans = 1x2
255 255
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
You could manually scale it using mat2gray or rescale
img8 = mat2gray([0.5, 0.6, 0.8]) % Produces a double image in the range 0-1
img8 = 1x3
0 0.3333 1.0000
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
img8 = uint8(255 * img8) % Convert range to 0-255 and cast to uint8 data type.
img8 = 1x3
0 85 255
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
img8 = uint8(rescale([91.2, 412.4, 2345.6], 0, 255))
img8 = 1x3
0 36 255
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
DGM
DGM 2024 年 5 月 11 日
編集済み: DGM 2024 年 5 月 11 日
Using rescale(), mat2gray() or imshow(...,[]) won't preserve the original contrast, and the latter only works on single-channel images anyway.
inpict = imread('peppers.png'); % RGB, properly-scaled uint8
inpict = double(inpict); % RGB, improperly-scaled double (not damaged yet)
imshow(inpict,[]) % does not work on RGB anyway
inpict = imread('peppers.png'); % RGB, properly-scaled uint8
inpict = im2double(inpict); % RGB, properly-scaled double
inpict = uint8(inpict); % RGB, unit-scale uint8 (permanently destroyed)
imshow(inpict,[]) % does not work on RGB anyway
inpict = imread('peppers.png'); % RGB, properly-scaled uint8
inpict = im2double(inpict); % RGB, properly-scaled double
inpict = im2uint8(inpict); % RGB, properly-scaled uint8
imshow(inpict) % it works fine
The answer is to pay attention to class and scale instead of presuming that image data always extends to the available dynamic range. If you're not paying attention to both, then you're just throwing away information. If you don't know what that means, just don't use double() or uint8(). Don't create improperly-scaled images unless you're making extra effort to make sure that you handle them correctly. Use im2double() and im2uint8() to handle conversion of both class and scale at the same time. So long as your images are always properly-scaled, imshow()/imwrite()/etc will handle them correctly, and contrast will be preserved.
If your input is a double array on an arbitrary scale, then yes, using rescale() or mat2gray() or normalize() might make sense as part of bringing the data into a standard scale.

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

 採用された回答

Star Strider
Star Strider 2014 年 10 月 26 日

1 投票

I would use the uint8 function.

11 件のコメント

Waseem AL Aqqad
Waseem AL Aqqad 2014 年 10 月 26 日
I believe that there is no such function.
What's the complete function? is it im2unit8(I)? Thanks!
Star Strider
Star Strider 2014 年 10 月 26 日
The uint8 function exists (at least in the documentation). Click on the link.
An equivalent Image Processing Toolbox function is im2uint8.
Mikhail
Mikhail 2014 年 10 月 26 日
Of course you can use uint8.
Star Strider
Star Strider 2014 年 10 月 26 日
Thank you Mikhail, and welcome to the ranks of those of us who believe in the existence of uint8!
Waseem AL Aqqad
Waseem AL Aqqad 2014 年 10 月 26 日
guys,
it keeps giving me this error:
??? Undefined function or method 'im2unit8' for input arguments of type 'double'.
I dunno why?
Star Strider
Star Strider 2014 年 10 月 26 日
Do you have the Image Processing Toolbox?
In the Command Window type:
which im2uint8 -all
You should get as the result:
C:\Program Files\MATLAB\R2014b\toolbox\images\images\im2uint8.m
C:\Program Files\MATLAB\R2014b\toolbox\images\images\@gpuArray\im2uint8.m % gpuArray method
with the correct directory for the version of MATLAB you are using.
Waseem AL Aqqad
Waseem AL Aqqad 2014 年 10 月 26 日
I got this result:
C:\Users\KUWA2002\Desktop\MATLAB_portable\R2010b\toolbox\curvefit\cftoolgui\private\im2uint8.m % Private to cftoolgui C:\Users\KUWA2002\Desktop\MATLAB_portable\R2010b\toolbox\images\images\im2uint8.m
Star Strider
Star Strider 2014 年 10 月 26 日
It seems you have the Image Processing Toolbox and im2uint8. (I don’t know what it’s doing in the Curve Fitting Toolbox, but since I don’t have the CFT, I won’t comment further.) You should be able to use it since it’s in your MATLAB search path.
If im2uint8 won’t work for you for whatever reason, see if uint8 will produce an acceptable result.
(I am certain uint8 exists. I saw its footprints in the garden and strands of its fur snagged on the hedge!)
Star Strider
Star Strider 2014 年 10 月 26 日
Yes. Use uint8 and see if it does what you want.
Star Strider
Star Strider 2014 年 10 月 26 日
Actually, very few of us here are MathWorks employees. Most of us (including me) are unpaid volunteers who do our individual and collective best to share what we know.
If you cannot get im2unit8 or uint8 working, contact MathWorks Tech Support to see if they can figure out what the problem is.
Star Strider
Star Strider 2014 年 10 月 26 日
My pleasure!
I wish we could have gotten im2uint8 or uint8 working for you.

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

その他の回答 (1 件)

Image Analyst
Image Analyst 2014 年 10 月 26 日

1 投票

There is no function 'im2unit8' but there is a function im2uint8. It matters how you spell it and you spelled it incorrectly. I never use it - I use uint8(). im2uint8() also does scaling, of course you could use uint8(mat2gray()) to scale it to the full range.

2 件のコメント

Star Strider
Star Strider 2014 年 10 月 26 日
I misspelled it in my last Comment but not earlier. Waseem has it in his MATLAB search path but apparently can’t use it or uint8.
That seems to be the problem.
Star Strider
Star Strider 2014 年 10 月 26 日
Waseem — You spelled it correctly earlier in this thread. However we did not see your code, so I assumed you simply misspelled it in your Question.

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

カテゴリ

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

質問済み:

2014 年 10 月 26 日

編集済み:

DGM
2024 年 5 月 11 日

Community Treasure Hunt

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

Start Hunting!

Translated by