簡體   English   中英

如何使用python將數組(大小=(8,8,3))的最后一列的前8個元素替換為大小為(8,8)的二維數組

[英]how to replace the first 8 elements of last column of array(size = (8,8,3 )) with 2d array of size (8,8) using python

我有兩個大小為 (8,8,3) 和 (8,8) 的數組。 第一個 3D 數組最后一列的前 8 個元素必須使用 python 替換為最后一個 2D 數組的元素。

基本上是在處理不同大小的圖像。 m 提取圖像的藍色部分,對其進行一些計算並將其替換回來。 提取的藍色部分正在形成一個 mxn 數組,而原始圖像具有 dim= mxnxk。

我目前正在研究尺寸 (4,4,3) 的圖像,該圖像將擴展為更高維度的圖像。 這里 img 是圖像 havg 維度 = (4,4,3),q 是從一些計算得出的數組,結果大小為 (4,4)。

img = cv2.imread("ori.jpg")
print(img)

img[:,2] = q       #here q is an 4x4 array

回溯(最近一次調用最后一次):文件“C:\\Python36\\fresh_seminar\\wm_E && extract.py”,第 195 行,在 img[:,2] = q ValueError:無法從形狀(4,4)廣播輸入數組成型 (4,3)

這是我在最后一行代碼中得到的錯誤

經過這么長時間的掙扎,我終於找到了答案。 可以使用以下代碼將數組 b 替換為樣本圖像數組的一部分。 我知道它很簡單,但我花了很多時間才弄明白!

b = np.ones((8,8), dtype = int)     #array b of size(8,8) to be replaced
half_cover = cv2.imread("sample.jpg")  
print(half_cover)
print(half_cover.shape)              shape = (894, 894, 3)  

for i in range(8):
    for j in range(8):
        half_cover[i,j] = b[i,j]
print(half_cover)

暫無
暫無

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

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