Introduction: CNC Edge Detection for Plotters
Edge detectors enable you to determine the outlines within a photo.
This instructable explains how edge detectors work and how to convert your edges into gcode that is suitable for use with a plotter.
Processing 3 code is included for plotting Sobel outlines and Canny outlines. The gcode from these two programs is compatible with the drum plotter described in my instructable https://www.instructables.com/id/CNC-Drum-Plotter/
Both of the above programs use recursion to trace the edges. The blue in each of the above images shows this recursion in action when a single line is scanned.
Step 1: Simple Edge Detector
Edge detection is remarkably simple.
In the above diagram we compare two adjacent pixels as we move them across the image.
The output from this two-cell kernel is always zero unless the cell straddles an edge.
As shown this two-cell kernel only detects vertical edges. A second detector is needed to detect any horizontal edges.
Step 2: Other Operators
Roberts cross detector
This detector is similar to the simple edge detector except that the diagonal pixels from each kernel are compared.
Notice how lines through the zeros are at 90 degrees to each other.
As before the output from each kernel is zero unless an edge is detected.
The absolute brightness equals the sqrt(kernel12 +kernel22)
Due to its small kernel size this filter is very susceptible to noise. The noise may be controlled by applying a gaussian blur to the image prior to processing.
The Sobel edge detector uses two 3x3 matrix kernels, one with the zeros aligned along the vertical axis, and one with the zeros aligned along the horizontal axis.
The matrix shape and the arrangement of the numbers within each kernel have some complex mathematical properties. Suffice to say that the numbers within each kernel add up to zero which means that the output will be zero whenever all pixels are the same. For more information see https://en.wikipedia.org/wiki/Sobel_operator
As with the Roberts cross detector the absolute brightness of the center pixel equals the sqrt(kernel12 + kernel22)
Step 3: Canny Edge Detection
The Canny edge detector builds upon the above edge detector kernels. Perhaps the most popular are the Sobel kernels described above.
The above image depicts a 3x3 Sobel kernel being moved across a grayscale image in which there is a positive gradient. At each location the pixels beneath each cell are multiplied by the number in the cell then added together before being parsed to the center pixel (which must be on a different sheet of paper).
If we examine the the output we find that we get a positive response ... the leading edge has a positive slope ... the trailing edge has a negative slope. This curve is labelled "first derivative".
If we now run a (different) kernel over this first derivative output we will get two responses ... a positive response as we move over the leading edge ... and a negative response as we move over the trailing edge. This curve is labelled "second derivative".
The center of the original gradient is where a line drawn from each of these two peaks crosses the zero axis as shown by the blue line. Magic !
In addition to this, the Canny edge detector is able to determine whether an edge is orientated N, S, NE, NW which allows it to determine whether an edge is connected to another edge.
Two thresholds are used by the Canny edge detector.
- edges above the top threshold are displayed
- edges below the lower threshold are rejected
- "connected" edges between the the two thresholds are displayed
For further information see https://en.wikipedia.org/wiki/Canny_edge_detector
From a plotters perspective the most significant difference between Sobel single threshold detection and Canny two threshold detection is that each Canny outline is only one pixel wide as shown in the opening photo.
Step 4: Processing 3 Code
The attached files are similar except that I have used an "openCV library" for the more complex Canny code.
The sobel_outline.pde code shows how to to process each of the two Sobel kernels.
Step 5: Workflow
Create the gcode
Download Processing 3 from https://processing.org/
Copy the contents of the file "sobel_outline.pde" into a new sketch and save the sketch as "sobel_outline".
Locate the folder in which "sobel_outline" resides ... for windows it should be in your documents folder under "processing|sobel_outline".
Create a "data" folder within the "sobel_outline" folder and place your "image.jpg" there.
Click the "run" button and wait for the image to turn blue.
An "outline.ngc" file should now be in your "sobel_outline" folder.
Send the gcode to your plotter
Install my "CNC Gcode Sender" program from https://www.instructables.com/id/CNC-Gcode-Sender/
Copy the above "outline.ngc" file to the "processing3_terminal" folder.
Switch on your (drum) plotter ... see https://www.instructables.com/id/CNC-Drum-Plotter/
Open "processing3_terminal.pde" and click the run tab.
Wait for the plotter to connect then (assuming your pen and scale factors are correct) right-click the "window" and enter "outline.ngc". Your plotter should start plotting.
The method for plotting the "canny_outline" is essentially the same except that you need to install the "openCV" library. Instructions for doing this are given in the file "header".
Click here to view my other instructables.
We have a be nice policy.
Please be positive and constructive.