Solly - 3 months ago 20

Python Question

I have an image in the form of a matrix of 512 x 512 entries. Something like

`[[12,234, . . ... . (512 entries)],`

[[12,234, . . ... . (512 entries)],

[[12,234, . . ... . (512 entries)],

.

.

[12,234, . . ... . (512 entries)]]

I want to break the image into 4x4 blocks and put them into one list. How do I do this? There would be 128 blocks of size 4 x 4, starting indexing from left.

Answer

`OpenCV`

use `numpy`

and `numpy`

lets you use indexes like `image[0:4, 0:4]`

to get square:

So you need something similar to this:

```
width, height, depth = image.shape
blocks = []
for y in range(0, height, 4):
for x in range(0, width, 4):
blocks.append(image[y:y+4, x:x+4])
```