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编程中三条常用的技巧
May 11 Python
python学习必备知识汇总
Sep 08 Python
python如何把嵌套列表转变成普通列表
Mar 20 Python
Tensorflow使用tfrecord输入数据格式
Jun 19 Python
在Python中构建增广矩阵的实现方法
Jul 01 Python
为什么黑客都用python(123个黑客必备的Python工具)
Jan 31 Python
Python ATM功能实现代码实例
Mar 19 Python
python异步Web框架sanic的实现
Apr 27 Python
Python3.7下安装pyqt5的方法步骤(图文)
May 12 Python
keras load model时出现Missing Layer错误的解决方式
Jun 11 Python
解决django migrate报错ORA-02000: missing ALWAYS keyword
Jul 02 Python
python实现不同数据库间数据同步功能
Feb 25 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在Web开发领域的优势
2006/10/09 PHP
如何在symfony中导出为CSV文件中的数据
2011/10/06 PHP
php启动时候提示PHP startup的解决方法
2013/05/07 PHP
PHP概率计算函数汇总
2015/09/13 PHP
yii2.0数据库迁移教程【多个数据库同时同步数据】
2016/10/08 PHP
javascript 中String.match()与RegExp.exec()的区别说明
2013/01/10 Javascript
简单易用的倒计时js代码
2014/08/04 Javascript
使用DNode实现php和nodejs之间通信的简单实例
2015/07/06 NodeJs
jQuery Easyui Datagrid实现单行的上移下移及保存移动的结果
2016/08/15 Javascript
深入理解Angular4订阅(Subscribe)与取消
2017/11/22 Javascript
Node.js中,在cmd界面,进入退出Node.js运行环境的方法
2018/05/12 Javascript
vue将毫秒数转化为正常日期格式的实例
2018/09/16 Javascript
extract-text-webpack-plugin用法详解
2019/02/14 Javascript
vue element中axios下载文件(后端Python)
2019/05/10 Javascript
vuex入门最详细整理
2020/03/04 Javascript
基于vue3.0.1beta搭建仿京东的电商H5项目
2020/05/06 Javascript
Vue中使用JsonView来展示Json树的实例代码
2020/11/16 Javascript
[13:39]2014 DOTA2华西杯精英邀请赛 5 25 NewBee VS DK第一场
2014/05/26 DOTA
python getopt详解及简单实例
2016/12/30 Python
深入理解python对json的操作总结
2017/01/05 Python
python之验证码生成(gvcode与captcha)
2019/01/02 Python
Python Image模块基本图像处理操作小结
2019/04/13 Python
Pytorch Tensor基本数学运算详解
2019/12/30 Python
python高级特性简介
2020/08/13 Python
Python中return函数返回值实例用法
2020/11/19 Python
CSS3实现超酷的黑猫警长首页
2016/04/26 HTML / CSS
html5 sessionStorage会话存储_动力节点Java学院整理
2017/07/06 HTML / CSS
美国婴童服装市场上的领先品牌:Carter’s
2018/02/08 全球购物
JAVA软件工程师测试题
2014/07/25 面试题
公司年会晚宴演讲稿
2014/01/06 职场文书
药学专业学生的自我评价分享
2014/02/06 职场文书
小区文明倡议书
2014/05/16 职场文书
父亲节活动策划方案
2014/08/24 职场文书
党的群众路线领导班子整改方案
2014/09/27 职场文书
党的群众路线教育实践活动制度建设计划
2014/11/03 职场文书
人力资源部工作计划
2019/05/14 职场文书