1

カラー画像が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

このエラーの理由は何ですか?それを解決する方法は?

4

1 に答える 1

1

これを試して:

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

スライス構文はstart:stop、ではなくstart:widthです。

于 2013-02-20T14:35:55.940 に答える