在 OpenCV 中向图像附加一行 Python
Appending a line to an image in OpenCV Python
假设我有一张尺寸为 40 x 39 的图像。我需要在图像中添加一条线,以便最终图像的尺寸为 40 x 40。我怎样才能水平和垂直地做到这一点?
我试过了,
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
如这里所述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)
我们还可以使用 OpenCV 中的函数 cv.copyMakeBorder() Documentation 函数来附加行或列
只需将所需的值赋予 top 、 down 、 left 、 right argumnets
def createBorder( ):
global img # image in which we want to append
borderType = cv.BORDER_CONSTANT
TDLU=[0 , 1, 0 , 1 ]#top,down,left,right values
img = cv.copyMakeBorder(img, TDLU[0] , TDLU[1] , TDLU[2] , TDLU[3] , borderType, None, 255)
row, col = img.shape
print(row, col)
假设我有一张尺寸为 40 x 39 的图像。我需要在图像中添加一条线,以便最终图像的尺寸为 40 x 40。我怎样才能水平和垂直地做到这一点?
我试过了,
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
如这里所述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)
我们还可以使用 OpenCV 中的函数 cv.copyMakeBorder() Documentation 函数来附加行或列 只需将所需的值赋予 top 、 down 、 left 、 right argumnets
def createBorder( ):
global img # image in which we want to append
borderType = cv.BORDER_CONSTANT
TDLU=[0 , 1, 0 , 1 ]#top,down,left,right values
img = cv.copyMakeBorder(img, TDLU[0] , TDLU[1] , TDLU[2] , TDLU[3] , borderType, None, 255)
row, col = img.shape
print(row, col)