Edge detection finds the places in an image where brightness changes sharply. These places usually mark the boundary of an object, a texture change or a shadow. It is one of the most common first steps in image processing, and MATLAB makes it easy to try. The video below shows the process of detecting the edges of a picture in MATLAB, and the sections after it explain the idea and the steps in writing.
What edge detection does
A digital image is a grid of pixel values. In a grayscale image, each value says how bright that pixel is. An edge is a spot where the value jumps from dark to light, or the other way round, over a short distance.
Most edge detectors measure this change with a gradient, which is the rate at which brightness changes from one pixel to the next. Where the gradient is large, the detector marks an edge. The result is usually a black-and-white image in which white pixels are edges and black pixels are everything else.
Edge maps are useful for several tasks:
- Finding the outline of an object before measuring or counting it.
- Simplifying an image before further analysis.
- Locating lines, borders and shapes in scanned documents or photos.
- Preparing images for segmentation or feature extraction.
What you need
You need MATLAB with the Image Processing Toolbox, because the edge function comes from that toolbox. You also need a picture to work with. The example files linked above the video can be downloaded, or you can use any JPEG or PNG image of your own.
Steps to detect the edges of a picture in MATLAB
1. Read the image
Load the picture into the workspace with imread. Give it the file name, including the extension, and store the result in a variable. If the file is not in the current folder, give the full path.
2. Convert it to grayscale
The edge function works on grayscale or binary images, not on color images. If your picture is a color (RGB) image, convert it with rgb2gray first. If it is already grayscale, skip this step.
3. Run the edge function
Call edge on the grayscale image and name the method you want. For example, you can ask for the Sobel method or the Canny method. The function returns a logical image of the same size, where edge pixels are 1 and all other pixels are 0.
4. Display the result
Show the original and the edge map with imshow. Placing them side by side, for example with subplot, makes it easier to judge whether the edges match what you see in the picture.
Choosing an edge detection method
The edge function supports several methods. They all find changes in brightness, but they behave differently on real photos.
- Sobel: uses a small filter to estimate the gradient. It is simple and fast, and it works well on clear outlines.
- Prewitt: similar to Sobel, but with slightly different filter weights. Results are often close to Sobel.
- Roberts: uses a very small filter and responds to fine detail, but it is more sensitive to noise.
- Laplacian of Gaussian (LoG): smooths the image first, then looks for places where the second derivative crosses zero. It tends to produce closed contours.
- Canny: smooths the image, finds strong and weak edges, and keeps weak ones only when they connect to strong ones. It usually gives thin, clean lines and is a common first choice.
If you are unsure where to start, try Canny and Sobel on the same image and compare them. The best method depends on the picture, so it is worth testing more than one.
Adjusting the threshold
Most methods use a threshold that decides how strong a change must be to count as an edge. A low threshold finds more edges, including faint ones and noise. A high threshold keeps only the strongest edges and may miss real detail.
If you do not give a value, MATLAB picks one automatically. That is a good starting point. If the result looks too busy or too sparse, pass your own threshold and change it a little at a time until the edge map looks right for your purpose.
Reducing noise before detecting edges
Noise and fine texture can produce many false edges, because each tiny change in brightness looks like a boundary. Two things can help:
- Smooth the image slightly before detection, for example with a Gaussian filter such as imgaussfilt.
- Use a method that smooths internally, such as Canny or LoG, and adjust its smoothing setting.
Smoothing too much will blur real edges and may remove thin lines, so keep it modest.
Common problems
- An error about the image type: you probably passed a color image. Convert it to grayscale first.
- A mostly black result: the threshold may be too high, or the picture may have very low contrast.
- A cluttered result: the threshold may be too low, or the image may need smoothing.
- Broken outlines: try Canny, or lower the threshold slightly.
Where to go next
Once you can produce a clean edge map, you can use it for tasks such as finding shapes, measuring objects or separating a subject from its background. For a written version of this topic, see our guide on detecting edges of a picture in MATLAB. You can also use the MORE EXAMPLES link above to browse other MATLAB image processing examples.
FAQ
Which MATLAB function detects edges?
The edge function in the Image Processing Toolbox. You pass it a grayscale image and, optionally, the method name and a threshold.
Which edge detection method is best?
There is no single best method. Canny often gives thin, clean edges, while Sobel and Prewitt are simpler and fast. Test a few on your own image and pick the one that suits your goal.
Do I need to convert the image to grayscale?
Yes, for a color image. Use rgb2gray before calling edge. A picture that is already grayscale does not need conversion.
Why does my edge map show too many or too few edges?
This is usually down to the threshold or noise in the image. Raise the threshold or smooth the image to remove clutter, and lower the threshold to reveal fainter edges.