python numpy 显示图像阵列的实例

每次要显示图像阵列的时候,使用自带的 matplotlib 或者cv2 都要设置一大堆东西,subplot,fig等等,突然想起 可以利用numpy 的htstack() 和 vstack() 将图片对接起来组成一张新的图片。因此写了写了下面的函数。做了部分注释,一些比较绕的地方可以自行体会。

大致流程包括:

1、输入图像列表 img_list

2、show_type : 最终的显示方式,输入为行数列数 (例如 show_type=22 ,则最终显示图片为两行两列)

3、basic_shape, 图片resize的尺寸。

def image_show( img_list, show_type, basic_size=[300,500]):
 '''
  img_list contains the images that need to be stitched,
  the show_typ contains the final shape of the stitched one, ie, 12 for 1 row 2 cols.
  basic_size : all input image need to be reshaped first. 

 '''
 # reshap row and col number.
 n_row, n_col = basic_size
 #print n_row,n_col

 # num of pixels need to be filled vertically and horizontally.
 h_filling = 10
 v_filling = 10

 # image resize.
 resize_list=[]
 for i in img_list:
  temp_img = cv2.resize( i, ( n_col, n_row ), interpolation = cv2. INTER_CUBIC )
  resize_list.append( temp_img )

 # resolve the final stitched image 's shape.
 n_row_img, n_col_img = show_type/10, show_type%10
 #print n_row_img, n_col_img

 # the blank_img and the image need to be filled should be defined firstly.
 blank_img= np.ones([n_row,n_col])*255
 blank_img= np.array( blank_img, np.uint8 )
 v_img= np.array( np.ones([n_row,v_filling])*255, np.uint8)
 h_img= np.array( np.ones ([ h_filling, n_col_img*n_col+(n_col_img-1)*h_filling])*255, np.uint8)

 # images in the image list should be dispatched into different sub-list
 # in each sub list the images will be connected horizontally.
 recombination_list=[]
 temp_list=[]
 n_list= len(resize_list)
 for index, i in enumerate ( xrange (n_list)):
  if index!= 0 and index % n_col_img==0 :
   recombination_list.append(temp_list)
   temp_list = []
   if len(resize_list)> n_col_img:
    pass
   else:
    recombination_list.append(resize_list)
    break
  temp_list.append( resize_list.pop(0))
 if n_list== n_col_img:
  recombination_list.append(temp_list)
 #print len(temp_list)
 #print temp_list

 # stack the images horizontally.
 h_temp=[]
 for i in recombination_list:
  #print len(i)
  if len(i)==n_col_img:

   temp_new_i=[ [j,v_img] if index+1 != len(i) else j for index, j in enumerate (i) ]
   new_i=[ j for i in temp_new_i[:-1] for j in i ]
   new_i.append( temp_new_i[-1])
   h_temp.append(np.hstack(new_i))
  else:

   add_n= n_col_img - len(i)
   for k in range(add_n):
    i.append(blank_img)

   temp_new_i=[ [j,v_img] if index+1 != len(i) else j for index, j in enumerate (i) ]
   new_i=[ j for i in temp_new_i[:-1] for j in i ]
   new_i.append( temp_new_i[-1])

   h_temp.append(np.hstack(new_i))

 #print len(h_temp)
 #print h_temp

 temp_full_img= [ [j, h_img ] if index+1 != len(h_temp) else j for index, j in enumerate(h_temp) ]
 if len(temp_full_img) > 2:
  full_img= [ j for i in temp_full_img[:-1] for j in i ]
  full_img.append(temp_full_img[-1])
 else:
  full_img= [ j for i in temp_full_img for j in i ]
  #full_img.append(temp_full_img[-1])

 if len(full_img)>1:
  return np.vstack( full_img)
 else:
  return full_img

最终输入情况和结果如下图:

第一组结果图:自行看输入

第二组结果图。

以上这篇python numpy 显示图像阵列的实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持我们。

(0)

相关推荐

  • 浅析python中numpy包中的argsort函数的使用

    概述 argsort()函数在模块numpy.core.fromnumeric中. 在python中排序数组,或者获取排序顺序的时候,我们常常使用numpy包的argsort函数来完成. 如下图所示,是使用python获取到数组中的排序的顺序. data=numpy.array([1,2,3,4,5]) datasort=numpy.argsort(data) datasort Out[39]: array([0, 1, 2, 3, 4], dtype=int64) data Out[40]:

  • Python实现曲线拟合操作示例【基于numpy,scipy,matplotlib库】

    本文实例讲述了Python实现曲线拟合操作.分享给大家供大家参考,具体如下: 这两天学习了用python来拟合曲线. 一.环境配置 本人比较比较懒,所以下载的全部是exe文件来安装,安装按照顺利来安装.自动会找到python的安装路径,一直点下一步就行.还有其他的两种安装方式:一种是解压,一种是pip.我没有尝试,就不乱说八道了. 没有ArcGIS 环境的,可以不看下面这段话了. 在配置环境时遇见一个小波折,就是原先电脑装过ArcGIS10.2 ,所以其会默认安装python2.7,而且pyth

  • python numpy 一维数组转变为多维数组的实例

    如下所示: import numpy new_list = [i for i in range(9)] numpy.array(new_list).reshape(3,3) 借助numpy库: 以上这篇python numpy 一维数组转变为多维数组的实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持我们.

  • python 借助numpy保存数据为csv格式的实现方法

    借助numpy可以把数组或者矩阵保存为csv文件,也可以吧csv文件整体读取为一个数组或矩阵. 1.csv ==> matrix import numpy my_matrix = numpy.loadtxt(open("D:\\test.csv","rb"), delimiter=",", skiprows=0) 2.matrix ==> csv import numpy numpy.savetxt("new.csv&quo

  • Python中的Numpy矩阵操作

    Numpy 通过观察Python的自有数据类型,我们可以发现Python原生并不提供多维数组的操作,那么为了处理矩阵,就需要使用第三方提供的相关的包. NumPy 是一个非常优秀的提供矩阵操作的包.NumPy的主要目标,就是提供多维数组,从而实现矩阵操作. NumPy's main object is the homogeneous multidimensional array. It is a table of elements (usually numbers), all of the sa

  • Python使用numpy产生正态分布随机数的向量或矩阵操作示例

    本文实例讲述了Python使用numpy产生正态分布随机数的向量或矩阵操作.分享给大家供大家参考,具体如下: 简单来说,正态分布(Normal distribution)又名高斯分布(Gaussian distribution),是一个在数学.物理及工程等领域都非常重要的概率分布,在统计学的许多方面有着重大的影响力.一般的正态分布可以通过标准正态分布配合数学期望向量和协方差矩阵得到.如下代码,可以得到满足一维和二维正态分布的样本. 示例1(一维正态分布): # coding=utf-8 '''

  • Python中Numpy包的安装与使用方法简明教程

    本文实例讲述了Python中Numpy包的安装与使用方法.分享给大家供大家参考,具体如下: Numpy包的安装 准备工作 1. Python安装 2. pip安装(如使用pip安装命令:pip install numpy) 3. 将pip所在的文件夹添加到环境变量path路径中 4. 下载相应的Numpy安装包,.whl格式.下载链接. 以上准备工作准备完毕之后,进行Numpy安装,先进入whl安装包的存放目录.比如在C盘: cd C:\ 再使用命令行安装: pip install numpy文

  • 详解windows python3.7安装numpy问题的解决方法

    我的是win7的系统,去python官网下载python3.7安装 CMD  #打开命令窗口 pip install numpy #在cmd中输入 提示 需要c++14.0, 解决办法: 1, 进入https://www.lfd.uci.edu/~gohlke/pythonlibs/#wordcloud 我的是win10的系统,去python官网下载python3.7安装 CMD  #打开命令窗口 pip install numpy #在cmd中输入 提示 需要c++14.0, 解决办法: 1,

  • python+numpy+matplotalib实现梯度下降法

    这个阶段一直在做和梯度一类算法相关的东西,索性在这儿做个汇总, 一.算法论述 梯度下降法(gradient  descent)别名最速下降法(曾经我以为这是两个不同的算法-.-),是用来求解无约束最优化问题的一种常用算法.下面以求解线性回归为题来叙述: 设:一般的线性回归方程(拟合函数)为:(其中的值为1) 则这一组向量参数选择的好与坏就需要一个机制来评估,据此我们提出了其损失函数为(选择均方误差): 我们现在的目的就是使得损失函数取得最小值,即目标函数为: 如果的值取到了0,意味着我们构造出了

  • 详解python中Numpy的属性与创建矩阵

    ndarray.ndim:维度 ndarray.shape:形状 ndarray.size:元素个数 ndarray.dtype:元素数据类型 ndarray.itemsize:字节大小 创建数组: a = np.array([2,23,4]) # list 1d print(a) # [2 23 4] 指定数据类型: a = np.array([2,23,4],dtype=np.int) print(a.dtype) # int 64 dtype可以指定的类型有int32,float,floa

随机推荐