How to round the number to the integer? ( having other's code)
1 visualizzazione (ultimi 30 giorni)
Mostra commenti meno recenti
Hi!
The code is about the rotation of an image. I don't get the rounded part like [ x=int16(x-min(min(x))+1) ]. I can't figure out because it's not intuitive for me. BTW, is there another way to round the number?
I = imread('pout.tif');
theta = 0.25*pi;
R = [cos(theta) -sin(theta);sin(theta) cos(theta)];
for i = 1:size(I,1)
for j = 1:size(I,2)
a = R*[i-1;j-1];
x(i,j)=a(1); %new x
y(i,j)=a(2); %new y
end
end
x=int16(x-min(min(x))+1);%rounded
y=int16(y-min(min(y))+1);%rounded
for i = 1:size(I,1)
for j = 1:size(I,2)
I_new(x(i,j),y(i,j)) = I(i,j);
end
end
subplot(1,2,1); imshow(I);
subplot(1,2,2); imshow(I_new);
0 Commenti
Risposte (1)
Max Heimann
il 2 Feb 2022
Modificato: Max Heimann
il 2 Feb 2022
int16() is a cast into an integer datatype. Those datatypes only support whole numbers and thus the data is rounded via some internal logic to the nearest integer value. Its not the cleanest way to round a number, but in MATLAB it works.
Other ways to round would be round(),ceil() and floor(). They differ in when they round up or down. Their exact behaviour is found in their help texts.
2 Commenti
Rik
il 2 Feb 2022
One more relevant function to mention: fix. It doesn't round in the usual sense of the word, but it has its uses. It rounds towards zero, essentially replacing sign(x)*floor(abs(x)).
Walter Roberson
il 2 Feb 2022
Technically, int16() is a cast rather than a typecast . A cast changes representation while attempting to preserve value, whereas a typecast preserves representation while changing interpretation.
Calculations on integer datatypes (except for the 64 bit ones) are defined to be "as-if" the operands were promoted to double precision, and the operation done in double precision, and then the values were cast back to the integer data types. The conversion of double to the integer data types is defined as using round() -- this is documented.
Vedere anche
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!