Measuring width in 'pixel rows' (new to ImageJ)

I’m pretty new to ImageJ so am not sure how to approach this problem, or really describe it for that matter.
Right now, I have a binary image of a particle. I want to measure the horizontal width of that particle at each y coordinate/pixel. For example, lets say this image is 200 pixels in height, I want have 200 particle width measurements. I’m not sure if that description makes sense, let me know if I need to clarify. Thanks everyone!

Kate

Here is a short macro script that should do what you want. Copy and paste this into a text file and save it. Then, use Plugins > Macros > Run... and select the text file. This will run the macro.

setTool("point");
waitForUser("Please select a pixel that belongs to the particle.\nThen click \"Okay\".");
getSelectionCoordinates(x, y);
target_value = getPixel(x[0], y[0]);
getDimensions(width, height, junk, junk, junk);
results = newArray(height);
left = 0;
right = 0;
left_found = false;
right_found = false;
value = 0;
for (i = 0; i < height; i++) {
    for (j = 0; (j < width) && !right_found; j++) {
        value = getPixel(j, i);
        if ((value == target_value) && !left_found) {
            left_found = true;
            left = j;
        } else if ((value != target_value) && left_found) {
            right_found = true;
            right = j;
        }
    }
    if (left_found) {
        if (!right_found) {
            right = width;
        }
        results[i] = right - left;
    } else {
        results[i] = 0;
    }
    left_found = false;
    right_found = false;
    value = 0;
}
Array.show(results);

Thank you so much Andrew!!

Good point! I had thought of that issue but decided I might have been overthinking it a bit. If you have a shape that isn’t completely convex, I wasn’t sure how you could define a single width. I guess in this case you are just summing them together.


Edit:

By the way, I am curious as to why you are putting requires( "1.52k" ); in all your macros. As far as I can tell, the one here and the other one here don’t use anything that are new to that version. Am I missing something or do you do that just to ensure that users are using a version you know works?

Am I missing something or do you do that just to ensure that users are using a version you know works?

Two reasons:

  1. Suggest to always use the most recent version.
  2. Ensure that users are using a version you know works.

Regards

Herbie

1 Like