# Best way to macro finding most black pixels in a selection

Using Fiji right now. Im trying to find the best way to have Fiji scan an image with a square (ex a square that is 4 cm by 4 cm) and locate the area in the image that contains the most black pixels (in an 8bit image). Any ideas?

You can iterate over all pixels in the image (each square and sum them up) with:

https://imagej.nih.gov/ij/developer/macro/functions.html#getPixel

Slower but ready to apply for smaller images:

You can simply use this macro below (change the values to your need) to create a ROI grid with defined dimensions which opens the ROI manager, too.

Then measure all ROIâ€™s with the action â€śMeasureâ€ť (select your required measurements, e.g., mean in the â€śSet Measurementsâ€ť action in the Analyze menu).

A result table will be shown, e.g., with the measured ROI mean value for each selected rectangle (ROI)

``````x = 0; //the x-start coordinate
y = 0; //the y-start coordinate
width = 4; //the width of the rectangle
height = 4; //the height of the rectangle
spacing = 0; //spacing between the rectangles
numRow = 63; //how many rows
numCol = 64; //how many columns

/*Create the selections and add them to the ROI Manager!*/
for (i = 0; i < numRow; i++) {
for (j = 0; j < numCol; j++) {
xOffset = j * (width + spacing);
yOffset = i * (height + spacing);
/*Create a rectangular selection!*/
makeRectangle(x + xOffset, y + yOffset, width, height);
/*Add the selection to the ROI Manager!*/

}
}
/*Show all selections in the image with (mouse) selectable labels!*/
roiManager("Show All with labels");
``````

The ROI with the highest mean contains the most black pixels.

If the image is big of course the first method would be more performant and more performant if you use Java.

Does it have to be a square? Are these â€śslidingâ€ť or "non-overlapping windows?
I wonder if running a Mean filter of the desired radius and then finding the minimum (e.g. via the histogram) would tell you where are the centres of regions with most black pixels.

Thank you so much! I have tried this and it worked nicely to place the grid. But i was wondering if i can have 1 box/selection of say 40x40 and it scan the image for the area i need. The way above works but It will have atmost 4 boxes containing the area of interest for me (the black area) on the 4 corners of the square.

can you help me with this? I have a shape (see attachment) i would like to find a way to find the max diameter (horiztonal and diameter), I cant use a bounded rectangle since the diameters have to be straight lines within the shape not the overall max/min. But instead i need it to find the longest vertical and horiztonal line that can be placed in the shape. thanks!!

There are several plugin available to measure shapes.

If this is the original image you can threshold the image (or convert to a binary) and then use the ImageJ â€śAnalyze Particlesâ€ť function to measure the shape (enable the include holes section to measure the polygon as one area).

@gabriel which answered you already has plugins available to measure a hughe amount of shape parameters, see:

http://www.mecourse.com/landinig/software/software.html

I can also recommend the @biovoxxel toolbox which you find here with a nice description of shape parameters:

In addition the MorphoLibJ suite of @iarganda and Legland:

1 Like

Itâ€™s very hard to understand your description what you are really trying to do without an example image.

However you can of course iterate over all pixels in a 40px window and then summarize the window with the getPixel method to find the darkest.