ROI to IJK coordinates with MATLAB bridge

Operating system: Windows 10
Slicer version: 4.11.0
Expected behavior: Find IJK coordinates of ROI
Actual behavior: I get negative or bigger than dimensions IJK coordinates

Dear slicers,

I am trying to get iMin, iMax, jMin, jMax, kMin, kMax from some Annotation ROI using a MATLAB bridge module such that
ROI = Volume[iMin:iMax,jMin:jMax,kMin:kMax] and then I will perform some analysis on the ROI on MATLAB.

I have created a MATLAB module thanks to MATLAB bridge to get IJK coordinates of an ROI.
Inputs of module

  • ROI
  • Volume (to get ijkToLpsTransform…)

Outputs

  • IJK coordinates so ROI = Volume(iMin:iMax,jMin:jMax,kMin:kMax)

I based my code on 2 MATLAB bridge module examples :

Here is my code

function outputParams=ijkROI(inputParams)
% returns ijk coordinates of Annotation ROI
% Parameters:
%   inputParams.region: ROI
%   inputParams.inputvolume: input volume
%   outputParams.stringCoordinates: coordinates of ROI as string 'iMin:iMax,jMin:jMax,kMin:kMax'

imageValue = cli_imageread(inputParams.inputImage);
regionValue = cli_pointvectordecode(inputParams.region);
[~, regionCount] = size(regionValue);

% only one ROI
assert(regionCount==1)
regionIndex = 1;

% get center of ROI and radius
radius_LPS = [regionValue(4:6,regionIndex); 0];
% radius_IJK = abs(round(imageValue.ijkToLpsTransform\radius_LPS));
radius_IJK = ceil(abs(imageValue.ijkToLpsTransform\radius_LPS));

center_LPS = [regionValue(1:3,regionIndex); 1];
center_IJK = round(imageValue.ijkToLpsTransform\center_LPS);

% get coordinates of ROI
ROIijk = [center_IJK(1)-radius_IJK(1),...
    center_IJK(1)+radius_IJK(1),...
    center_IJK(2)-radius_IJK(2),...
    center_IJK(2)+radius_IJK(2),...
    center_IJK(3)-radius_IJK(3),...
    center_IJK(3)+radius_IJK(3)];

% assert coordinates are not outside input volume
volumeSize = size(imageValue.pixelData);
% assert((ROIijk(1) > 1) & (ROIijk(3) > 1) & (ROIijk(5) > 1))
% assert((ROIijk(2) < volumeSize(1)) & (ROIijk(4) < volumeSize(2)) & (ROIijk(6) < volumeSize(3)))

% Write outputs
outputParams.stringCoordinates = sprintf('%d:%d,%d:%d,%d:%d',ROIijk(1),ROIijk(2),ROIijk(3),ROIijk(4),ROIijk(5),ROIijk(6));

I have tried it with a random Annotation ROI inside MRHead volume.
The center given by regionValue = cli_pointvectordecode(inputParams.region) is consistent with coordinates of center in Red, Green and Yellow slices in user’s interface.
Yet radius is bigger than expected and I get negative coordinates or coordinates bigger than dimension.

Any idea why the ijkToLpsTransform gives results outside the ROI ?

Thank you very much

Eventually I have found the problem.
I need to convert first RAS coordinates to LPS coordinates. With this piece of code everything works smoothly.

% get center of ROI
center_RAS = [regionValue(1:3,regionIndex); 1];
% Point coordinates are provided by Slicer in RAS coordinate system
% Multiply the first two components by -1 to obtain points in LPS coordinate system
center_LPS = [-1*center_RAS(1:2,:); center_RAS(3:end,:)];
center_IJK = round(imageValue.ijkToLpsTransform\center_LPS);

% get radius of ROI
radius_RAS = [regionValue(4:6,regionIndex); 0];
radius_LPS = [-1*radius_RAS(1:2,:); radius_RAS(3:end,:)];
radius_IJK = round(imageValue.ijkToLpsTransform\radius_LPS);