The active image does not have selection

fiji
imagej
macro

#1

I am running a fiji macro code which is for loop calling one image each time to do freehand selection.

selectWindow(imagename);
makeSelection("freehand", x1, y1);
roiManager("Add");

but I get the error: The active image does not have selection. I have : selectWindow(imagename); which I added to select the image but did not work. Anyone can help?


How to add a string to a path of file which is changing in each loop in imagej macro language?
#2

What’s the content of your x1 and y1 variables? The following works for me:

x1 = newArray(20, 20);
y1 = newArray(30, 40);

makeSelection("freehand", x1, y1);
roiManager("Add");

EDIT:
I saw you posted this in a different active topic already:

To avoid wasting the time of those people trying to help you, please don’t open new topics at will without any context.

  • Either keep posting in the same thread if the topic title still matches the ongoing discussion, or
  • when creating a new topic, at least link to the other topic, so that others have some context to your question.

We’re trying to be nice and helpful here on the forum, but please don’t waste our time, and (as I posted on another thread already) put as least as much effort into formulating your question as you’d others expect to spend on the reply.


#3

@imagejan Thanks for your quick response. I already have x1 = newArray(20, 20);
y1 = newArray(30, 40); in my code.
The reason I am asking question in different contexts is that I try to segment the problem I have to smaller one so people can help me easier than asking a big question and sending the whole code which takes a lot of time to understand what is going on. People may have different experiences for example someone may worked with ROI before and if see my question which includes ROI in title, he will open and check and help me.
I am new user of imagej as well as this forum and I dont really want to waste people’s time. I just think I can ask for help in an organized and specific manner which can be helpful for others instead of asking question which is only limited to my code and not useful that much for others. You can also find likes that other people give under the answers to my questions which means it was helpful for others as well.
Thank you again


#4

Does that mean your issue is solved?


#5

@imagejan I mean I had it in my code while I was getting error. No it is not solved yet.


#6

Sorry, I can’t reproduce your issue, as in my ImageJ it is working as expected.

Can you please post a minimal but complete and verifiable example macro (MCVE) that reproduces the issue?


#7

@imagejan Here is the code. Just change the path file to the address of two sample 1.tif and 2.tif images in your computer.

        x1 = newArray(46,47,48,48,49,50);
       y2 = newArray(837,838,837,836,837,836);
        x2 = newArray(46,47,48,48,49,50);

for (j=1;j<3; j=j+1){

path="C:/Users/Zeynab/Desktop/script test/"+j+".tif";
open(path);

	xn="x"+j;
	yn="y"+j;

imagename=d2s(j, 0)+".tif";
selectWindow(imagename);
makeSelection("freehand", xn, yn);
roiManager("Add");
     type = selectionType();
     newImage("Outline", "b-bit white", getWidth, getWidth, 1);
     setColor(0); // black
     moveTo(xn[0], yn[0]);
     
     for (i=1; i<xn.length; i++)
         lineTo(xn[i], yn[i]);
     if (type==2 || type==3)
        lineTo(xn[0], yn[0]);
        
 close(); 
 
}run("script:Macro.ijm.ijm", "show");

#8

@imagejan I changed a code a little and now I get the error
image
If you run the one I sent you should get this error.


#9

Looking only quickly at your code, I see that xn and yn are strings (due to the string concatenation "x"+j). As the error message states, an array (of numbers) is required.

Array expected in line 20


#10

@imagejan I need to make array xn and yn. How can I do that?


#11

Hello Zeynab -

I can speculate about the source of your problem.

Quoting from the code you posted earlier:

        x1 = newArray(46,47,48,48,49,50);
       y2 = newArray(837,838,837,836,837,836);
        x2 = newArray(46,47,48,48,49,50);

for (j=1;j<3; j=j+1){

[snip]

	xn="x"+j;
	yn="y"+j;

[snip]

     for (i=1; i<xn.length; i++)
         lineTo(xn[i], yn[i]);

I think that you are trying to define multiple arrays that specify
ROIs, and then loop over them. I think that when j is equal
to 2, you want xn="x"+j to be the array x2, but of course
it isn’t. It is the character string “x2”.

The root of this problem is that ImageJ’s macro language (IJM)
does not support arrays of arrays. (Most language do.) Thus
(other than using a scripting language that does support arrays
of arrays) you need to do something like:

arr1 = newArray (1, 2, 3)
arr2 = newArray (4, 5, 6)
arr3 = newArray (7, 8, 9)

for (i = 1, i <= 3; i++) {
  if       (i == 1)  arri = arr1;
  else if  (i == 2)  arri = arr2;
  else     (i == 3)  arri = arr3;

  // do something with arri ...
}

(You would really like arr to be an array of arrays so that arr[1]
is equal to arr1, and so on, so you can just use arr[i] in your
for loop without the if / else if chain. But that is not something
you can do in IJM.)

As an aside, please let me reiterate Jan’s comment. It would
help a lot if you could post complete, trimmed down code
examples that clearly illustrate the issue you are facing.
Sometimes you post suggestive code fragments that don’t
have enough context for us to tell whether they are right or
wrong. And sometimes they are misleading because the
details don’t match what you are actually trying to do.

Please also don’t post the full code you are trying to make
work. First simplify it as much as you can while still preserving
the issue you are trying to work through. (From experience, I’ve
often discovered the solution to my problem by going through
this procedure before even asking a question.)

Post the full, runnable (at least up to the point of error) simplified
code, together with any error messages you get, and tell us what
you expected to get instead. (Lastly, please post actual, textual
code and error messages, not screen shots. Save screen shots
for issues with the gui or special cases like the contents of the
ROI manager.)

Thank, mm


#12

You could store them as arrays of strings and then use split.

x = newArray("46,47,48,48,49,50", "46,47,48,48,49,50");
y = newArray("837,838,837,836,837,836", "837,838,837,836,837,836");
num_sets = x.length;
for (j = 0; j < num_sets; j++) {
    xn = split(x[j], ",");
    yn = split(y[j], ",");
    // your stuff here
}

#13

*Hi friends I’m new to imageJ and I could not be able to create a .obj 3D file without losing some data from the .raw file stack.
*Basically the .raw file contains many number of images arranged in stack.
*These images are black and white images only. Each and every pixel in this image is very important.
*After importing the .raw file into the imagej it can be saved as a .obj generally. But while saving this .obj even after with the low threshold and re-sampling factor as 1, some of the pixel datas are missing in the 3d model.
*Can anyone help me with this issue please?
A small information will be really useful too. All I need is a 3d model output from the Image stack without any loss in data.
Thanks in advance.


#14

It sounds like your issue is completely unrelated to the main topic of this thread. Could you please create a separate thread?


#15

If you would prefer to have multidimensional arrays, you can copy the functions from this example macro.

/********************* this comment is 80 characters long *********************/
function NDACreate(dims) {
    length = 1;
    num_dims = dims.length;
    for (i = 0; i < num_dims; i++) {
        if (dims[i] < 0) {
            exit("ND array: The length of dimension "+(i+1)+" is negative."
                 +" Dimensions must have positive lengths.");
        }
        length = length * dims[i];
    }
    return newArray(length);
}

function NDAGetIndex(dims, idcs) {
    if (dims.length != idcs.length) {
        exit("ND array: The number of dimensions and number of indices do not"
             +" match. There must be one (1) index for each dimension.");
    }
    index = 0;
    mult = 1;
    num_dims = dims.length;
    for (i = 0; i < num_dims; i++) {
        if (dims[i] < 0) {
            exit("ND array: The length of dimension "+(i+1)+" is negative."
                 +" Dimensions must have positive lengths.");
        }
        index = index + mult * ((idcs[i] % dims[i] + dims[i]) % dims[i]);
        mult = mult * dims[i];
    }
    return index;
}

function NDAGet(array, dims, idcs) {
    return array[NDAGetIndex(dims, idcs)];
}

function NDASet(array, dims, idcs, value) {
    array[NDAGetIndex(dims, idcs)] = value;
}

dims = newArray(2, 3, 4);
idcs = newArray(dims.length);

//create an array where the values are the corresponding linear indices
a = NDACreate(dims);
counter = 0;
for (i = 0; i < dims[2]; i++) {
    for (j = 0; j < dims[1]; j++) {
        for (k = 0; k < dims[0]; k++) {
            idcs = newArray(k, j, i);
            NDASet(a, dims, idcs, counter);
            counter = counter + 1;
        }
    }
}

Array.show(a);
idcs = newArray(1, 2, 2);
print(NDAGet(a, dims, idcs));
idcs = newArray(-1, 8, -6);
print(NDAGet(a, dims, idcs));

#16

@mountain_man Thanks for your comments and helps.


#17

good idea! Thank you