New to ImageJ need help generating points within selection

imagej

#1

Aloha everyone,

I am trying to generate 100 random points within a free-hand selection of a tree canopy. I have tried several macros that I’ve found online but cannot get them to only generate points within the selection. I know that these macros are not tailored to my images and I don’t know how to program a macro from scratch so I’m not surprised that they aren’t working for me.

I am trying to use ROI.contains, but I keep getting errors as I’m not sure that I’m defining X and Y correctly.

My image consists of a free-hand selection with the area outside deleted so that it is all black, there is also no black in the image. I believe that what I have to do is tell ImageJ to generate 100 random points on my image but to also not place points in areas that have an RGB=255.

I have tried converting to binary to make analysis easier, but there are different shades of green in the background and in the canopy itself so the binary images do not properly separate the canopy from the background.

Any help or advice is much appreciated.

Mahalo


#2

@MBHI

So… the good news…

You don’t have to! You can use the Macro Recorder… to get you started writing code (without having to know the macro language ahead of time!). Also - these links are SUPEr helpful when starting with scripting in ImageJ/Fiji:

Here is a quick macro I just made that generates random points within a selected area (I just used the image sample “Blobs” that comes with FijiFileOpen Samples...Blobs (25k)):

n = 100; // number of dots


makeRectangle(47, 57, 115, 112); // not sure how you want to make the selection area... for the "big" ROI...
roiManager("Add"); // add that selection area to the ROI Manager
Roi.getBounds(x, y, w, h);
for (i = 0; i < n; i++) {
	roiManager( "select", 0 ); // select the first, "big" ROI
	x1 = random() * w + x;
	y1 = random() * h + y;
	if ( selectionContains(x1, y1) == true ) { // if coordinates are inside the "big" ROI 
		makePoint(x1, y1); // generate random point
		roiManager("Add"); // add the point to the ROI Manager
	}
}

I hope this helps get you started !!! I dont’ have time to quick add the RGB=255 condition… but give it a try and post again if you have questions. And in that case - post your code and an image - so we can run it ourselves too to better help.

Gotta run for now!

eta :slight_smile:


#3

Thank you so much for the help! I will have some time to play around with your suggestion later today and will update on my progress.

I’m having trouble uploading the image directly so I made a google folder that anyone can access with this link

https://drive.google.com/drive/folders/1KHVrFceH4BoaILSxwvqdpRdTIlEZXk4f?usp=sharing


#4

Is there a way to set the ROI to be only the area inside of what is currently selected?


#5

I’ve managed to get it to only select points within the current selection! but it does not keep going until there are 100 points, ie it returns a different number of points each time b/c some points randomly fall outside the selection. Just looking at the code it seems like this should not be happening.

n = 100; // number of dots

selectionName;
roiManager(“Add”);
Roi.getBounds(x,y,w,h);
for (i = 0; i < n; i++) {
roiManager( “select”, 0 ); // select the first, “big” ROI
x1 = random() * w + x;
y1 = random() * h + y;
if ( selectionContains(x1, y1) == true ) { // if coordinates are inside the “big” ROI
makePoint(x1, y1); // generate random point
roiManager(“Add”); // add the point to the ROI Manager
}
}


#6
    n = 100; // number of dots


selectionName; 
roiManager("Add");
Roi.getBounds(x,y,w,h);
for (i = 0; i < n; i++) {
	roiManager( "select", 0 ); // select the first, "big" ROI
	x1 = random() * w + x;
	y1 = random() * h + y;
	if ( selectionContains(x1, y1) == true ) { // if coordinates are inside the "big" ROI 
		makePoint(x1, y1); // generate random point
		roiManager("Add"); // add the point to the ROI Manager
	}
}

#7

@MBHI

Yes! You are right… what we need to do in this code is count the number of times a point is added so you get up to 100 points… otherwise - those points that are outside the selected area are counted even though they are not added… you see?

So something like:

#@int(label = "Number of random points to generate:") numPoints


makeRectangle(47, 57, 115, 112); // not sure how you want to make the selection area... for the "big" ROI...
roiManager("Add"); // add that selection area to the ROI Manager
Roi.getBounds(x, y, w, h);
count = 0;
while (count < numPoints) { 
	roiManager( "select", 0 ); // select the first, "big" ROI
	x1 = random() * w + x;
	y1 = random() * h + y;
	if (selectionContains(x1, y1) == true ) { // if coordinates are inside the "big" ROI 
		makePoint(x1, y1); // generate random point
		roiManager("Add"); // add the point to the ROI Manager
		count++; // ONLY increase count when point is added
	}
}

eta

Note: I also added a Script Parameter there so you can ask the user for the number of points to create… just for fun! But of course - you can hard-code this value if you want. :slight_smile:


#8

IT WORKS!!! For whatever reason, I had to set numPoints to 99 to get 100 points on the image, but it works and I am so stoked. Thank you both so much for the help.

Big Mahalo

{
numPoints=99

selectionName; 
roiManager("Add");
Roi.getBounds(x,y,w,h);
count = 0;
while (count < numPoints) { 
	roiManager( "select", 0 ); // select the first, "big" ROI
	x1 = random() * w + x;
	y1 = random() * h + y;
	if (selectionContains(x1, y1) == true ) { // if coordinates are inside the "big" ROI 
		makePoint(x1, y1); // generate random point
		roiManager("Add"); // add the point to the ROI Manager
		count++; // ONLY increase count when point is added
	}
}

#9

please try to think why you had to set 99 instead of 100 to have 100 points.
it will help you to improve