Image Edge Detection Script Help

Hi All,

I am completely new to ImageJ and image processing, and I am currently working on putting together an ImageJ script written in Python that asks the user to select an image and then finds some edges (based on user input) and report the results in a GUI such as a pop-up window. Essentially, an image is loaded into the script based on what the user selects, and then the script would detect the location of web edges in the image and report the location of those edges in a GUI. Are there already built in Macros/Commands I can use for this? The main goal is to find the left and right edge of the web (the location in some unit of length such as nm, m, cm) and report the locations in a pop-up window. Thank you very much!

If you share the script you have already, helping you gets a bit easier.

Did you try Process > Find Edges?

1 Like

Good day!

Without seeing a typical raw image in the original PNG- or TIF-format there is little chance that you will receive substantial help.



Thank you for the prompt response. Actually, I did create this script using Matrox, but now I want to create it in ImageJ as Matrox is extremely outdated and inefficient. I figured I could come up with some ideas on the best way to get started using this forum. How do I take in an image and use the Process > Find Edges macro to detect the edges and display it in a pop-up box similar to the one attached? I have also attached an example image. I am sorry for the overload of questions, I am just trying to figure out the best way to get started, for I am completely unfamiliar with ImageJ. Thank you!


I have attached the images (the Image hyperlink is different from the one in my comment above). Thank you very much!

Good day and thanks for the sample image!

Not perfectly sure what you regard as the edges of interest in your sample image. I’d start using and analyzing the vertical image projection because there appears being little variation in y-direction.

Here is the projection of your image

and it is rather easy to write e.g. an ImageJ-macro that outputs the x-positions of the sharp peaks that indicate what I would regard as the edges in question.



1 Like


Thank you so much for all your help. I appreciate it immensely!

I ran the script and it worked as you stated, but I am curious about the following things:

How would I modify it to account for edges that output the x-positions of the sharp peaks? (which is what you mentioned above)
Is there also a way to modify it from pixels to units of measurement such as inches, centimeters, etc?
Can I access the various results such as the edge list and edge locations in a spot in ImageJ?

A side question that I have is about finding stripes and stripe widths and reporting them to the user. Essentially this is what I would want to happen:

i. Ask the user to enter the web width – this is used to crop the image down to just the web section
ii. Apply a 50 point vertical smooth 30 times (30 iterations) – Kernel file = Filter_DW_50_30Iterations.flt  This gets rid of any noise in the image
iii. Ask the user for the number of coating strips
iv. Sub-sample the image so that auto thresholding can be used
v. Adjust the auto-threshold result to ensure that the edge is maintained
vi. Apply a threshold and does blobbing (defect extraction at a set threshold) to find the edge of each coating strip
vii. Handles all the goofy cases where the sub-sample might have 0, 1, N edges and it also handles the camera overlap (example images I have are 16484 x 2048 pixels – the image is actually two 8192 cameras stitched together)
viii. Calculates, reports, and saves all the stripe and gap information

Regarding blobbing, I am wondering:
How do you find bright or dark features in ImageJ?
How do you get access to the blob results?
How do you write results to an output file?

How do I draw a line profile and access the data?

Once again, thank you very much for your help. I will greatly appreciate it if you could answer any of the questions. The overall goal is to create a script that finds the stripes and stripe widths in an image and report it to a user.[Splotchy Image]
Result Output

Here is an example of an output and a splotchy image. Thank you!

Good day!

I am curious about the following things:

How would I modify it to account for edges that output the x-positions of the sharp peaks? (which is what you mentioned above)

I don’t understand, because my macro does just this.

Is there also a way to modify it from pixels to units of measurement such as inches, centimeters, etc?

Yes, a scale setting can be used.

Can I access the various results such as the edge list and edge locations in a spot in ImageJ?

Yes, instead of writing the results to the log-window, you can write them to an ImageJ-results table.

Essentially this is what I would want to happen:

You received considerable help and advice and this is what the Forum is for. The Forum is not for ordering solutions or code. The provided code provides a solution for your initial problem and should give you an idea of how to solve related tasks. Now it’s your turn.

Please get acquainted with ImageJ by studying the User Guide.

Then learn the basics of macro coding and get acquainted with the built-in macro functions.

Macro coding is really easy and after having studied the documents, you should be able to understand each line of code of my macro and you should be able to write the code necessary for the desired operations.

Sub-sample the image so that auto thresholding can be used

This doesn’t make sense. Sampling and auto-thresholding are unrelated.

Good luck


1 Like

Thanks for your response and the links you provided. I assumed that I would be able to pose questions like this and receive specific examples of code, seeing as you provided code in the previous reply. Best wishes.

as you provided code in the previous reply

The code was meant to get you started.
Do you understand how it works?

Sub-sample the image so that auto thresholding can be used

Do you understand why “sampling and auto-thresholding are unrelated”?

Please start creating your own solutions based on the given hints.



Hi Herbie,

Thank you for all your help so far. I looked through all the links you provided, and I found the built-in Macro Functions one to be particularly useful. I looked through all the functions you used to understand your code. Additionally, I see the difference between sub-sampling and auto-thresholding and apologize for my error. I am curious about one thing in particular. How can I modify the Array.findMaxima to get all the peaks? The macro that you provided finds the left and right edge, but if I want to obtain all the sharp peaks of the x-positions shouldn’t the findMaxima function do that? I see that it says “Returns an array holding the peak positions (sorted with descending strength)” but I am struggling to figure out a way of distinguishing between the light and dark regions.

For example, for the macro that you gave, I wrote:

for(i=0; i<b.length;i++)
print (b[i]);

which outputs a numerous amount of pixel locations, but I am just trying to output the edge pixel location when there is a difference between a light and dark region.

Also, is there a way I can have a user draw a rectangle on an image that would specify an ROI, and then based on the ROI I can output the edge pixel locations? Thank you very much, and please let me know if I need to clarify anything!


I see that I can draw a rectangle on a popped up image and run the process “Find Edges” and then anything I hover over it displays the pixel locations of the x and y coordinates. Can I manually click on each of the edges I am interested in and then output those x coordinates in a text file? I see that it already displays it in the fiji tool bar when I hover over it, but I want to send those x values to a text file. Thanks!

Hi Herbie,

Thanks for the response. I am not sure if you saw my other comment, but how can I have a user draw an ROI (like draw a rectangle in a certain region of one of those sample images I uploaded) and then find the edges based on that ROI? I would then want to output those x coordinates of the edges into a text file. Thanks again!

Please study the ImageJ User Guide. Your question appears very basic!



When you say “have a user draw an ROI” do you mean: ask them to do it, then wait for them to do it, before continuing with the script? If so, I recommend using the “wait for user” functionality.

From ImageJ macro, you can use the waitForUser macro function:

waitForUser(title, msg);

See the WaitForUserDemo.txt example macro for more about that.

From Python script, you can create a WaitForUserDialog directly. Here is an example:

#@ UIService ui

from ij import IJ
from ij.gui import WaitForUserDialog

title = "User Interaction Required"
text = "Please draw a rectangular ROI, then click OK."
waitForUserDialog = WaitForUserDialog(title, text)
if not waitForUserDialog.escPressed():
	bounds = IJ.getImage().getRoi().getBounds()
	ui.showDialog("ROI bounds = " + str(bounds), "Result")

Does that help? If something is still not clear, please ask!

1 Like