Main Content

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 R for each pixel. The R 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 R value near zero.

  • An edge has a high rate of change in only one direction and yields a negative R value.

  • A corner has high rates of change in all directions and yields a positive R value.

The model marks any pixel with a positive R 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, Ix and Iy. The Ixx, Iyy, and Ixy Neighborhood Processing Subsystem blocks each contain a Product block and compute the products of the brightness gradients, Ix2, Iy2, and IxIy.

3. Return to the model root and open the Corner Strength Function subsystem.

The subsystem computes R for each pixel. Consider the change function E(u,v):

E(u,v)=x,yw(x,y)[I(x+u,y+v)-I(x,y)]2.

For a neighborhood w(x,y), E(u,v) calculates the weighted sum of squared differences between the brightness in the neighborhood, I(x,y), and the brightness in a neighborhood of the same size shifted by (u,v), I(x+u,y+v). Applying Taylor expansion yields this approximation:

E(u,v)[uv]([Ix2IxIyIxIyIy2])[uv].

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 Ix2, Iy2, and IxIy.

Consider the matrix M:

M=w(x,y)[Ix2IxIyIxIyIy2].

Computing the eigenvalues of M yields the rates of change in brightness around the neighborhood w(x,y). Computing eigenvalues directly is computationally expensive, so the subsystem calculates the determinant and trace of M, which relate to the eigenvalues λ1 and λ2 in these ways:

  • det(M)=λ1λ2

  • trace(M)=λ1+λ2

4. Open the det subsystem to see how the subsystem calculates the determinant of M, det(M).

5. Return to the Corner Strength Function subsystem and open the trace subsystem to see how the subsystem calculates the trace of M, trace(M).

6. Return to the Corner Strength Function subsystem and open the harris subsystem to see how the subsystem calculates R.

The harris subsystem calculates R using this equation:

R=det(M)-0.04×trace(M)2.

7. Return to the model root and open the Check Exceeds Threshold subsystem.

The subsystem uses a Switch block to identify pixels with positive R 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

Related Topics