Login  Register

Re: roi select two rois

Posted by Toby Cornish on Jul 30, 2008; 2:38pm
URL: http://imagej.273.s1.nabble.com/roi-select-two-rois-tp3695488p3695489.html

This doesn't really answer the original poster's question, but this may address yours, Christophe.  Here is a macro function for combining an arbitrary number of selections.  There may be a better way, but this seems to work (code below).

toby

newImage("test", "8-bit Black", 128, 128, 1);
makeRectangle(54, 35,10,10);
roiManager("Add");
makeRectangle(70, 70,10,10);
roiManager("Add");

rois = newArray(0, 1);
index = combineSelections(rois,true);
print ("new index = "+index);
exit();

// FUNCTIONS BELOW ================================

function combineSelections(rois,deleteOriginals) {
 
  // get the mimimum width and height for a scratch image
  scratchWidth=0;
  scratchHeight=0;
  for (i =0; i < rois.length; i++) {
    roiManager("select",rois[i]);
    getSelectionBounds(x, y, width, height);
    if (x+width>scratchWidth) scratchWidth = x+width;
    if (y+height>scratchHeight) scratchHeight = y+height;
  }

  //create scratch image
  newImage("scratch", "8-bit Black", scratchWidth, scratchHeight, 1);
  selectWindow("scratch");

  // fill and combine the selections
  for (i =0; i < rois.length; i++) {
    roiManager("select",rois[i]);
    run("Add...", "value=255");
  }
  run("Select All");
  run("Invert LUT");
  run("Create Selection");
  roiManager("Add");
 
  //cleanup
  if (deleteOriginals) {
    sort(rois);
    for (i =0; i < rois.length; i++) {
      roiManager("select",rois[i]-i);
      roiManager("Delete");
    }    
  }
  selectWindow("scratch");
  close();
 
  // return the new selection index
  newIndex = roiManager("Count")-1;
  roiManager("select",newIndex);
  return newIndex;
}

// wayne's qsort

  function sort(a) {quickSort(a, 0, lengthOf(a)-1);}

  function quickSort(a, from, to) {
      i = from; j = to;
      center = a[(from+to)/2];
      do {
          while (i<to && center>a[i]) i++;
          while (j>from && center<a[j]) j--;
          if (i<j) {temp=a[i]; a[i]=a[j]; a[j]=temp;}
          if (i<=j) {i++; j--;}
      } while(i<=j);
      if (from<j) quickSort(a, from, j);
      if (i<to) quickSort(a, i, to);
  }

>Date:    Tue, 29 Jul 2008 17:12:24 +0200
>From:    Christophe Leterrier <[hidden email]>
>Subject: Re: roi select two rois
>
>There is indeed a problem with selecting multiple ROIs in a macro. This is
>more easily seen when trying to use the "Combine" command on two selected
>ROIs in a macro. Below is a question I sent a few days ago and the response
>of Wayne Rasband :
>
>
> I would like to select two ROIs in the manager and combine them in a
>> macro. What is wrong in the following code ?
>>
>> newImage("test", "8-bit Black", 128, 128, 1);
>> makeRectangle(54, 35,10,10);
>> roiManager("Add");
>> makeRectangle(70, 70,10,10);
>> roiManager("Add");
>> roiManager("select",0);
>> setKeyDown("alt");
>> roiManager("select",1);
>> roiManager("Combine");
>>
>> The composite macro is displayed, but I get the error : "More than one
>> item must be selected, or none". Using shift as a modifier leads to
>> the same error. Any ideas ?
>>
>
>Dear Christophe,
>
>As far as I know, there is no way in a macro to select a subset of the ROIs
>in the ROI Manager. There does appear to be a way to programatically select
>multiple items in an java.awt.List.
>
>-wayne
>
>So basically it cannot be done... Too bad this java.awt.List seems a bit
>limited (another gripe is that you cannot expand the ROI manager window to
>see more ROIs or to see longer names, at least on OSX).
>
>Christophe



Toby C. Cornish, M.D., Ph.D.
Pathology Resident
Johns Hopkins Medical Institutions
[hidden email]