change data type to double

6 views (last 30 days)
mohd akmal masud
mohd akmal masud on 30 Mar 2019
Answered: Walter Roberson on 30 Mar 2019
Dear all
i want to know, what is the purpose we change data type to double?
like below
Image_name = 'your_image.dcm'; % add your file name
%% read dicom image
IMG = dicomread(Image_name);
IMG = double(IMG); % chage data type double

Answers (1)

Walter Roberson
Walter Roberson on 30 Mar 2019
In MATLAB, if you do mathematical operations on data that is integer data type, then the result is either an error or else the same integer data type. For example you are simply not permitted to take log() or sin() or exp() or sqrt() of uint8 data type, and uint8(17)/2 gives a result which is uint8(9) rather than giving 8.5
Furthermore, if you subtract a value from an unsigned integer data type, then the result can never be negative and instead "underflows" to 0. So for example uint8([3 5]) - uint8([5 3]) does not give [-2 2] and instead gives uint8([0 2]) because uint8 cannot represent negative values.
This especially affects image subtraction. If you have two uint8 images, A and B, then A-B would never be negative.
Suppose you were trying to do object detection by background subtraction. Then if you had a bright background and a dark object in front of it, then if you subtracted as unsigned integer then low value (dark) minus high value (bright background) could not give negative and so would give 0 -- which is the same you would get where there was no object at all and the two images were the same. But if you were to take double(A)-double(B) then the result could be as negative as was needed.

Community Treasure Hunt

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

Start Hunting!