| Products & Services | Solutions | Academia | Support | User Community | Company |
| Download Product Updates | | | Get Pricing | | | Trial Software |
| Documentation → Video and Image Processing Blockset |
| Contents | Index |
| Learn more about Video and Image Processing Blockset |
| On this page… |
|---|
You can use the Edge Detection block to find the edges of objects in an image. This block finds the pixel locations where the magnitude of the gradient of intensity is larger than a threshold value. These locations typically occur at the boundaries of objects. In this section, you use the Edge Detection block to find the edges of rice grains in an intensity image:
Create a Simulink model, and add the blocks shown in the following table.
Block | Library | Quantity |
|---|---|---|
Image From File | Video and Image Processing Blockset > Sources | 1 |
Edge Detection | Video and Image Processing Blockset > Analysis & Enhancement | 1 |
Minimum | Video and Image Processing Blockset > Statistics | 2 |
Maximum | Video and Image Processing Blockset > Statistics | 2 |
Video Viewer | Video and Image Processing Blockset > Sinks | 4 |
Subtract | Simulink > Math Operations | 2 |
Divide | Simulink > Math Operations | 2 |
Place the blocks so that your model resembles the following figure.

You are now ready to set your block parameters by double-clicking the blocks, modifying the block parameter values, and clicking OK.
Use the Image From File block to import your image. Set the parameters as follows:
File name to rice.png.
Output data type to single.
Use the Edge Detection block to find the edges in the image. Set the block parameters as follows:
Output type = Binary image and gradient components
Select the Edge thinning check box.

The Edge Detection block convolves the input matrix with the Sobel kernel. This calculates the gradient components of the image that correspond to the horizontal and vertical edge responses. The block outputs these components at the Gh and Gv ports, respectively. Then the block performs a thresholding operation on the gradient components to find the binary image. The binary image is a matrix filled with 1s and 0s. The nonzero elements of this matrix correspond to the edge pixels and the zero elements correspond to the background pixels. The block outputs the binary image at the Edge port.
View the original image using the Video Viewer block and the binary image using the Video Viewer1 block. Accept the default parameters for both viewers.
The matrix values at the Gv and Gh output ports from of the Edge Detection block are double-precision floating-point. These matrix values need to be scaled between 0 and 1 in order to display them using the Video Viewer blocks. This is done with the Statistics and Math Operation blocks.
Use the Minimum blocks to find the minimum value of Gv and Gh matrices. Set the Mode parameters to Value.
Use the Subtract blocks to subtract the minimum values from each element of the Gv and Gh matrices. This process ensures that the minimum value of these matrices is 0. Accept the default parameters.
Use the Maximum blocks to find the maximum value of the new Gv and Gh matrices. Set the Mode parameters to Value.
Use the Divide blocks to divide each element of the Gv and Gh matrices by their maximum value. This normalization process ensures that these matrices range between 0 and 1.Accept the default parameters.
View the gradient components of the image using the Video Viewer1 and Video Viewer2 blocks. Accept the default parameters.
Connect the blocks as shown in the following figure.

Set the configuration parameters. Open the Configuration dialog box by selecting Configuration Parameters from the Simulation menu. Set the parameters as follows:
Solver pane, Stop time = 0
Solver pane, Type = Fixed-step
Solver pane, Solver = Discrete (no continuous states)
The Video Viewer window displays the original image. The Video Viewer1 window displays the edges of the rice grains in white and the background in black.


The Video Viewer2 window displays the intensity image of the vertical gradient components of the image. You can see that the vertical edges of the rice grains are darker and more well defined than the horizontal edges.

The Video Viewer3 window displays the intensity image of the horizontal gradient components of the image. In this image, the horizontal edges of the rice grains are more well defined.

Double-click the Edge Detection block and clear the Edge thinning check box.
Your model runs faster because the Edge Detection block is more efficient when you clear the Edge thinning check box. However, the edges of rice grains in the Video Viewer window are wider.

You have now used the Edge Detection block to find the object boundaries in an image. For more information on this block, see the Edge Detection block reference page in the Video and Image Processing Blockset Reference.
Finding lines within images enables you to detect, measure, and recognize objects. In this section, you use the Hough Transform, Find Local Maxima, and Hough Lines blocks to find the longest line in an image.
Create a Simulink model, and add the blocks shown in the following table.
Block | Library | Quantity |
|---|---|---|
Image From File | Video and Image Processing Blockset > Sources | 1 |
Edge Detection | Video and Image Processing Blockset > Analysis & Enhancement | 1 |
Hough Transform | Video and Image Processing Blockset > Transforms | 1 |
Find Local Maxima | Video and Image Processing Blockset > Statistics | 1 |
Hough Lines | Video and Image Processing Blockset > Transforms | 1 |
Draw Shapes | Video and Image Processing Blockset > Text & Graphics | 1 |
Video Viewer | Video and Image Processing Blockset > Sinks | 2 |
Variable Selector | Signal Processing Blockset > Signal Management > Indexing | 2 |
Selector | Simulink > Signal Routing | 2 |
Terminator | Simulink > Sinks | 1 |
Place the blocks so that your model resembles the following figure.

You are now ready to set your block parameters by double-clicking the blocks, modifying the block parameter values, and clicking OK.
Use the Image From File block to import your image. Set the File name parameter to circuit.tif.
Use the Edge Detection block to find the edges in the intensity image. This process improves the efficiency of the Hough Lines block as it reduces the image area over which the block searches for lines. The block also converts the image to a binary image, which is the required input for the Hough Transform block. Accept the default parameters.
Use the Video Viewer block to display the original image and the Video Viewer1 block to display the edges found by the Edge Detection block. Accept the default parameters.
Use the Hough Transform block to compute the Hough matrix by transforming the input image into the rho-theta parameter space. The block also outputs the rho and theta values associated with the Hough matrix. Set the block parameters as follows:
Theta resolution (radians) = pi/360
Select the Output theta and rho values check box.

Use the Find Local Maxima block to find the location of the maximum value in the Hough matrix. Set the block parameters as follows:
Maximum number of local maxima (N) = 1
Select the Input is Hough matrix spanning full theta range check box.

Use the Selector blocks to separate the indices of the rho and theta values, which are output at the Idx port, that are associated with the maximum value in the Hough matrix. Set the Selector block parameters as follows:
Number of input dimensions: 1
Index mode = Zero-based
Index Option = Index vector (dialog)
Index = 0
Input port size = 2
Set the Selector1 block parameters as follows:
Number of input dimensions: 1
Index mode = Zero-based
Index Option = Index vector (dialog)
Index = 1
Input port size = 2
Use the Variable Selector blocks to index into the rho and theta vectors and determine the rho and theta values that correspond to the longest line in the original image. Set the parameters of the Variable Selector blocks as follows:
Select = Columns
Index mode = Zero-based
Use the Hough Lines block to determine where the longest line intersects the edges of the original image. You use these coordinates to superimpose a white line on the original image. Set the Sine value computation method to Trigonometric function.
Use the Draw Shapes block to draw a white line over the longest line on the original image. Set the block parameters as follows:
Shape = Lines
Border color = White
Use the Video Viewer2 block to display the original image with a white line superimposed over the longest line in the image. Accept the default parameters.
Connect the blocks as shown in the following figure.

Set the configuration parameters. Open the Configuration dialog box by selecting Configuration Parameters from the Simulation menu. Set the parameters as follows:
Solver pane, Stop time = 0
Solver pane, Type = Fixed-step
Solver pane, Solver = Discrete (no continuous states)
The Video Viewer window displays the original image.

Video Viewer1 displays the edges found in the original image in white and the background in black.

The Video Viewer2 window displays the original image with a white line drawn over the longest line in the image.

You have now used the Hough Transform, Find Local Maxima, and Hough Lines blocks to find the longest line in an image. For more information on these blocks, see the Hough Transform, Find Local Maxima, and Hough Lines block reference pages in the Video and Image Processing Blockset Reference. For additional examples of the techniques used in this section, see the Lane Departure Warning System and Rotation Correction demos. You can open these demos by typing vipldws and viphough at the MATLAB command prompt.
The Hough Transform, Find Local Maxima, and Hough Lines blocks enable you to find lines in images. With the Draw Shapes block, you can annotate images. In the following example, you use these capabilities to draw lines on the edges of two beams and measure the angle between them.
Create a new Simulink model, and add to it the blocks shown in the following table.
Block | Library | Quantity |
|---|---|---|
Image From File | Video and Image Processing Blockset > Sources | 1 |
Color Space Conversion | Video and Image Processing Blockset > Conversions | 1 |
Edge Detection | Video and Image Processing Blockset > Analysis & Enhancement | 1 |
Hough Transform | Video and Image Processing Blockset > Transforms | 1 |
Find Local Maxima | Video and Image Processing Blockset > Statistics | 1 |
Draw Shapes | Video and Image Processing Blockset > Text & Graphics | 1 |
Video Viewer | Video and Image Processing Blockset > Sinks | 3 |
Hough Lines | Video and Image Processing Blockset > Transforms | 1 |
Submatrix | Signal Processing Blockset > Math Functions > Matrices and Linear Algebra > Matrix Operations | 4 |
Terminator | Simulink > Sinks | 1 |
Selector | Simulink > Signal Routing | 4 |
Embedded MATLAB Function | Simulink > User-Defined Functions | 1 |
Display | Simulink > Sinks | 1 |
Position the blocks as shown in the following figure.

Use the Image From File block to import an image into the Simulink model. Set the parameters as follows:
File name = gantrycrane.png
Sample time = 1
Use the Color Space Conversion block to convert the RGB image into the Y'CbCr color space. You perform this conversion to separate the luma information from the color information. Accept the default parameters.
Use the Selector and Selector1 blocks to separate the Y' (luminance) and Cb (chrominance) components from the main signal.
The Selector block separates the Y' component from the entire signal. Set its block parameters as follows:
Number of input dimensions = 3
Index mode = Zero-based
1
Index Option = Select all
2
Index Option = Select all
3
Index Option = Index vector (dialog)
Index = 1
The Selector1 block separates the Cb component from the entire signal. Set its block parameters as follows:
Number of input dimensions = 3
Index mode = Zero-based
1
Index Option = Select all
2
Index Option = Select all
3
Index Option = Index vector (dialog)
Index = 2
Use the Submatrix and Submatrix1 blocks to crop the Y' and Cb matrices to a particular region of interest (ROI). This ROI contains two beams that are at an angle to each other. Set the parameters as follows:
Starting row = Index
Starting row index = 66
Ending row = Index
Ending row index = 150
Starting column = Index
Starting column index = 325
Ending column = Index
Ending column index = 400
Use the Edge Detection block to find the edges in the Cb portion of the image. This block outputs a binary image. Set the Threshold scale factor parameter to 1.
Use the Hough Transform block to calculate the Hough matrix, which gives you an indication of the presence of lines in an image. Select the Output theta and rho values check box as shown in the following figure.
Note In step 11, you find the theta and rho values that correspond to the peaks in the Hough matrix. |

Use the Find Local Maxima block to find the peak values in the Hough matrix. These values represent potential lines in the input image. Set the parameters as follows:
Neighborhood size = [11 11]
Input is Hough matrix spanning full theta range = selected
Because you are expecting two lines, leave the Maximum number of local maxima (N) parameter set to 2, and connect the Count port to the Terminator block.
Use the Submatrix2 block to find the indices that correspond to the theta values of the two peak values in the Hough matrix. Set the parameters as follows:
Starting row = Index
Starting row index = 2
Ending row = Index
Ending row index = 2
The Idx port of the Find Local Maxima block outputs a matrix whose second row represents the zero-based indices of the theta values that correspond to the peaks in the Hough matrix. Now that you have these indices, you can use a Selector block to extract the corresponding theta values from the vector output of the Hough Transform block.
Use the Submatrix3 block to find the indices that correspond to the rho values of the two peak values in the Hough matrix. Set the parameters as follows:
Ending row = Index
Ending row index = 1
The Idx port of the Find Local Maxima block outputs a matrix whose first row represents the zero-based indices of the rho values that correspond to the peaks in the Hough matrix. Now that you have these indices, you can use a Selector block to extract the corresponding rho values from the vector output of the Hough Transform block.
Use the Selector2 and Selector3 blocks to find the theta and rho values that correspond to the peaks in the Hough matrix. These values, output by the Hough Transform block, are located at the indices output by the Submatrix2 and Submatrix3 blocks. Set both block parameters as follows:
Index mode = Zero-based
1
Index Option = Index vector (port)
Input port size = -1
You set the Index mode to Zero-based because the Find Local Maxima block outputs zero-based indices at the Idx port.
Use the Hough Lines block to find the Cartesian coordinates of lines that are described by rho and theta pairs. Set the Sine value computation method parameter to Trigonometric function.
Use the Draw Shapes block to draw the lines on the luminance portion of the ROI. Set the parameters as follows:
Shape = Lines
Border color = White
Use the Embedded MATLAB Function block to calculate the angle between the two lines. Copy and paste the following code into the block:
function angle = compute_angle(theta)
%Compute the angle value in degrees
angle = abs(theta(1)-theta(2))*180/pi;
%Always return an angle value less than 90 degrees
if (angle>90)
angle = 180-angle;
endUse the Display block to view the angle between the two lines. Accept the default parameters.
Use the Video Viewer blocks to view the original image, the ROI, and the annotated ROI. Accept the default parameters.
Connect the blocks as shown in the following figure.

Set the configuration parameters. Open the Configuration dialog box by selecting Configuration Parameters from the Simulation menu. Set the parameters as follows:
Solver pane, Stop time = 0
Solver pane, Type = Fixed-step
Solver pane, Solver = Discrete (no continuous states)
Run the model.
The Video Viewer window displays the original image.

The Video Viewer1 window displays the ROI where two beams intersect.

The Video Viewer2 window displays the ROI that has been annotated with two white lines.

The Display block shows a value of 58, which is the angle in degrees between the two lines on the annotated ROI.
You have now annotated an image with two lines and measured the angle between them. For additional information, see the Hough Transform, Find Local Maxima, Hough Lines, and Draw Shapes block reference pages in the Video and Image Processing Blockset Reference.
![]() | Analysis and Enhancement | Image Enhancement | ![]() |

Learn more about Simulink through this collection of videos, articles, technical literature and the Getting Started with Simulink Guide.
| © 1984-2009- The MathWorks, Inc. - Site Help - Patents - Trademarks - Privacy Policy - Preventing Piracy - RSS |