Why is MATLAB rounding to whole number after multiplying and summing DICOM structure fields?

조회 수: 6 (최근 30일)
Working with DICOM files, thinking a meshgrid the best way to work with DICOM coordinates between structure and dose (please tell me if there's a better way), I'm trying to prepare a meshgrid to represent the DICOM coordinates, given the RTDOSE matrix with Attributes specifying the first element's DICOM coordinate:
>> doseinfo.ImagePositionPatient(1)
ans = -191.1930
>> gridxmin = double(doseinfo.ImagePositionPatient(1))
gridxmin = -191.1930
>> doseinfo.Width * doseinfo.PixelSpacing(1)
ans = 380
>> gridxmax=double(gridxmin+ doseinfo.Width *doseinfo.PixelSpacing(1))
gridxmax = 189
>> gridxmin + doseinfo.Width * doseinfo.PixelSpacing(1)
ans = 189
>> gridxmax - gridxmin
ans = 380.1930
As you can see, double appears not to be the problem, but rather, somehow multiplying causes the result to round. Why is it summing to 189 instead of the correct value 188.8070?
It may be worth noting that if I try inserting a factor of 1000 (to convert from millimeter to micrometer), the max value is 0 instead of 188807 (gridxmin = -191193, gridxmax = 0) -- although such a meshgrid can't be used anyway, as it throws the error
Error using repmat
Maximum variable size allowed by the program is exceeded.
Error in meshgrid (line 77)
xx = repmat(xx, ny, 1, nz);
Something weird seems to be happening with this use of structures created from dicominfo. The problem is not resolved if I extract the data from the structure before calculation:
>> xstart = doseinfo.ImagePositionPatient(1);
xwidth = doseinfo.Width; xspacing = doseinfo.PixelSpacing(1);
gridxmin = xstart;
gridxmax = xstart + xwidth * xspacing
gridxmax =
189

채택된 답변

Walter Roberson
Walter Roberson 2016년 7월 7일
When you multiply a double by an integer data type, the result is calculated as double and then converted to the integer data type.
  댓글 수: 6
Walter Roberson
Walter Roberson 2016년 7월 7일
In C, does K * 2 + 1 mean K * (short int)(2) + (short int)1 with the short int widened if necessary depending on the type of K? Or does it mean K * (int)(2) + (int)(1) with the int widened as needed? Or does it mean K * (long int)(2) + (long int)(1) with the int converted to float or double if needed? If you have a long int, float is not necessarily widening...
It has been a while for me; I would need to re-check the C standards to find out exactly how the expression would be interpreted. As (int) is what my memory is suggesting.
Guillaume
Guillaume 2016년 7월 7일
In C (and C++), an integer literal with no suffix is of type int.
In C#, an integer literal with no suffix is of type int, uint, long, or ulong (whichever smallest type can fit the constant).
If K is narrower than int, then the result will be int. If K is wider, the result will be the type of K.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Image Data Workflows에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by