我想遍历图像并在图像中绘制边界框,然后使用图像的子矩阵进行一些计算。我试图让下面的代码在python中工作C++(取自这里的答案)。
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;
}
}
我想计算平均值并打印 ROI。这是我使用Pillow的Python代码。我用于代码的图像在这里。
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()
由于图像是,而我的步骤是,我期望打印16个区域,但它只打印第一个区域,其余的似乎是空的(看看Pillow对象的大小)。我也不明白为什么它打印了24次(),而我期待16次。这是我的输出:256 x 25664<PIL.Image.Image>
[[[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>
按照这里的答案,我明白我需要在打开图像后立即将图像转换为NumPy数组,但是,这无济于事。
我做错了什么?我将不胜感激任何帮助。
翻翻过去那场雪
相关分类