Feature Extraction

Finding Edges in Images

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:

  1. Define an intensity image in the MATLAB® workspace. To read in an intensity image from a PNG file, at the MATLAB command prompt, type

    I= imread('rice.png'); 
    

    I is a 256-by-256 matrix of 8-bit unsigned integers.

  2. To view the image this matrix represents, at the MATLAB command prompt, type

    imshow(I)
    

  3. Create a new Simulink® model, and add to it the blocks shown in the following table.

    Block

    Library

    Quantity

    Image From Workspace

    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

    3

    Subtract

    Simulink > Math Operations

    2

    Divide

    Simulink > Math Operations

    2

  4. 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.

  5. Use the Image From Workspace block to import your image from the MATLAB workspace. Set the block parameters as follows:

  6. Use the Edge Detection block to find the edges in the image. Set the block parameters as follows:

    The Edge Detection block convolves the input matrix with the Sobel kernel to calculate 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 it performs a thresholding operation on these gradient components to find the binary image, 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.

  7. View the binary image using the Video Viewer block. Accept the default parameters.

    The matrices output from the Gv and Gh ports of the Edge Detection block are composed of double-precision floating-point values. You must scale these matrix values between 0 in 1 to display them using the Video Viewer blocks.

  8. Use the Minimum blocks to find the minimum value of Gv and Gh matrices. Set the Mode parameters to Value.

  9. 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.

  10. Use the Maximum blocks to find the maximum value of the new Gv and Gh matrices. Set the Mode parameters to Value.

  11. 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.

  12. View the gradient components of the image using the Video Viewer1 and Video Viewer2 blocks. Accept the default parameters.

  13. Connect the blocks as shown in the following figure.

  14. Set the configuration parameters. Open the Configuration dialog box by selecting Configuration Parameters from the Simulation menu. Set the parameters as follows:

  15. Run your model.

    The Video Viewer window displays the edges of the rice grains in white and the background in black. To view the image at its true size, right-click the window and select Set Display To True Size.

    The Video Viewer1 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 following image is shown at its true size.

    The Video Viewer2 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. The following image is shown at its true size.

  16. Double-click the Edge Detection block and clear the Edge thinning check box.

  17. Run your model again.

    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 in Images

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.

  1. Define an intensity image in the MATLAB workspace. At the MATLAB command prompt, type

    I= imread('circuit.tif'); 
    

    I is a 280-by-272 matrix of 8-bit unsigned integers.

  2. To view the image, at the MATLAB command prompt, type

    imshow(I)
    

  3. Create a new Simulink model, and add to it the blocks shown in the following table.

    Block

    Library

    Quantity

    Image From Workspace

    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

    Selector

    Simulink > Signal Routing

    2

    Variable Selector

    Signal Processing Blockset > Signal Management > Indexing

    2

    Terminator

    Simulink > Sinks

    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

  4. 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.

  5. Use the Image From Workspace block to import your image from the MATLAB workspace. Set the Value parameter to I.

  6. 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.

  7. Use the Video Viewer block to display the edges found by the Edge Detection block. Accept the default parameters.

  8. 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:

  9. Use the Find Local Maxima block to find the location of the maximum value in the Hough matrix. Set the block parameters as follows:

  10. 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:

    Set the Selector1 block parameters as follows:

  11. 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:

  12. 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.

  13. Use the Draw Shapes block to draw a white line over the longest line on the original image. Set the block parameters as follows:

  14. Use the Video Viewer block to display the original image with a white line superimposed over the longest line in the image. Accept the default parameters.

  15. Connect the blocks as shown in the following figure.

  16. Set the configuration parameters. Open the Configuration dialog box by selecting Configuration Parameters from the Simulation menu. Set the parameters as follows:

  17. Run your model.

    The Video Viewer window displays the edges found in the original image in white and the background in black. To view the image at its true size, right-click the window and select Set Display To True Size.

    The Video Viewer1 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.

Measuring an Angle Between Lines

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.

  1. 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

    Submatrix

    Signal Processing Blockset > Math Functions > Matrices and Linear Algebra > Matrix Operations

    4

    Terminator

    Simulink > Sinks

    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

    Selector

    Simulink > Signal Routing

    4

    Hough Lines

    Video and Image Processing Blockset > Transforms

    1

    Embedded MATLAB Function

    Simulink > User-Defined Functions

    1

    Draw Shapes

    Video and Image Processing Blockset > Text & Graphics

    1

    Display

    Simulink > Sinks

    1

    Video Viewer

    Video and Image Processing Blockset > Sinks

    3

  2. Position the blocks as shown in the following figure.

  3. Use the Image From File block to import an image into the Simulink model. Set the parameters as follows:

  4. 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.

  5. 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:

    The Selector1 block separates the Cb component from the entire signal. Set its block parameters as follows:

  6. 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:

  7. 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.

  8. 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.

  9. 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:

    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.

  10. 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:

    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.

  11. 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:

    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.

  12. 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:

    You set the Index mode to Zero-based because the Find Local Maxima block outputs zero-based indices at the Idx port.

  13. 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.

  14. Use the Draw Shapes block to draw the lines on the luminance portion of the ROI. Set the parameters as follows:

  15. 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;  
    end
  16. Use the Display block to view the angle between the two lines. Accept the default parameters.

  17. Use the Video Viewer blocks to view the original image, the ROI, and the annotated ROI. Accept the default parameters.

  18. Connect the blocks as shown in the following figure.

  19. Set the configuration parameters. Open the Configuration dialog box by selecting Configuration Parameters from the Simulation menu. Set the parameters as follows:

  20. 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 54, 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.

  


 © 1984-2008- The MathWorks, Inc.    -   Site Help   -   Patents   -   Trademarks   -   Privacy Policy   -   Preventing Piracy   -   RSS