What Is a Chain Code?

Chain code (also known as freeman code) is a method to describe the curve or boundary by the coordinates of the starting point of the curve and the direction code of the boundary point. It is often used to represent curves and areas in the fields of image processing, computer graphics, pattern recognition boundary.

Commonly used chain codes follow the center
according to
After using the chain code, the object can be described by using the starting point coordinates, the perimeter (the number of boundary points), the chain code, and the object number. Chain code is generally used when there are multiple objects in an image, and it is not applicable to a single object.
In the Matlab image processing toolbox, a special bwlabel () function is provided for connected branch labeling (non-chain code) of binary images [1] . The calling format is as follows: L = bwlabel (BW, n). This function returns an L matrix of the same size as the input binary image BW, containing category labels labeled for each connected region in BW. The values of these labels are 1, 2, n (the number of connected regions). The value of n is 4 or 8, which indicates whether the area is searched according to 4 or 8 connections. If the parameter is omitted, the default value is 8.
8 Connected Boundary Chain Code Generation Program [2] :
function out = chaincode8 (image)
% Function: Realize 8 connected chain codes
% Input: binary image
% Output: result of chain code
n = [0 1; -1 1; -1 0; -1 -1; 0 -1; 1 -1; 1 0; 1 1];
% Set flag
flag = 1;
% Initial output chain code string is empty
cc = [];
% Found starting point
[xy] = find (image == 1);
x = min (x);
imx = image (x, :);
y = min (find (imx == 1));
first = [xy];
dir = 7;
while flag == 1
tt = zeros (1,8);
newdir = mod (dir + 7-mod (dir, 2), 8);
for i = 0: 7
j = mod (newdir + i, 8) +1;
tt (i + 1) = image (x + n (j, 1), y + n (j, 2));
end
d = min (find (tt == 1));
dir = mod (newdir + d-1,8);
% After finding the direction code of the next pixel, it is added after the chain code
cc = [cc, dir];
x = x + n (dir + 1,1); y = y + n (dir + 1,2);
% Discrimination of end of chain code
if x == first (1) && y == first (2)
flag = 0;
end
end
out = cc;

IN OTHER LANGUAGES

Was this article helpful? Thanks for the feedback Thanks for the feedback

How can we help? How can we help?