How should I separate out shapes from a bitmap?

Hi there,
I'm no programmer, but I need to find or write software for a rather unusual purpose. I need to partially automate the accurate recovery of the letterforms of a Renaissance font from printed pages, presented as monochrome bitmaps.

Firstly, the program needs to isolate each of the black forms on the white background, placing each separately in memory. These forms do not stand in neat rectangles of white.

I have no idea how to do this in the languages of which I understand a little, VB (5!) and Python, and a solution for either or in pseudocode would be wonderful. I assume this is easy for someone who knows what they're doing: I don't.

Thanks very much,
George

Comments

  • Just separating black shapes from bitmap is not difficult.

    What you would have to do, is find a black pixel and start filling all black pixels around it and remember each pixel you filled. Then repeat it for pixels not yet filled or tested to find all shapes.

    A pseudo-codeish C code for finding something like this might look something like this (note, that the routine changes the starting bitmap, just make a copy or use extra buffer to remember already used pixels, also you need to modify the fillShape code, if you want the following bitmap to read a single shape.
    [code]
    // 2 shapes will be found
    ######
    # #####
    ######

    1. #
    and
    2. ######
    #####
    ######
    [/code]
    ):

    [code]
    int maxX=100;
    int maxY=100;
    int maxShapes=100;

    // Finding a single shape, returns true, if a shape is found
    bool findShape(int pixels[maxX][maxY], int x, int y, Shape &shape) {
    if(x<0 || x>=maxX || y<0 || y>=maxY) // Check for image bounds
    return false;
    if(pixels[x][y]==0) {
    pixels[x][y]=2; // Mark pixel as checked
    shape.addPixel(x, y); // Add pixel coordinates to shape
    findShape(pixels, x+1, y, shape); // Check for right pixel
    findShape(pixels, x-1, y, shape); // Check for left pixel
    findShape(pixels, x, y+1, shape); // Check for lower pixel
    findShape(pixels, x, y-1, shape); // Check for upper pixel
    return true;
    }
    else {
    return false;
    }
    }

    // In the parameter, let pixels be 0 or 1 for black or white and 2 for
    // pixels already belonging to a shape
    void findAllShapes(int pixels[maxX][maxY]) {
    int shapes=0; // Number of found shapes
    Shape shape[maxShapes]; // Shapes

    // Check all pixels
    for(int i=0; i<maxX; i=i+1) {
    for(int j=0; j<maxY; j=j+1) {
    Shape newShape; // Potential new shape
    if(findShape(pixels, i, j, newShape)) {
    // A pixel belonging to a shape is found at i, j, lets add
    // a new shape to found shapes
    shape[shapes]=shape;
    shapes=shapes+1
    }
    }
    }
    }
    [/code]

    You just need to find max and min coordinates for each pixel in shape to find the size of found shape, create a new bitmap with right size and fill in the pixels as black thats coordinates are stored in the Shape. I didn't implement the Shape class, but it is just a dynamically allocatable array of pixel coordinates.

    Good luck,
    XLoom

  • Wonderful! Thanks very much,
    George
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories