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 相关文章推荐
跟老齐学Python之不要红头文件(1)
Sep 28 Python
Python爬取读者并制作成PDF
Mar 10 Python
python多进程提取处理大量文本的关键词方法
Jun 05 Python
对python:print打印时加u的含义详解
Dec 15 Python
python根据文章标题内容自动生成摘要的实例
Feb 21 Python
python实现对图片进行旋转,放缩,裁剪的功能
Aug 07 Python
如何修复使用 Python ORM 工具 SQLAlchemy 时的常见陷阱
Nov 19 Python
Pytorch实现各种2d卷积示例
Dec 30 Python
python如何将两张图片生成为全景图片
Mar 05 Python
Pyqt助手安装PyQt5帮助文档过程图解
Nov 20 Python
python 通过pip freeze、dowload打离线包及自动安装的过程详解(适用于保密的离线环境
Dec 14 Python
详解用selenium来下载小姐姐图片并保存
Jan 26 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读取csv实现csv文件下载功能
2013/12/18 PHP
php合并数组中相同元素的方法
2014/11/13 PHP
Java和PHP在Web开发方面对比分析
2015/03/01 PHP
PHP使用适合阅读的格式显示文件大小的方法
2015/03/05 PHP
PHP迭代与递归实现无限级分类
2017/08/28 PHP
Mac下关于PHP环境和扩展的安装详解
2019/10/17 PHP
CheckBox 如何实现全选?
2006/06/23 Javascript
新浪刚打开页面出来的全屏广告代码
2007/04/02 Javascript
在JavaScript中获取请求的URL参数
2010/12/22 Javascript
js 实现菜单上下显示附效果图
2013/11/21 Javascript
使用jquery+CSS实现控制打印样式
2014/12/31 Javascript
jQuery实现的简单提示信息插件
2015/12/08 Javascript
js实现根据身份证号自动生成出生日期
2015/12/15 Javascript
使用jQuery Mobile框架开发移动端Web App的入门教程
2016/05/17 Javascript
js基础之DOM中document对象的常用属性方法详解
2016/10/28 Javascript
基于JS实现移动端向左滑动出现删除按钮功能
2017/02/22 Javascript
9种改善AngularJS性能的方法
2017/11/28 Javascript
浅析node应用的timing-attack安全漏洞
2018/02/28 Javascript
vue.js数据绑定操作详解
2018/04/23 Javascript
ng-repeat指令在迭代对象时的去重方法
2018/10/02 Javascript
Vue实现简易计算器
2020/02/25 Javascript
python 多进程并行编程 ProcessPoolExecutor的实现
2019/10/11 Python
Python opencv相机标定实现原理及步骤详解
2020/04/09 Python
Python decimal模块使用方法详解
2020/06/08 Python
python实现定时发送邮件
2020/12/23 Python
Python读取pdf表格写入excel的方法
2021/01/22 Python
linux面试题参考答案(4)
2014/09/21 面试题
linux面试题参考答案(6)
2016/06/23 面试题
献爱心倡议书
2014/04/14 职场文书
计算机专业自荐信范文
2015/03/26 职场文书
小学运动会加油稿
2015/07/22 职场文书
2016廉洁从政心得体会
2016/01/19 职场文书
Python利器openpyxl之操作excel表格
2021/04/17 Python
详解Javascript实践中的命令模式
2021/05/05 Javascript
python opencv通过按键采集图片源码
2021/05/20 Python
MySql如何将查询的出来的字段进行转换
2022/06/14 MySQL