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

1 回表示 (過去 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 Center および File ExchangeDICOM Format についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by