Note that your comment about the file being a PDF instead of a PNG changes the question, in that there ways to process a PDF that you cannot do to PNG.
First import the "Pages"
of the PDF and stored the first (and only) one in i
:
i = First @ Import["https://dl.dropboxusercontent.com/u/8003134/img.pdf", "Pages"];
Now the image is in fact scalable Graphics
and the black squares and green rectangles are stored as JoinedCurve
and FilledCurve
objects:
Cases[i, _JoinedCurve, Infinity, 1]
Cases[i, _FilledCurve, Infinity, 1]
(*
{JoinedCurve[{{{0, 2, 0}, {0, 1, 0}, {0, 1, 0}}},
{{{2., 647.}, {47., 647.}, {47., 602.}, {2., 602.}}},
CurveClosed -> {1}]}
{FilledCurve[{{{0, 2, 0}, {0, 1, 0}, {0, 1, 0}}},
{{{2., 597.}, {47., 597.}, {47., 552.}, {2., 552.}}}]}
*)
Note that the coordinates of the squares and rectangles are in the second argument.
Also upon inspection, the squares are stored in order by columns. So we can extract the squares, Partition them, and map the coordinates of a corner to the index of the square. The green rectangles share two of the coordinates with its enclosing square, so we pick a corner in common to both (the 4th coordinate). We can get the area of a green rectangle by subtracting the coordinates of opposite corners and multiplying, and store then as rules mapping corner to areas. We can get the total area of a square the same way.
corner2idx = With[{squares = Cases[i, JoinedCurve[_, {rect_}, ___] :> rect, Infinity]},
Flatten @ MapIndexed[#[[4]] -> Reverse[#2] &, Partition[squares, 13], {2}]];
corner2area = With[{green = Cases[i, FilledCurve[_, {rect_}, ___] :> rect, Infinity]},
#[[4]] -> Times @@ First@Differences[#[[{2, 4}]]] & /@ green];
totalArea =
Times @@ First @ Differences[
Cases[i, JoinedCurve[_, {rect_}, ___] :> rect, Infinity, 1][[1, {2, 4}]]]
(*
2025.
*)
Now we can put it all together, using the rules corner2idx
that map corner coordinates to indices to create a list of array rules that map the index to the corresponding area. Dividing by the total area gives the proportion of green in each square.
proportions = SparseArray[corner2area /. corner2idx, {13, 13}, 0.]/totalArea;
Round[proportions, 0.02] // MatrixForm