Python 将图像复制到新的较大图像上
我有一个彩色图像Python 将图像复制到新的较大图像上,python,opencv,image-processing,numpy,Python,Opencv,Image Processing,Numpy,我有一个彩色图像sourceImage,我会将此图像复制到一个新的更大的彩色图像destImage:源图像应该位于新图像的中心。为了执行此过程,我编写了以下代码: destHeight:新的较大图像的高度 destWidth:新的较大图像的宽度 sourceFilename:源映像的路径 sourceImage = cv2.imread(sourceFilename,1) imgHeight, imgWidth, imgChannels = sourceImage.shape[:3] #prin
sourceImage
,我会将此图像复制到一个新的更大的彩色图像destImage
:源图像应该位于新图像的中心。为了执行此过程,我编写了以下代码:
destHeight:新的较大图像的高度
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
回溯(最近一次呼叫最后一次):
文件“examples.py”,第30行,在
Destinmage[yBorder:imgHeight,xBorder:imgWidth]=源映像
ValueError:形状不匹配:无法将对象广播到单个形状
这个错误的原因是什么?如何解决它?试试这个:
destImage[yBorder:yBorder + imgHeight,xBorder:xBorder + imgWidth] = sourceImage
切片语法是start:stop
,而不是start:width
尝试以下操作:
destImage[yBorder:yBorder + imgHeight,xBorder:xBorder + imgWidth] = sourceImage
切片语法是start:stop
,而不是start:width