gpt4 book ai didi

python - 将 1 channel 骨架图像叠加到 3 channel RGB 图像上

转载 作者:行者123 更新时间:2023-12-04 16:24:34 25 4
gpt4 key购买 nike

我有兴趣将骨架像素为 255(白色)的黑白骨架图像叠加到 3 channel RGB 图像上。我并不偏向于使用 OpenCV,但是当我使用 add 函数时,我得到了形状不兼容的错误。

这样做的方法是什么?

import cv2
skl_img = cv2.imread("sample_skeleton_image/579366_2_train2017_skel.png", 0)
print(skl_img.shape)
plt.imshow(skl_img)

224x400

enter image description here

obj_img = cv2.imread("/content/drive/Shareddrives/Wish 2021/Code/sample_skeleton_image/579366_2_train2017.jpg", cv2.IMREAD_COLOR)
plt.imshow(obj_img)

obj_img shape is 224x400x3

enter image description here

final_img = cv2.add(obj_img,skl_img)
plt.imshow(final_img)

error                                     Traceback (most recent call last)
<ipython-input-27-7de7a4136982> in <module>()
----> 1 final_img = cv2.add(obj_img,skl_img)
2 plt.imshow(final_img)

error: OpenCV(4.1.2) /io/opencv/modules/core/src/arithm.cpp:663: error: (-209:Sizes of input arguments do not match) The operation is neither 'array op array' (where arrays have the same size and the same number of channels), nor 'array op scalar', nor 'scalar op array' in function 'arithm_op'

基本上,我对最终图像感兴趣,如下所示: enter image description here

最佳答案

以下是在 Python 中将 mask 应用到图像的其他几种方法。

输入:

enter image description here

面具:

enter image description here

import cv2
import numpy as np

# read image
img = cv2.imread('elephant_trunk.png')

# read mask as grayscale
mask = cv2.imread('elephant_trunk_skeleton.png', cv2.IMREAD_GRAYSCALE)

# Method 1: Numpy masking
result1 = img.copy()
result1[mask!=0] = (255,255,255)

# Method 2: Merge mask to 3 channels
mask2 = cv2.merge([mask,mask,mask])
result2 = cv2.add(img, mask2)

# Method 3: Convert mask to BGR
mask3 = cv2.cvtColor(mask, cv2.COLOR_GRAY2BGR)
result3 = cv2.add(img, mask3)

# save result
cv2.imwrite("elephant_trunk_masked.png", result1)

# show result
cv2.imshow("RESULT1", result1)
cv2.imshow("RESULT2", result2)
cv2.imshow("RESULT3", result3)
cv2.waitKey(0)

结果:

enter image description here

关于python - 将 1 channel 骨架图像叠加到 3 channel RGB 图像上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68028006/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com