0

I am bit new to image processing. What i'm doing is recognize rectangular shapes (not overlapped) of a given image and create separate images by crop them out. So the output images should be without a border. I tried some examples but none of it did the trick. FYI: those horizontal rectangles are with black lined border in a white background. There are some symbols inside them.

does anyone have a clue or an similar example? regards on help

javatar
  • 1,332
  • 1
  • 17
  • 24
  • I've never done such things, but what you could do is, when you find a black pixel, do two recursive loop (one that goes to the right and one to the bottom). Then, call the loop until you don't find anymore black pixel near the one you are looking for. Both recursive loop will return to the 1st call. You will have to keep the current amount of black pixels found in each of your 2 loops. Then, compare them. If you find that there's nearly the same amount, there's a square/rectangle. I'm not really clear, but i'm not even sure this would work. But it worth the try. – Larta Jun 02 '14 at 11:17
  • Thanks bro. Any similar example for a head start? – javatar Jun 02 '14 at 11:20

1 Answers1

1

This is pseudo C code, but my idea is there.

 Main Structure

struct data {
   float pixelsNb;
   int currentX;
   int currentY;
}

Main loop 

void mainLoop(){
    void **imgData = getPixelsFromImage("toto.png");
    struct dataRight, dataDown;
    loopRight(&dataRight, imgData);
    loopDown(&dataDown, imgData);
    // now you data right struct contains the number of
    //following black pixels to the right
    // and you data down, same for the down side.
    if (dataRight->pixelNb == dataDown->pixelNb) // not really, should be in %
       printf("There's a square !");
}

void loopRight(struct data *dataCurrent, void **imgData){
   if (imgData[dataCurrent->currentY][dataCurrent->currentX] == color(0x0)){
       dataCurrent->pixelNb++;
       dataCurrent->currentX++;
      loopRight(dataCurrent, imgData);
   }
}
void loopDown(struct data *dataCurrent, void **imgData){
   if (imgData[dataCurrent->currentY][dataCurrent->currentX] == color(0x0)){
       dataCurrent->pixelNb++;
       dataCurrent->currentY++;
      loopDown(dataCurrent, imgData);
   }
}
}

This is really not accurate. Don't try to copy and past, it will fail. But you have the idea here. Also note that i only check the line in the up side, and the linde on the left side

XXXXXXXX
X      o
X      o
X      o
X      o
Xooooooo

X are checked, not o

The algo here is just to check if there's the same number of X on the left side and on the top side. If it's the case, you have a square. For sure, if you want to find a rectangle, you have to check down side and right side. Then, it would be : If there's the same amount on the left side + down side and on the top side + right side, then we have a rectangle.

That sort of algorythm should do the trick.

Larta
  • 406
  • 2
  • 9