Something like this should do this:
int line_width = 1; //the width of the square line int squares = 0; for (int x = 0; x < width; ++x) { for (int y = 0; y < height; ++y) { for (int size = line_width; x + size < width && y + size < height; ++x) { if (valid_square_at(x, y, x + size, y + size, line_width)) { ++squares; } } } } return squares; bool valid_square_at(x_0, y_0, x_1, y_1, width) { return valid_line(x_0, y_0, x_0_ x_1, width) && valid_line(x_0, y_0, x_1_ x_0, width) && valid_line(x_0, y_1, x_1_ x_1, width) && valid_line(x_1, y_0, x_1_ x_1, width); } bool valid_line(x_0, y_0, x_1, y_1, width) { //check that all pixel in that line are black... }
Basically, for each point in the picture, you check every possible square size if a square of that size starts with this pixel. This is pretty easy, since your squares are all βalignedβ with the borders ... The problem would be if they were not ...
user1494736
source share