简体   繁体   中英

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

i have two arrays of size (8,8,3) and (8,8). the first 8 elements of last column of first 3D array has to be replaced by elements of last 2D array using python.

basically m working on images of different sizes. m extracting the blue part of image, doing some calculations on it and replacing it back. the extracted blue part is forming a mxn array whereas the original image has dim= mxnxk.

m currently working on image of size (4,4,3) which will be extended for image of higher dimension. here img is image havg dimension = (4,4,3) and q is array derived from some calculations which results in size (4,4).

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

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

Traceback (most recent call last): File "C:\\Python36\\fresh_seminar\\wm_E && extract.py", line 195, in img[:,2] = q ValueError: could not broadcast input array from shape (4,4) into shape (4,3)

this is the error i get for the last code line

After struggling for such a long time i finally found the answer. Replacing the array b with part of sample image array can be done with the following code. i know its simple but it took me a lot of time to get that!

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM