1

I have a color image sourceImage and I would copy this image on a new larger color image destImage: the source image should be centered over the new image. In order to perform this procedure, I wrote the following code:

destHeight: the height of the new larger image destWidth: the width of the new larger image

sourceFilename: the path of the source image

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)

But when I run the script, the python interpreter displays the following error:

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

What is the reason for this error? How to solve it?

4

1 回答 1

1

尝试这个:

destImage[yBorder:yBorder + imgHeight,xBorder:xBorder + imgWidth] = sourceImage

切片语法是start:stop,不是start:width

于 2013-02-20T14:35:55.940 回答