在新的较大图像上复制图像

2024-03-29 10:23:08 发布

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

我有一个彩色图像sourceImage,我会把这个图像复制到一个新的更大的彩色图像destImage:源图像应该位于新图像的中心。为了执行此过程,我编写了以下代码:

destwheight:新的较大图像的高度 destWidth:新的较大图像的宽度

sourceFilename:源映像的路径

sourceImage = cv2.imread(sourceFilename,1)
imgHeight, imgWidth, imgChannels = sourceImage.shape[:3]
#print sourceImage.shape[:3]

destImage = np.zeros((destHeight,destWidth,imgChannels), np.uint8)
#print destImage.shape[:3]

yBorder = (destHeight-imgHeight)/2
xBorder = (destWidth-imgWidth)/2
#print yBorder, xBorder

destImage[yBorder:imgHeight,xBorder:imgWidth] = sourceImage
cv2.imshow('dst', destImage)
cv2.waitKey(0)

但是当我运行脚本时,python解释器会显示以下错误:

Traceback (most recent call last):
  File "examples.py", line 30, in <module>
    destImage[yBorder:imgHeight,xBorder:imgWidth] = sourceImage
ValueError: shape mismatch: objects cannot be broadcast to a single shape

这个错误的原因是什么?如何解决?你知道吗


Tags: 图像npcv2彩色图像printshapesourcefilenameimgwidth