Chain Codes

From the Binary Image Library homepage

Chaincodes are a way of representing a path of connected pixels. We store the starting x, y co-ordinates, then the moves, up, down, right, left, up-left, and so on. Usually chain codes represent contours, though they can also be used to represent pixel-wide lines.

The conventional representations are
321
4X0
567

Chain codes are a compact way of representing a binary image that consists mainly of large connected objects, and provide a means for comparing two objects for similarity. They are alos useful for converting binary images into vector representation.

Functions

getchaincode

Get a chain code from a binary image. Returns the chain code in ascii representation, so 0 is '0'. We extract the top-leftmost object.

binary - the binary image
width - image width
height - image height
connex - 4 or 8 connectivity (4 not implemented)
xret - return for start position x
yret - return for start position y
returns the chain code

char *getchaincode(unsigned char *binary, int width, int height, int connex, int *xret, int *yret)

chaincodetoperimeter

Create a perimeter from a chain code.

binary - the binary image
width - image width
height - image height
code - the chain code
x - start point x
y - start point y
returns 0 on success, or the number of pixels out of bounds.

  int chaincodetoperimeter(unsigned char *binary, int width, int height, char *code, int x, int y);

Examples

int testchaincodes()
{
	unsigned char *binary, *out;
	int width, height;
	unsigned char opal[256 * 3];
	int trans;
	char *ccode;
	int sx, sy;

	binary = loadgif("image.gif", &width, &height, opal, &trans);
	giftobinary(binary, width, height, opal, 256, trans);
	ccode = getchaincode(binary, width, height, 8, &sx, &sy);
	printf("code %s\n", ccode);
	out = malloc(width * height);
	memset(out, 0, width * height);
	chaincodetofill(out, width, height, ccode, sx, sy);
	printbinary(out, width, height);

}