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.