Image Edge Detection Script Help

fiji
imagej

#1

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!


#2

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

Did you try Process > Find Edges?


Quick ROI Question
#3

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.

Regards

Herbie


#4

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!

Image


#5

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


#6

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.

Regards

Herbie


#7

I understand that you are looking for a Python solution but perhaps you try the following ImageJ-macro that should do what you want:

requires( "1.52k" );
open("");
img=File.nameWithoutExtension;
run("Select All");
a=getProfile();
close();
Array.getStatistics(a,mi,mx);
mx*=0.5;
for (i=0; i<a.length; i++) {if (a[i]<mx) a[i]=0;}
b=Array.findMaxima(a,0);
Array.getStatistics(b,mi,mx);
print("Left edge of \""+img+"\" at x = "+mi+" pixels;");
print("Right edge of \""+img+"\" at x = "+mx+" pixels;");
exit();

Paste the above macro code to an empty macro window (Plugins >> New >> Macro) and run it. It will ask you for the image to analyze.

Please report if it works for you.

Regards

Herbie


#8

Hi,

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.


#9

https://drive.google.com/file/d/13r9P6FvHBXXaTpyWy8DfJRaAq30eJHQQ/view?usp=sharing[Splotchy Image]
Result Output

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


#10

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

Herbie


#11

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.


#12

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.

Regards

Herbie


#13

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!


#14

Hi,

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!


#15

Good day,

here is the vertical projection of sample image “6 Coating Lanes Splotchy”:

Although it can be analyzed according to your description, I’d recommend to use the vertical projection of its bandpass-filtered version:

Please note that the position of the “bands” is defined by the crossings of the base-line, i.e. between the positive and the negative peak for a falling slope and between the negative and the positive peak for a rising slope. The positions of the narrow dark lines are given by single downward peaks. For an easy analysis I’d first subtract the mean from the plot, to set the baseline to about zero. Two carefully chosen thresholds will then ease the detection of the peak positions.

Good luck

Herbie


#16

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!


#17

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

Regards

Herbie