# 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'

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

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

% get center of ROI and radius

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

% get coordinates of ROI

% 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);