简体   繁体   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

i have two arrays of size (8,8,3) and (8,8).我有两个大小为 (8,8,3) 和 (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.第一个 3D 数组最后一列的前 8 个元素必须使用 python 替换为最后一个 2D 数组的元素。

basically m working on images of different sizes.基本上是在处理不同大小的图像。 m extracting the blue part of image, doing some calculations on it and replacing it back. m 提取图像的蓝色部分,对其进行一些计算并将其替换回来。 the extracted blue part is forming a mxn array whereas the original image has dim= mxnxk.提取的蓝色部分正在形成一个 mxn 数组,而原始图像具有 dim= mxnxk。

m currently working on image of size (4,4,3) which will be extended for image of higher dimension.我目前正在研究尺寸 (4,4,3) 的图像,该图像将扩展为更高维度的图像。 here img is image havg dimension = (4,4,3) and q is array derived from some calculations which results in size (4,4).这里 img 是图像 havg 维度 = (4,4,3),q 是从一些计算得出的数组,结果大小为 (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)回溯(最近一次调用最后一次):文件“C:\\Python36\\fresh_seminar\\wm_E && extract.py”,第 195 行,在 img[:,2] = q ValueError:无法从形状(4,4)广播输入数组成型 (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.可以使用以下代码将数组 b 替换为样本图像数组的一部分。 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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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