2016-02-20 56 views
1

說我有一個尺寸爲40 x 39的圖像。我需要向圖像添加一行,以便最終圖像的尺寸爲40 x 40。我怎樣才能做到這一點水平和垂直?將行添加到OpenCV中的圖像Python

我試過,

img = cv2.imread('image.jpg') 
blank_image = np.zeros((1, 40, 3), np.float32) 
img = np.concatenate((img, blank_image), axis=1) 

但是這給了我這個錯誤,

ValueError: all the input arrays must have same number of dimensions 

回答

1

如這裏說numpy.concatenate的數組必須有相同的形狀,除了在與軸的維度(第一, 默認)。

嘗試創建比除了在軸的尺寸的img同樣形狀的東西blank_image(在這種情況下,我認爲這是兩個)

shape = img.shape 
shape = list(shape) 
#axis dimmension 
shape[1] = 1 
shape = tuple(shape) 
blank_image = np.zeros(shape, np.float32) 
+0

由於它的工作!由於元組是不可變的,所以不得不稍微編輯代碼。 – sope

+0

對!我會糾正答案 –