I want to traverse through an image and draw bounding boxes in the image, and do some calculations with the submatrix of the image. I am trying to make the following code in C++ work in Python (taken from the answer here).
for (int y = 0; y<resizedImage.cols - 32; y += 32) {
for (int x = 0; x<resizedImage.rows - 32; x += 32) {
// get the average for the whole 32x32 block
Rect roi(x, y, 32, 32);
Scalar mean, dev;
meanStdDev(resizedImage(roi), mean, dev); // mean[0] is the mean of the first channel, gray scale value;
}
}
I want to calculate the mean and print the ROI. This is my code in Python using Pillow. The image I used for my code is here.
image = Image.open(path)
draw = ImageDraw.Draw(image)
step = 64
original_rows, original_cols = image.size
rows = original_rows + step
cols = original_cols + step
image_arr = np.asarray(image)
for row in range(0, rows, step):
if row <= rows - step:
for col in range(0, cols, step):
if col <= cols - step:
box = (col,row,step,step)
region = image.crop(box)
print(np.asarray(region))
draw.rectangle([col,row,step,step], width = 1, outline="#FFFFFF")
image.show()
Since the image is 256 x 256
and my step is 64
, I am expecting to print 16 regions, but it only prints the first one and the rest seem to be empty (look at the size of the Pillow object). I also do not understand why it prints it 24 times (<PIL.Image.Image>
), while I am expecting 16. Here is my output:
[[[255 0 0 255]
[255 0 0 255]
[255 0 0 255]
...
[255 0 0 255]
[255 0 0 255]
[255 0 0 255]]]]
<PIL.Image.Image image mode=RGBA size=0x64 at 0x11937F5F8>
<PIL.Image.Image image mode=RGBA size=0x64 at 0x10E9A4748>
<PIL.Image.Image image mode=RGBA size=0x64 at 0x11937F3C8>
<PIL.Image.Image image mode=RGBA size=0x64 at 0x1193618D0>
<PIL.Image.Image image mode=RGBA size=64x0 at 0x11937F5F8>
<PIL.Image.Image image mode=RGBA size=0x0 at 0x10E9A4748>
<PIL.Image.Image image mode=RGBA size=0x0 at 0x11937F3C8>
<PIL.Image.Image image mode=RGBA size=0x0 at 0x1193618D0>
<PIL.Image.Image image mode=RGBA size=0x0 at 0x11937F5F8>
<PIL.Image.Image image mode=RGBA size=64x0 at 0x10E9A4748>
<PIL.Image.Image image mode=RGBA size=0x0 at 0x11937F3C8>
<PIL.Image.Image image mode=RGBA size=0x0 at 0x1193618D0>
<PIL.Image.Image image mode=RGBA size=0x0 at 0x11937F5F8>
<PIL.Image.Image image mode=RGBA size=0x0 at 0x10E9A4748>
<PIL.Image.Image image mode=RGBA size=64x0 at 0x11937F3C8>
<PIL.Image.Image image mode=RGBA size=0x0 at 0x1193618D0>
<PIL.Image.Image image mode=RGBA size=0x0 at 0x11937F5F8>
<PIL.Image.Image image mode=RGBA size=0x0 at 0x10E9A4748>
<PIL.Image.Image image mode=RGBA size=0x0 at 0x11937F3C8>
<PIL.Image.Image image mode=RGBA size=64x0 at 0x1193618D0>
<PIL.Image.Image image mode=RGBA size=0x0 at 0x11937F5F8>
<PIL.Image.Image image mode=RGBA size=0x0 at 0x10E9A4748>
<PIL.Image.Image image mode=RGBA size=0x0 at 0x11937F3C8>
<PIL.Image.Image image mode=RGBA size=0x0 at 0x1193618D0>
Following the answer here, I understood that I needed to turn the image into a NumPy array straight after I open the image, however, it does not help.
What am I doing wrong? I would appreciate any help.
EDIT: I made it work using the NumPy array. I still do not understand why and how using Pillow's crop didn't work.
image = Image.open(path)
step = 64
rows, cols = image.size
image_arr = np.asarray(image) #Added this
for row in range(0, rows, step):
for col in range(0, cols, step):
roi = image_arr[row:row+step, col:col+step] #Added this instead of using Pillow
print(np.mean(roi))