簡體   English   中英

如何遍歷圖像繪制邊界框?

[英]How to traverse through image drawing bounding boxes?

我想遍歷圖像並在圖像中繪制邊界框,並使用圖像的子矩陣進行一些計算。 我正在嘗試使以下 C++ 代碼在 Python 中工作( 取自這里的答案)。

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;
    }
}

我想計算平均值並打印投資回報率。 這是我在 Python 中使用 Pillow 的代碼。 我用於代碼的圖像在這里

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()

由於圖像是256 x 256並且我的步驟是64 ,我期望打印 16 個區域,但它只打印第一個區域,其余的似乎是空的(看看 Pillow 對象的大小)。 我也不明白為什么它打印 24 次( <PIL.Image.Image> ),而我期待 16 次。這是我的輸出:

[[[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>

按照這里的答案,我明白我需要在打開圖像后直接將圖像轉換為 NumPy 數組,但是,它沒有幫助。

我究竟做錯了什么? 我將不勝感激任何幫助。

編輯:我使用 NumPy 數組使它工作。 我仍然不明白為什么以及如何使用 Pillow 的作物不起作用。

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))

我想知道,你為什么要使用 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()

輸出圖像:

輸出圖像

打印輸出:

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
----------------------------------------

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM