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 are3 | 2 | 1 |
4 | X | 0 |
5 | 6 | 7 |
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.
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)
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);
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); }