python - 为什么在python中按位显示图像错误?
问题描述
我在 python 上有代码,我在其中打开图像并在其上设置掩码:
import cv2
import numpy as np
img1 = np.zeros((250, 500, 3), np.uint8)
img1 = cv2.rectangle(img1,(200, 0), (300, 100), (255, 255, 255), -1)
img2 = cv2.imread("blackie.jpg")
bitAnd = cv2.bitwise_and(img2, img1)
bitOr = cv2.bitwise_or(img2, img1)
bitXor = cv2.bitwise_xor(img1, img2)
bitNot1 = cv2.bitwise_not(img1)
bitNot2 = cv2.bitwise_not(img2)
cv2.imshow("img1", img1)
cv2.imshow("img2", img2)
cv2.imshow('bitAnd', bitAnd)
cv2.imshow('bitOr', bitOr)
cv2.imshow('bitXor', bitXor)
cv2.imshow('bitNot1', bitNot1)
cv2.imshow('bitNot2', bitNot2)
cv2.waitKey(0)
cv2.destroyAllWindows()
当我尝试按位图像时,这给了我错误:
error: OpenCV(4.5.2) C:\Users\runneradmin\AppData\Local\Temp\pip-req-build-ttbyx0jz\opencv\modules\core\src\arithm.cpp:214: error: (-209:Sizes of input arguments do not match) 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 'cv::binary_op'
解决方案
@crackanddie 是对的,您可以使用以下代码:
import cv2
import numpy as np
import sys
img2 = cv2.imread("blackie.jpg")
img1 = np.zeros(img2.shape, np.uint8)
if img2.shape[0] > 200 or img2.shape[1] > 300:
print("Bounding box is bigger then target image")
sys.exit(0)
img1 = cv2.rectangle(img1,(200, 0), (300, 100), (255, 255, 255), -1)
bitAnd = cv2.bitwise_and(img2, img1)
bitOr = cv2.bitwise_or(img2, img1)
bitXor = cv2.bitwise_xor(img1, img2)
bitNot1 = cv2.bitwise_not(img1)
bitNot2 = cv2.bitwise_not(img2)
cv2.imshow("img1", img1)
cv2.imshow("img2", img2)
cv2.imshow('bitAnd', bitAnd)
cv2.imshow('bitOr', bitOr)
cv2.imshow('bitXor', bitXor)
cv2.imshow('bitNot1', bitNot1)
cv2.imshow('bitNot2', bitNot2)
cv2.waitKey(0)
cv2.destroyAllWindows()
推荐阅读
- sql-server - 在 Linux 上迁移到 dotnet core 2.2 后,连接到我们的 Azure SQL 数据库经常会引发登录前握手错误
- javascript - 在 Ajax 调用中控制 settimeout 的速度
- css - 仅针对父级 li 而不是具有 css 的子级
- android - 如何使用 Firebase ML Kit 自定义模型识别数字
- json - 如何在yaml中的对象内嵌套数组?
- css - Vuejs .vue 文件:动态少导入
- spring - spring cloud sleuth 无法生成 spanID
- php - Laravel html/collective 值加载到下拉列表中
- c# - 如何从另一个脚本中获取枚举值(Unity)
- .net - 是否可以在大图像上找到并计算所有“猫”?