python numpy 显示图像阵列的实例


Posted in Python onJuly 02, 2018

每次要显示图像阵列的时候,使用自带的 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 显示图像阵列的实例

第二组结果图。

python numpy 显示图像阵列的实例

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

Python 相关文章推荐
wxPython框架类和面板类的使用实例
Sep 28 Python
python 中字典嵌套列表的方法
Jul 03 Python
python 实现数字字符串左侧补零的方法
Dec 04 Python
Python批量修改图片分辨率的实例代码
Jul 04 Python
windows上安装python3教程以及环境变量配置详解
Jul 18 Python
Python目录和文件处理总结详解
Sep 02 Python
pandas将多个dataframe以多个sheet的形式保存到一个excel文件中
Oct 10 Python
详解Python 重学requests发起请求的基本方式
Feb 07 Python
Selenium元素定位的30种方式(史上最全)
May 11 Python
Django Model中字段(field)的各种选项说明
May 19 Python
python的数学算法函数及公式用法
Nov 18 Python
python 常用的异步框架汇总整理
Jun 18 Python
Python实现图片拼接的代码
Jul 02 #Python
python远程连接服务器MySQL数据库
Jul 02 #Python
对Python 数组的切片操作详解
Jul 02 #Python
python读取LMDB中图像的方法
Jul 02 #Python
python读写LMDB文件的方法
Jul 02 #Python
对numpy中的数组条件筛选功能详解
Jul 02 #Python
python matlibplot绘制多条曲线图
Feb 19 #Python
You might like
发款php蜘蛛统计插件只要有mysql就可用
2010/10/12 PHP
解析PHP生成静态html文件的三种方法
2013/06/18 PHP
解析PHP中的unset究竟会不会释放内存
2013/07/18 PHP
PHP限制页面只能在微信自带浏览器访问的代码
2014/01/15 PHP
PHP实现利用MySQL保存session的方法
2014/08/23 PHP
php实现的支持imagemagick及gd库两种处理的缩略图生成类
2014/09/23 PHP
PHP魔术方法使用方法汇总
2016/02/14 PHP
PHP有序表查找之二分查找(折半查找)算法示例
2018/02/09 PHP
gearman管理工具GearmanManager的安装与php使用方法示例
2020/02/27 PHP
safari,opera嵌入iframe页面cookie读取问题解决方法
2010/06/23 Javascript
基于jquery的blockui插件显示弹出层
2011/04/14 Javascript
js分解url参数(面向对象-极简主义法应用)
2012/08/09 Javascript
JQuery实现当鼠标停留在某区域3秒后自动执行
2014/09/09 Javascript
jquery实现简单的二级导航下拉菜单效果
2015/09/07 Javascript
BootStrap响应式导航条实例介绍
2016/05/06 Javascript
javascript中的replace函数(带注释demo)
2018/01/07 Javascript
vue 实现复制内容到粘贴板clipboard的方法
2018/03/17 Javascript
mpvue小程序循环动画开启暂停的实现方法
2019/05/15 Javascript
jQuery实现form表单基于ajax无刷新提交方法实例代码
2019/11/04 jQuery
用Python制作检测Linux运行信息的工具的教程
2015/04/01 Python
Python数据结构之双向链表的定义与使用方法示例
2018/01/16 Python
python自动登录12306并自动点击验证码完成登录的实现源代码
2018/04/25 Python
python实现简单的文字识别
2018/11/27 Python
python图的深度优先和广度优先算法实例分析
2019/10/26 Python
pytorch中nn.Conv1d的用法详解
2019/12/31 Python
python实现在内存中读写str和二进制数据代码
2020/04/24 Python
Python web如何在IIS发布应用过程解析
2020/05/27 Python
python中用ctypes模拟点击的实例讲解
2020/11/26 Python
快速创建 HTML5 Canvas 电信网络拓扑图的示例代码
2018/03/21 HTML / CSS
个人自我鉴定写法
2013/11/30 职场文书
毕业生就业自荐书
2013/12/15 职场文书
我爱我家教学反思
2014/05/01 职场文书
国旗下的讲话演讲稿
2014/05/08 职场文书
Pytorch 中net.train 和 net.eval的使用说明
2021/05/22 Python
python中的None与NULL用法说明
2021/05/25 Python
Android超详细讲解组件ScrollView的使用
2022/03/31 Java/Android