numpy.hstack
1. numpy.hstack(tup)
Stack arrays in sequence horizontally (column wise).
水平 (按列) 顺序堆叠数组。
This is equivalent to concatenation along the second axis, except for 1-D arrays where it concatenates along the first axis. Rebuilds arrays divided by hsplit
.
这等效于沿第二个轴的连结,除了一维数组沿第一个轴连结。通过 hsplit
分离复原数组。
This function makes most sense for arrays with up to 3 dimensions. For instance, for pixel-data with a height (first axis), width (second axis), and r/g/b channels (third axis). The functions concatenate
, stack
and block
provide more general stacking and concatenation operations.
此功能对 3 维的阵列最有意义。例如,对于具有高度 (第一轴),宽度 (第二轴) 和 r/g/b 通道 (第三轴) 的像素数据。函数 concatenate
, stack
and block
提供了更常规的堆叠和连结操作。
horizontally [ˌhɒrɪˈzɒntəli]:adv. 水平地,地平地
sequence [ˈsiːkwəns]:n. 序列,顺序,续发事件 vt. 按顺序排好
concatenation [kənˌkætəˈneɪʃn]:n. 一系列相互关联的事,连结
rebuild [ˌriːˈbɪld]:vt. 重建,改造,重新组装,复原 vi. 重建
1.1 Parameters
tup : sequence of ndarrays
The arrays must have the same shape along all but the second axis, except 1-D arrays which can be any length.
除第二个轴外,所有阵列的形状都必须相同,除了一维阵列可以是任意长度。
1.2 Returns
stacked : ndarray
The array formed by stacking the given arrays.
通过堆叠给定数组形成的数组。
2. Examples
strong@foreverstrong:~$ python3
Python 3.5.2 (default, Oct 8 2019, 13:06:37)
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import numpy as np
>>>
>>> a = np.array((1,2,3))
>>> b = np.array((2,3,4))
>>>
>>> a
array([1, 2, 3])
>>> b
array([2, 3, 4])
>>>
>>> np.hstack((a,b))
array([1, 2, 3, 2, 3, 4])
>>>
>>> a = np.array([[1],[2],[3]])
>>> b = np.array([[2],[3],[4]])
>>>
>>> a
array([[1],
[2],
[3]])
>>> b
array([[2],
[3],
[4]])
>>>
>>> np.hstack((a,b))
array([[1, 2],
[2, 3],
[3, 4]])
>>>
>>> exit()
strong@foreverstrong:~$
3. Examples
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Yongqiang Cheng
from __future__ import absolute_import
from __future__ import print_function
from __future__ import division
import os
import sys
sys.path.append(os.path.dirname(os.path.abspath(__file__)) + '/..')
current_directory = os.path.dirname(os.path.abspath(__file__))
import numpy as np
# import tensorflow as tf
import cv2
# import time
def inference(image_file, current_directory):
img = cv2.imread(image_file, cv2.IMREAD_COLOR)
hstack_img = np.hstack((img, img))
# get dimensions of image
dimensions = hstack_img.shape
# height, width, number of channels in image
height = hstack_img.shape[0]
width = hstack_img.shape[1]
channels = hstack_img.shape[2]
print('Image Dimension : ', dimensions)
print('Image Height : ', height)
print('Image Width : ', width)
print('Number of Channels : ', channels)
tmp_directory = current_directory + "/tmp"
if not os.path.exists(tmp_directory):
os.makedirs(tmp_directory)
cv2.namedWindow("Press ESC on keyboard to exit.", cv2.WINDOW_NORMAL)
# Display the resulting frame
cv2.imshow("Press ESC on keyboard to exit.", hstack_img)
k = cv2.waitKey(0)
if k == 27: # wait for ESC key to exit
pass
elif k == ord('s'): # wait for 's' key to save and exit
image_name = "%s/%s.jpg" % (tmp_directory, "source_image")
cv2.imwrite(image_name, hstack_img, [int(cv2.IMWRITE_JPEG_QUALITY), 100])
# When everything done, release the capture
cv2.destroyAllWindows()
if __name__ == '__main__':
image_file = "./tmp/000505.jpg"
os.environ["CUDA_VISIBLE_DEVICES"] = '0'
print("os.environ['CUDA_VISIBLE_DEVICES']:", os.environ['CUDA_VISIBLE_DEVICES'])
inference(image_file, current_directory)
/usr/bin/python2.7 /home/strong/tensorflow_work/R2CNN_Faster-RCNN_Tensorflow/yongqiang.py --gpu=0
os.environ['CUDA_VISIBLE_DEVICES']: 0
Image Dimension : (1080, 3840, 3)
Image Height : 1080
Image Width : 3840
Number of Channels : 3
Process finished with exit code 0
NumPy hstack function (np.hstack or numpy.hstack)