MATLAB exercise program (tracking of inner and outer boundaries of binary images)

Source: Internet
Author: User

All pixels at the inner boundary of the target are inside the target, and all pixels at the outer boundary of the target are not on the target, which is surrounded by the target.

There are two ways to calculate the inner and outer boundaries of a binary image. Therefore, there are four algorithms in total, but only one method is actually used for tracking.

 

First, internal border tracking:

The first method is not the tracking method. The step is to first corrode the original image, and then subtract the corrosive image from the original image to obtain the border.

The second method is the tracking method. The procedure is as follows:

1. traverse the image.

2. mark the first foreground pixel (I, j) that meets the pixel block ).

3. Search for the eight neighboring regions of this pixel in a counter-clockwise manner. If foreground pixels are found, update the coordinates (I, j) to (I ', j') and mark them.

4. Continue Step 1 until the pixel block marked for the first time is met again.

5. Continue step 1.

 

Then there is the outer border tracking:

The first method is similar to the first method for finding the inner boundary. First, expand the original image, and then subtract the original image from the expanded image.

The second method is not a trace method, but a tag algorithm. You just need to mark the non-foreground pixels around the foreground pixels in the image.

 

The effect is as follows:

Source image:

Inner Boundary:

Outer border:

The MATLAB program is as follows:

Inner Boundary:

Clear all; close all1_clc1_img1_imread('rice.png '); IMG = img> 128; imshow (IMG); [m n] = size (IMG); imgn = zeros (m, n ); % border Tagged Image ED = [-1-1; 0-1; 1-1; 1 0; 1 1; 0 1;-1 1;-1 0]; % search for I = 2: S-1 for J = 2: n-1 if IMG (I, j) = 1 & imgn (I, j) counterclockwise from the upper left corner of the pixel) = 0% current is untagged white pixel if sum (IMG (I-1: I + 1, J-1: J + 1 )))~ = 9% internal white pixels without marking II = I; % coordinate JJ = J used for intra-pixel search; imgn (I, j) = 2; % The boundary of the first tag in this pixel block. The first boundary pixel is 2 while imgn (II, JJ )~ = 2% indicates whether a circle is searched along the pixel block. For k = % ttmpi = II + ed (k, 1); % tmpj = JJ + ed (K, 2 ); if IMG (tmpi, tmpj) = 1 & imgn (tmpi, tmpj )~ = 2% a new boundary is found, and no circle is found. II = tmpi; % update the internal search coordinates and continue searching for JJ = tmpj; imgn (II, JJ) = 1; % boundary mark the pixel mark of the image. The common boundary is 1 break; end endfigure; imgn = imgn> = 1; imshow (imgn, []); % however, if the boundary of a binary image is really taken, it is usually the original image minus its corrosive image. Se = strel ('square ', 3); imgn = IMG-imerode (IMG, se); figure; imshow (imgn)

Outer border:

Clear all; close all1_clc1_img1_imread('rice.png '); IMG = img> 128; imshow (IMG); [m n] = size (IMG); imgn = zeros (m, n ); % border Tagged Image ED = [-1-1; 0-1; 1-1; 1 0; 1 1; 0 1;-1 1;-1 0]; % determine from the upper left corner of the pixel for I = 2: S-1 for J = 2: n-1 if IMG (I, j) = 1% if the current pixel is foreground pixel for k = II = I + ed (k, 1); JJ = J + ed (K, 2); If IMG (II, JJ) = 0% if the current pixel is surrounding the background, the boundary marks the corresponding pixel mark imgn (II, JJ) = 1; end endfigure; imshow (imgn, []); % But if the outer boundary of the binary image is really taken, it is usually the expansion of the original image minus the original image. Se = strel ('square ', 3 ); imgn = imdilate (IMG, SE)-IMG; figure; imshow (imgn)

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.