用Python中的double-for-loop将点积运算的结果赋给3D张量的2轴

2024-04-19 19:09:43 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试通过“灰度图像像素”执行点积“RGb图像像素”
我需要将点积的结果赋给ori\u img的2个轴(最后一个轴)
从np\u img\R和np\u img\u S创建“逐像素点生成”图像
代码如下

# I have (826, 600, 3) 3D tensor, which will be used as final result RGB color image
ori_img=np.zeros((826, 600, 3))

# And I also have (826, 600, 3) 3D tensor np_img_R as RGB color image,
# and (826, 600) 2D matrix np_img_S as grayscale image

# I'd like to perform dot product of 2 axis of np_img_R (3 length list like [x x x])
# and one scalar value of np_img_S indexed by row and column indices
# And I'd like to assign result of dot product into 3 axis of ori_img like [92 22 12]
# So, I used double for loop
for i in range(0,825):
    for j in range(0,599):
        # np_img_R[i,j,:]: select all from last axis
        # np_img_S[i,j]: index by row and column
        # ori_img[i,j,:]: assign result of dot product into last axis
        # to create new image
        ori_img[i,j,:]=np.dot(np_img_R[i,j,:],np_img_S[i,j])


# And I checked whether "result of dot product" and "assigned value in ori_img 3D tensor" are same
print(np.dot(np_img_R[456,232,:],np_img_S[456,232]))
# [130 250 255]
print(ori_img.astype(int)[456,232,:])
# [130 250 255]
# Above results show same result

print(np.dot(np_img_R[825,599,:],np_img_S[825,599]))
[ 50  29 113]
print(ori_img.astype(int)[825,599,:])
[0 0 0]
# But above results show different result, actually ori_img is not assigned by result of dot product

print(np_img_R.shape)
# (826, 600, 3)
print(ori_img.shape)
# (826, 600, 3)
# Shape of both show same

我想把点积的结果分配到ori\u img的3个轴上
我该怎么修?你知道吗


Tags: andof图像imageimgasnpresult