简体   繁体   中英

How to identify and crop a rectangle from a picture using java

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM