ROI was offset after deflate

I’m drawing an ROI on imageJ with deflation:
However, after setRoi, I found that the roi goes towards left.
Afte fillling ROI, there are a few regions that falls out of the yellow region that cannot be filled. I found the reason why these patches cannot be filled, but cannot figure out how the ROI goes towards left.

afn = 'D:\QMDownload\4\CDF34_tif\CDF34.mrxs (d=10.00,x=0,y=0,w=50729,h=51406,_z0,_t0,_c1).tif';
ips = IJ.openImage(afn);
aBgrdNew = [0,0,0,0]';
roiEnlarge = 15;

ip = ips.getProcessor;
ip.setColor(java.awt.Color(0,0,0,0));
ip.setLineWidth(1);

ips.show;

for i = 1 : size(aMarchingHistory,1)
    a = aMarchingHistory{i,4};
    aROI = ij.gui.PolygonRoi(a(:,1)',a(:,2)',size(a,1),ij.gui.Roi.POLYGON);
    aROI_enlarge = ij.plugin.RoiEnlarger.enlarge(aROI,roiEnlarge);
%    ips.setRoi(aROI_enlarge)
    ip.fill(aROI_enlarge);
     ips.draw;
end
ips.show;
x = aROI_enlarge.getXCoordinates;
y = aROI_enlarge.getYCoordinates;
plot(x,y)

%E6%97%A0%E6%A0%87%E9%A2%98

I added the ImageJ tag as this doesn’t appear to involve QuPath. I’m afraid I don’t understand the question well enough to be able to help, and the script itself relies on things that aren’t included to easily reproduce what is happening.

To fix this bug we need a Minimal Working Example (https://en.wikipedia.org/wiki/Minimal_working_example). This script is not minimal and we cannot use it to reproduce the bug.

2 Likes

Below is the working example:

%% To reproduce the problem:
% Step1: a Matlab script: using ij.jar-1.51p
javaaddpath('ij.jar'); 
IJ=ij.IJ;
ips=IJ.createImage('aTestImage','grayscale',5073,5141,1,1,1);
ips.show;

% Step2: set a polygon ROI on image:
% This polygon ROI produce the bug, but others not. I do not know why.
aXposition=[5073,5073,936,1166,1166,1395,1395,1626,1626,1855,1855,2086,2086,...
    2315,2315,2545,2545,2775,2775,3218,3218,3448,3448,4155,4155,4384,4384,4614,4614,4843,4843];

aYpositon=[3236,5140,5140,5140,4950,4950,4759,4759,4567,...
    4567,4377,4377,4187,4187,3999,3999,3811,3811,3810,3810,3999,3999,...
    4187,4187,4186,4186,3997,3997,3618,3618,3236];
aROI=ij.gui.PolygonRoi(aXposition,aYpositon,ij.gui.Roi.POLYGON);
ips.setRoi(aROI);
% we can see that an roi appeared

% Step3.1 :From here, we can either:click Edit-Process-Enlarge to enlarge the ROI,
    % we can see that the roi was enlargd, but enexpectedly moved to left
    
% Step3.2: or set a enlarged roi by code
aROI_enlarge=ij.plugin.RoiEnlarger.enlarge(aROI,roiEnlarge);
ips.setRoi(aROI_enlarge);
% we can see that the roi was enlargd, but enexpectedly moved to left
```![%E6%97%A0%E6%A0%87%E9%A2%98|690x388](upload://6hMa1Sc9C3AEIF8bzeNnLJfcfFY.png)

%E6%97%A0%E6%A0%87%E9%A2%98

If I change a number from x y position, then this bug disapeared.
I am afraid that in future this bug may appear again.

The latest ImageJ daily build (1.52q34) fixes a bug that caused enlarged ROIs to be shifted if the polygon defining the original ROI had points outside the filled polygon.

Here is JavaScript code that reproduces the problem:

  img = IJ.createImage("Untitled", "8-bit black", 500, 500, 1);
  xpoints = [182,184,87,369,369];
  ypoints = [96,315,315,315,95];
  roi = new PolygonRoi(xpoints, ypoints, Roi.POLYGON);
  img.setOverlay(new Overlay(roi));
  roi2 = RoiEnlarger.enlarge(roi, 15);
  img.setRoi(roi2);
  img.show();
3 Likes

I tried and found that the bug was fixed, the background can be filled in right places now.

Thank you very much for this updation

Dear Wayne,

I found that in the updated ij.jar, the enlarged ROI give the correct position. However, it also crop away the single-line-ROI part that stick out from the border of the ROI as in the example you posted. Since ROI enlarge aims to enlarge all the ROI, it is possible to make the single-line stick out also enlarged to cover more than one line thickness?

Thanks.

The line will be enlarged if it has a width greater than zero, as in this example:

  img = IJ.createImage("Untitled", "8-bit black", 500, 500, 1);
  xpoints = [182,182,87,87,369,369];
  ypoints = [96,314,314,315,315,96];
  roi = new PolygonRoi(xpoints, ypoints, Roi.POLYGON);
  img.setOverlay(new Overlay(roi));
  roi2 = RoiEnlarger.enlarge(roi, 15);
  img.setRoi(roi2);
  img.show();

Screenshot