第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

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

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

瀟湘沐 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個區(qū)域,但它只打印第一個區(qū)域,其余的似乎是空的(看看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>按照這里的答案,我明白我需要在打開圖像后立即將圖像轉(zhuǎn)換為NumPy數(shù)組,但是,這無濟于事。我做錯了什么?我將不勝感激任何幫助。
查看完整描述

1 回答

?
翻翻過去那場雪

TA貢獻2065條經(jīng)驗 獲得超14個贊

我想知道,為什么你使用PIL,特別是你的代碼源是基于OpenCV的,無論如何你都需要處理NumPy數(shù)組。


這就是我的解決方案:


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

輸出圖像:

http://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 關(guān)注
  • 104 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號