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 Django批量导入不重复数据
Mar 25 Python
Python增量循环删除MySQL表数据的方法
Sep 23 Python
python数据结构链表之单向链表(实例讲解)
Jul 25 Python
Python数据拟合与广义线性回归算法学习
Dec 22 Python
Python读写docx文件的方法
May 08 Python
使用pytorch进行图像的顺序读取方法
Jul 27 Python
使用python采集脚本之家电子书资源并自动下载到本地的实例脚本
Oct 23 Python
Python使用itchat模块实现简单的微信控制电脑功能示例
Aug 26 Python
python多进程(加入进程池)操作常见案例
Oct 21 Python
Python利用全连接神经网络求解MNIST问题详解
Jan 14 Python
解决python图像处理图像赋值后变为白色的问题
Jun 04 Python
Python中requests做接口测试的方法
May 30 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
不用GD库生成当前时间的PNG格式图象的程序
2006/10/09 PHP
PHP中MVC模式的模板引擎开发经验分享
2011/03/23 PHP
Yii清理缓存的方法
2016/01/06 PHP
js 数组实现一个类似ruby的迭代器
2009/10/27 Javascript
Extjs4 GridPanel的主要配置参数详细介绍
2013/04/18 Javascript
javascript 小数取整简单实现方式
2014/05/30 Javascript
Bootstrap项目实战之首页内容介绍(全)
2016/04/25 Javascript
Jquery ui datepicker设置日期范围,如只能隔3天【实现代码】
2016/05/04 Javascript
javascript设计模式Constructor(构造器)模式
2016/08/19 Javascript
js实现控制textarea输入字符串的个数,鼠标按下抬起判断输入字符数
2016/10/25 Javascript
使用Ajax与服务器(JSON)通信实例
2016/11/04 Javascript
jQuery Password Validation密码验证
2016/12/30 Javascript
详解JS获取HTML DOM元素的8种方法
2017/06/17 Javascript
浅谈微信小程序之官方UI框架we-ui使用教程
2018/08/20 Javascript
微信小程序实现随机验证码功能
2018/12/20 Javascript
使用uni-app开发微信小程序的实现
2019/12/13 Javascript
Nodejs + Websocket 指定发送及群聊的实现
2020/01/09 NodeJs
js实现鼠标点击飘爱心效果
2020/08/19 Javascript
深入理解Python 代码优化详解
2014/10/27 Python
Python中标准库OS的常用方法总结大全
2017/07/19 Python
Python查找第n个子串的技巧分享
2018/06/27 Python
对python中数据集划分函数StratifiedShuffleSplit的使用详解
2018/12/11 Python
Python+OpenCV实现图像的全景拼接
2020/03/05 Python
利用pandas向一个csv文件追加写入数据的实现示例
2020/04/23 Python
Python键鼠操作自动化库PyAutoGUI简介(小结)
2020/05/17 Python
戴尔加拿大官网:Dell加拿大
2016/09/17 全球购物
联想新加坡官方网站:Lenovo Singapore
2017/10/24 全球购物
主键(Primary Key)约束和唯一性(UNIQUE)约束的区别
2013/05/29 面试题
介绍一下Mysql的存储引擎
2015/02/12 面试题
大学生水文观测实习自我鉴定
2013/09/29 职场文书
工作失误检讨书范文
2015/01/26 职场文书
工商局个人工作总结
2015/03/03 职场文书
党支部审查意见
2015/06/02 职场文书
2016初一新生军训心得体会
2016/01/11 职场文书
品牌形象定位,全面分析
2019/07/23 职场文书
Python中的np.argmin()和np.argmax()函数用法
2021/06/02 Python