Binary Image Library
Macros | Functions
C:/Users/Malcolm/Documents/temp/binaryimagelibrary/binaryutils.c File Reference
#include <stdlib.h>
#include <string.h>

Macros

#define CNT_TOPLEFT   0x80
 
#define CNT_TOPRIGHT   0x40
 
#define CNT_BOTTOMRIGHT   0x20
 
#define CNT_BOTTOMLEFT   0x10
 
#define CNT_RIGHT   1
 
#define CNT_DOWN   2
 
#define CNT_LEFT   3
 
#define CNT_UP   4
 

Functions

int morphclose (unsigned char *binary, int width, int height, unsigned char *sel, int swidth, int sheight)
 
int morphopen (unsigned char *binary, int width, int height, unsigned char *sel, int swidth, int sheight)
 
int dilate (unsigned char *binary, int width, int height, unsigned char *sel, int swidth, int sheight)
 
int erode (unsigned char *binary, int width, int height, unsigned char *sel, int swidth, int sheight)
 
int * labelconnected (unsigned char *binary, int width, int height, int connex, int *Nout)
 
int eulernumber (unsigned char *binary, int width, int height)
 
int getbiggestobject (unsigned char *binary, int width, int height, int connex)
 
int branchpoints (unsigned char *binary, int width, int height, int **xout, int **yout)
 
int ends (unsigned char *binary, int width, int height, int **xout, int **yout)
 
unsigned char * perimeter (unsigned char *binary, int width, int height)
 
void invertbinary (unsigned char *binary, int width, int height)
 
unsigned char * copybinary (unsigned char *binary, int width, int height)
 
unsigned char * subbinary (unsigned char *binary, int width, int height, int x, int y, int swidth, int sheight)
 
void boundingbox (unsigned char *binary, int width, int height, int *x, int *y, int *bbwidth, int *bbheight)
 
int simplearea (unsigned char *binary, int width, int height)
 
double complexarea (unsigned char *binary, int width, int height)
 
void * compressbinary (unsigned char *binary, int width, int height, int *clen)
 
unsigned char * decompressbinary (unsigned char *comp, int *width, int *height)
 
int getcontours (unsigned char *binary, int width, int height, double ***x, double ***y, int **Nret)
 
int lineends (unsigned char *binary, int width, int height, int **xout, int **yout)
 
unsigned char * binary_addborder (unsigned char *binary, int width, int height, int border, unsigned char fill)
 
unsigned char * binary_addborderwrapped (unsigned char *binary, int width, int height, int border)
 
unsigned char * binary_removeborder (unsigned char *binary, int width, int height, int border)
 

Detailed Description

Binaryutils.c

Source for the binary image processing library simple utlities.

Put anything that is simple and stands alone more or less here.

Very much a first, provisional version.

By Malcolm McLean.

Function Documentation

unsigned char* binary_addborder ( unsigned char *  binary,
int  width,
int  height,
int  border,
unsigned char  fill 
)

Add a border to binary image (useful for removed edge conditions)

Parameters
[in]binary- the binary image
width- image width
height- image height
Returns
Pointer to malloced enlarged image, 0 on out of memory.
unsigned char* binary_addborderwrapped ( unsigned char *  binary,
int  width,
int  height,
int  border 
)

Add border with wrapping (useful for some types of filters).

Parameters
[in]binary- the binary image
width- image width
height- image height
border- number of border pixels to add
Returns
The enlarged image, 0 on out of memory.
unsigned char* binary_removeborder ( unsigned char *  binary,
int  width,
int  height,
int  border 
)

Remove a border from a binary image.

Parameters
[in]binary- the binary image
width- image width
height- image height
border- number of border pixels to remove
Returns
Malloced destination image, 0 on failure.
void boundingbox ( unsigned char *  binary,
int  width,
int  height,
int *  x,
int *  y,
int *  bbwidth,
int *  bbheight 
)

Get the bounding box of the set pixels in binary image.

Parameters
[in]binary- the binary image
width- image width
height- image height
[out]xreturn for bounding box top left x co-ordinate
[out]yreturn for bounding box top left y co-ordinate
[out]bbwidth- return for bounding box width
[out]bbheight- return for bounding box height
Note
x and y are -1 if there are no set pixels in the image.
int branchpoints ( unsigned char *  binary,
int  width,
int  height,
int **  xout,
int **  yout 
)

Get branch points

Parameters
[in]binary- the binary image
width- image width
height- image height
[out]xout- return for branch point x-coordiantes (malloced)
[out]yout- return for branch point y-coordinates ( malloced)
Returns
Number of branch pints found, -1 on fail.
double complexarea ( unsigned char *  binary,
int  width,
int  height 
)

Get the area of binary image, weighting pixels by pattern.

Parameters
[in]binary- the binary image
width- image width
height- image height
Returns
weighted area of the image.
void * compressbinary ( unsigned char *  binary,
int  width,
int  height,
int *  clen 
)

Quick run-length compression of a binary image.

Parameters
[in]binary- the binary image
width- image width
height- image height
[out]clen- return pointer for length of compressed data
Returns
pointer to malloced compressed data.
unsigned char * copybinary ( unsigned char *  binary,
int  width,
int  height 
)

Copy a binary image.

Parameters
[in]binary- the binary image
width- iamge width
height- image height
Returns
Pointer to malloced copy of image, 0 on out of memory.
unsigned char * decompressbinary ( unsigned char *  comp,
int *  width,
int *  height 
)

Decompression of run-length compressed binary image.

Parameters
[in]comp- pointer to the compressed data
[out]width- return pointer for image width
[out]height- return pointer for image height
Returns
decompressed image data
static int dilate ( unsigned char *  binary,
int  width,
int  height,
unsigned char *  sel,
int  swidth,
int  sheight 
)

Dilate operation.

Parameters
[in,out]bianry- the binary image
width- image width
height- image height
sel[in]- the selection element
swidth- selection element width
sheight- selection element height
Returns
0 on sucess, -1 on error.
int ends ( unsigned char *  binary,
int  width,
int  height,
int **  xout,
int **  yout 
)

Get ends

Parameters
[in]binary- the binary image
width- image width
height- image height
[out]xout- return for end x-coordiantes (malloced)
[out]yout- return for end y-coordinates ( malloced)
Returns
Number of branch pints found, -1 on fail.
Note
naive algorithm, just returns everythign with 1 neighbout
int erode ( unsigned char *  binary,
int  width,
int  height,
unsigned char *  sel,
int  swidth,
int  sheight 
)

Erode operation.

Parameters
[in,out]bianry- the binary image
width- image width
height- image height
sel[in]- the selection element
swidth- selection element width
sheight- selection element height
Returns
0 on sucess, -1 on error.
int eulernumber ( unsigned char *  binary,
int  width,
int  height 
)

Calculate the Euler number for a binary image.

[in] binary - the binary image

Parameters
width- image width
height- image height
Returns
Euler number, = number of objects - number of holes.
Note
on error returns INT_MIN.
int getbiggestobject ( unsigned char *  binary,
int  width,
int  height,
int  connex 
)

Erase everything except biggest object in bianry image.

Parameters
[in.out]binary - the binary image
width- image width
height- image height
connex- 4 or 8 connectivity
Returns
0 on success, -1 on fail
int getcontours ( unsigned char *  binary,
int  width,
int  height,
double ***  x,
double ***  y,
int **  Nret 
)

Go round a binary image, extracting the contours of the set pixels.

Parameters
[in]binary- the image
width- image width
height- image height
[out]x- return for x co-ordinates of contours (malloced)
[out]y- return for y co-ordinates of contours (malloced)
[out]Nret- return for contour lengths (malloced)
Returns
Number contours found, -1 on out of memory.
void invertbinary ( unsigned char *  binary,
int  width,
int  height 
)

Invert a binary image.

Parameters
[in,out]binary- the binary image
width- image width
height- image height
Note
in place.
int * labelconnected ( unsigned char *  binary,
int  width,
int  height,
int  connex,
int *  Nout 
)

Label connected components in binary image.

Parameters
[in]binary- the binary image
width- image width
height- image height
connex- 4 or 8 connectivity
[out]Nout- number of components found returns Width * height array of labels for connected components.
int lineends ( unsigned char *  binary,
int  width,
int  height,
int **  xout,
int **  yout 
)

Get line end points

Parameters
[in]binary- the binary image
width- image width
height- image height
[out]xout- return for branch point x-coordiantes (malloced)
[out]yout- return for branch point y-coordinates ( malloced)
Returns
Number of line end found, -1 on fail.
int morphclose ( unsigned char *  binary,
int  width,
int  height,
unsigned char *  sel,
int  swidth,
int  sheight 
)

Morphological close operation.

Parameters
[in,out]bianry- the binary image
width- image width
height- image height
sel[in]- the selection element
swidth- selection element width
sheight- selection element height
Returns
0 on sucess, -1 on error.
int morphopen ( unsigned char *  binary,
int  width,
int  height,
unsigned char *  sel,
int  swidth,
int  sheight 
)

Morphological open operation.

Parameters
[in,out]bianry- the binary image
width- image width
height- image height
sel[in]- the selection element
swidth- selection element width
sheight- selection element height
Returns
0 on sucess, -1 on error.
unsigned char * perimeter ( unsigned char *  binary,
int  width,
int  height 
)

get perimeter pixels.

Parameters
[in]binary- the bianry image
width- image width
height- image height
Returns
The perimeter pixels.
int simplearea ( unsigned char *  binary,
int  width,
int  height 
)

Get the area (number of set pixels) in a binary image.

Parameters
[in]binary- the binary image
width- image width
height- image height
Returns
number of set pixels.
unsigned char * subbinary ( unsigned char *  binary,
int  width,
int  height,
int  x,
int  y,
int  swidth,
int  sheight 
)

Take a sub-image of a binary image.

Parameters
[in]binary- the binary image
width- image width
height- image height
x- top left x co-ordinate of sub-image
y- top left y co-ordinate of sub-image
swidth- sub image width
sheight- sub image height
Returns
Malloced pointer to sub image.
Note
If the sub-image extends the image boundary, it is zero padded, so x and y can be negative.