Chapter 01-1: Edge Detection using the straight line selection


Chapter 01-1 descripes a simple detection of an edge in a image. The first step is to open an image. Select the straight line selection in imagej and draw a line over a relevant edge in the image. Basically you could use any picture format that is working with imagej, but in industrial image processing it is common to use the bmp format. Almost any camera is supporting the bmp format and there is no loss of information in the image due to compression. Befor starting the process of the edge detection you should convert your image to a 8-bit grayscale image. The three channel colour image is not needed for edge detection. 

Step 01: The start-up image

chapter01-1_step1.jpg


This is an example of the startup image. I simply scanned a sd-card and saved it as a 8-bit bmp-image. Basically you can use any image, but for elementary understanding I suggest an image with a sharp and clear edge.

Step 02: “Straight line selection” and the “Plot Profile” command

chapter01-1_step2.jpg


The image on the left side shows the "Straight-Line" selection. The plot image on the rightside shows the corresponding plot of the pixel values. Like you can see it is not very hard to localize the the edge of the sdcard. The most distinct edge is supposed to be where the difference of the pixelvalue has his maximum. So the next step would be to compute the difference of each pixelvalue of the selection and his neighbour pixels to get the maximum difference in the selection.

Step 03: Compute the maximum difference

Some simple code sample for the imagej macro language. Just copy the code into a new imagej text window and run the macro.


// This is an example for a simple edge detection. It works only with a --horizontal-- straight line selection.

selection = selectionType();
if (selection!=5) exit("This macro requires a straight line selection!");

max = 0;  // maximum of the pixel value difference --> bright to dark edge
maxloc = 0;  // location of the first maximum edge 
min =0;  // minimum of the pixel value difference --> dark to bright edge
minloc = 0; // location of the first minimum edge

profile = getProfile();

for (i = 0; i < profile.length-1; i++)
 {
 print("pixel value at i and i+1:   ",profile[i],"   ",profile[i+1], "    i =  ", i);
 difference = profile[i]-profile[i+1];
 print("difference at i:   ",difference);
 if (difference>max)  {max = difference;
    maxloc = i;
    };
 if (difference<min)  {
    min = difference;
    minloc = i;
    };
 print("max:   ",max,"   min:   ",min);
 }

getSelectionCoordinates(xCoordinates, yCoordinates);
cornerpointx = xCoordinates[0] + maxloc;
cornerpointy = yCoordinates[0];
makePoint(cornerpointx, cornerpointy);
print("cornerpoint x:   ",cornerpointx,"   cornerpoint y:   ",cornerpointy);


Step 04: Enjoy the results

chapter01-1_step3.jpg


At the left side of the sd-card you can see the detected edge. The Log window shows the x-y coordinates of the detected cornerpoint. It also shows the maximum difference for a bright-to-dark corner and the maximum difference for a dark-to-bright corner. If you want to detect a dark-to-bright corner you can easy adjust the codesample. A good thing to dig deeper into programming with imagej is to play around with the code. This is good practice and makes the code more comprehendible.