The maximum value is not changing even after adding 1

조회 수: 6 (최근 30일)
Palguna Gopireddy
Palguna Gopireddy 2024년 6월 13일
댓글: Voss 2024년 6월 14일
I have an image with values ranging from [0,255]
It's data is attached below as data.mat below.
I read it and added 1 to its data.
The minimum value of data is changed to 1, but the maximum value of data is still 255.
I don't understand why.

채택된 답변

Voss
Voss 2024년 6월 13일
편집: Voss 2024년 6월 13일
x0_test is an array of unsigned 8-bit integers
load data
class(x0_test)
ans = 'uint8'
The minimum value an 8-bit unsigned integer can have is 0, and the maximum value is 2^8-1 = 255.
In MATLAB, the convention when performing an operation on a variable of an integer class that will cause the variable to exceed the maximum value for the class (such as adding 1 to an 8-bit unsigned integer whose value is 255) is to cap the value of the variable at the max value of the class. That's why 255 stays at 255 after adding 1.
x = uint8(255:-15:1)
x = 1x17
255 240 225 210 195 180 165 150 135 120 105 90 75 60 45 30 15
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
x+1
ans = 1x17
255 241 226 211 196 181 166 151 136 121 106 91 76 61 46 31 16
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
x+2
ans = 1x17
255 242 227 212 197 182 167 152 137 122 107 92 77 62 47 32 17
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
x+100
ans = 1x17
255 255 255 255 255 255 255 250 235 220 205 190 175 160 145 130 115
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>

추가 답변 (1개)

Ganesh
Ganesh 2024년 6월 13일
The reason is quite simple, your data is stored as an "uint8" which stands for "unsigned integers in 8 bit". The range of acceptable values for that type is from 0 to 255, which makes it ideal to store image data.
y = uint8(255)
y = uint8 255
y = y+1
y = uint8 255
x = uint8(243);
x = x+1
x = uint8 244
This is simply a way that MATLAB handles it. If you were to do the same in C, there will be an integer overflow and the value is changed to "0" instead. MATLAB protects this integer overflow and hence retains the value of 255.

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

제품


릴리스

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by