numpy.hstack

本文详细介绍了NumPy库中的hstack函数,该函数用于水平(按列)顺序堆叠数组,适用于最多三维的数组,特别适合处理像素数据。文章通过实例展示了如何使用hstack函数,并解释了其参数和返回值。

numpy.hstack

https://docs.scipy.org/doc/numpy/reference/generated/numpy.hstack.html

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)

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Yongqiang Cheng

梦想不是浮躁,而是沉淀和积累。

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值