为了账号安全,请及时绑定邮箱和手机立即绑定

如何遍历图像绘图边界框?

如何遍历图像绘图边界框?

潇湘沐 2022-08-25 15:12:10
我想遍历图像并在图像中绘制边界框,然后使用图像的子矩阵进行一些计算。我试图让下面的代码在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 = 64original_rows, original_cols = image.sizerows = original_rows + stepcols = original_cols + stepimage_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数组,但是,这无济于事。我做错了什么?我将不胜感激任何帮助。
查看完整描述

1 回答

?
翻翻过去那场雪

TA贡献2065条经验 获得超14个赞

我想知道,为什么你使用PIL,特别是你的代码源是基于OpenCV的,无论如何你都需要处理NumPy数组。


这就是我的解决方案:


import cv2

import numpy as np


# Read input image; create additional output image to draw on

image = cv2.imread('ZsyOG.png')

image_out = image.copy()


# Parameters

step = 64

cols, rows = image.shape[:2]


# Actual processing in loop

i_region = 0

for row in np.arange(0, rows, step):

    for col in np.arange(0, cols, step):

        mean = cv2.mean(image[row:row+step, col:col+step])

        image_out = cv2.rectangle(img=image_out,

                                  pt1=(row, col),

                                  pt2=(row + step, col + step),

                                  color=(255, 255, 255),

                                  thickness=1)

        image_out = cv2.putText(img=image_out,

                                text=str(i_region),

                                org=(int(col+1/2*step), int(row+1/2*step)),

                                fontFace=cv2.FONT_HERSHEY_COMPLEX_SMALL,

                                fontScale=1.0,

                                color=(255, 255, 255))

        print('Region: ', i_region, '| Mean: ', mean)

        i_region += 1


cv2.imshow('image_out', image_out)

cv2.waitKey(0)

cv2.destroyAllWindows()

输出图像:

//img1.sycdn.imooc.com//630720f80001e0aa02570256.jpg

打印输出:


Region:  0 | Mean:  (0.0, 0.0, 255.0, 0.0)

Region:  1 | Mean:  (0.0, 0.0, 255.0, 0.0)

Region:  2 | Mean:  (0.0, 255.0, 255.0, 0.0)

Region:  3 | Mean:  (0.0, 255.0, 255.0, 0.0)

Region:  4 | Mean:  (0.0, 0.0, 255.0, 0.0)

Region:  5 | Mean:  (0.0, 0.0, 255.0, 0.0)

Region:  6 | Mean:  (0.0, 255.0, 255.0, 0.0)

Region:  7 | Mean:  (0.0, 255.0, 255.0, 0.0)

Region:  8 | Mean:  (0.0, 0.0, 0.0, 0.0)

Region:  9 | Mean:  (0.0, 0.0, 0.0, 0.0)

Region:  10 | Mean:  (255.0, 0.0, 0.0, 0.0)

Region:  11 | Mean:  (255.0, 0.0, 0.0, 0.0)

Region:  12 | Mean:  (0.0, 0.0, 0.0, 0.0)

Region:  13 | Mean:  (0.0, 0.0, 0.0, 0.0)

Region:  14 | Mean:  (255.0, 0.0, 0.0, 0.0)

Region:  15 | Mean:  (255.0, 0.0, 0.0, 0.0)

希望有所帮助!


----------------------------------------

System information

----------------------------------------

Platform:    Windows-10-10.0.16299-SP0

Python:      3.8.1

NumPy:       1.18.1

OpenCV:      4.2.0

----------------------------------------


查看完整回答
反对 回复 2022-08-25
  • 1 回答
  • 0 关注
  • 165 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号