gpt4 book ai didi

python opencv 按位异或

转载 作者:太空宇宙 更新时间:2023-11-03 22:44:16 25 4
gpt4 key购买 nike

我正在尝试合并两张图片,它们是原始图片的片段。这是两个分割的示例:

图片 enter image description here

图b enter image description here

但是当尝试将它们结合起来时:

img_to_assemble = cv2.bitwise_xor(pic_a,pic_b)

我收到以下错误:

The operation is neither 'array op array' (where arrays have the same size and type), nor 'array op scalar', nor 'scalar op array' in function binary_op

最佳答案

为了执行您想要执行的操作,请首先确保您的图像大小相同。

为了组合我执行的两个图像 cv2.bitwise_or()

python 代码:

img_a = cv2.imread("a.png", 1)
img_b = cv2.imread(".png", 1)

ret, thresh = cv2.threshold(img_b, 10, 255, cv2.THRESH_BINARY)

rows,cols,channels = img_b.shape
roi = img_a[0:rows, 0:cols ]

img2gray = cv2.cvtColor(img_b,cv2.COLOR_BGR2GRAY)
ret, mask = cv2.threshold(img2gray, 10, 255, cv2.THRESH_BINARY)
mask_inv = cv2.bitwise_not(mask)

img1_bg = cv2.bitwise_and(roi, roi, mask = mask_inv)
img2_fg = cv2.bitwise_or(img_b, img_b, mask = mask)

dst = cv2.add(img1_bg,img2_fg)
img_a[0:rows, 0:cols ] = dst
cv2.imshow('res',img_a)

这是我得到的结果:

enter image description here

关于python opencv 按位异或,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49957788/

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