Perform Corner Detection by Using Neighborhood Processing Subsystem Blocks
This example shows how to detect corners in an image by using Neighborhood Processing Subsystem blocks. Use corner detection to identify features and objects in an image.
Inspect Model
1. Open the model.
mdl = 'CornerDetectionNeighborhoodExample';
open_system(mdl);
The model performs Harris corner detection. Harris corner detection calculates a value for each pixel. The value computes the rate of change of brightness in multiple directions around a pixel and reflects whether the pixel represents an edge, corner, or flat region in the image.
A flat region has low rates of change in all directions and yields an value near zero.
An edge has a high rate of change in only one direction and yields a negative value.
A corner has high rates of change in all directions and yields a positive value.
The model marks any pixel with a positive values as a corner, and uses two Video Viewer blocks from Computer Vision Toolbox to display the image with and without the corner markings.
2. Return to the model root and open the Compute Gradients
subsystem.
The Ix
and Iy
Neighborhood Processing Subsystem blocks compute the gradients of brightness with respect to the horizontal and vertical directions respectively, and . The Ixx
, Iyy
, and Ixy
Neighborhood Processing Subsystem blocks each contain a Product block and compute the products of the brightness gradients, , , and .
3. Return to the model root and open the Corner Strength Function
subsystem.
The subsystem computes for each pixel. Consider the change function :
.
For a neighborhood , calculates the weighted sum of squared differences between the brightness in the neighborhood, , and the brightness in a neighborhood of the same size shifted by , . Applying Taylor expansion yields this approximation:
.
The Sxx
, Syy
, and Sxy
Neighborhood Processing Subsystem blocks each contain a Sum of Elements block and compute the sums of the brightness gradient products , , and .
Consider the matrix :
.
Computing the eigenvalues of yields the rates of change in brightness around the neighborhood . Computing eigenvalues directly is computationally expensive, so the subsystem calculates the determinant and trace of , which relate to the eigenvalues and in these ways:
4. Open the det
subsystem to see how the subsystem calculates the determinant of , .
5. Return to the Corner Strength Function
subsystem and open the trace
subsystem to see how the subsystem calculates the trace of , .
6. Return to the Corner Strength Function
subsystem and open the harris
subsystem to see how the subsystem calculates .
The harris
subsystem calculates using this equation:
.
7. Return to the model root and open the Check Exceeds Threshold
subsystem.
The subsystem uses a Switch block to identify pixels with positive values, which represent corners. The neighborhood subsystem returns the value 0.5
for these pixels, which creates a moderate shade of gray in the output image.
8. Return to the model root and open the Overlay Original Image
subsystem.
The subsystem uses a Switch block to overlay the corner markings and the original image.
Simulate and View Results
Simulate the model.
simout = evalc('sim(mdl)');
The Before
Video Viewer block displays the original checkerboard image.
The After
Video Viewer block displays the checkerboard image with gray dots marking the corners.
The upper left and lower right corners do not have markers because the Neighborhood Processing Subsystem blocks in the model use the Constant
padding option with a padding value of 0
. The Padding option block parameter controls how the Neighborhood Processing Subsystem block treats pixels in neighborhoods that extend beyond the input image. The parameters in the model configure the Neighborhood Processing Subsystem blocks to use the value 0
outside the input image, which treats the image as if it is surrounded by black pixels. Thus, the model recognizes only the white and gray corners of the image as corners.
For more information about the Padding option block parameter, see Neighborhood.
See Also
Neighborhood Processing Subsystem
Related Topics
- Calculate Optical Flow by Using Neighborhood Processing Subsystem Blocks
- Perform Edge Detection by Using a Neighborhood Processing Subsystem Block
- Perform Fog Rectification by Using Neighborhood Processing Subsystem Blocks
- Convert RGB Image to Grayscale by Using a Pixel Processing Subsystem Block
- Generate HDL Code from Frame-Based Models by Using Neighborhood Modeling Methods (HDL Coder)
- Use Neighborhood, Reduction, and Iterator Patterns with a Frame-Based Model or Function for HDL Code Generation (HDL Coder)