Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/285.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 将图像复制到新的较大图像上_Python_Opencv_Image Processing_Numpy - Fatal编程技术网

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