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 相关文章推荐
详解Django中的form库的使用
Jul 18 Python
详解Django框架中的视图级缓存
Jul 23 Python
Python操作使用MySQL数据库的实例代码
May 25 Python
Python实现判断给定列表是否有重复元素的方法
Apr 11 Python
利用python计算windows全盘文件md5值的脚本
Jul 27 Python
python图片指定区域替换img.paste函数的使用
Apr 09 Python
python实现逢七拍腿小游戏的思路详解
May 26 Python
python实现在线翻译
Jun 18 Python
django美化后台django-suit的安装配置操作
Jul 12 Python
python七种方法判断字符串是否包含子串
Aug 18 Python
基于python的opencv图像处理实现对斑马线的检测示例
Nov 29 Python
python编程实现清理微信重复缓存文件
Nov 01 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
重料打造自己的“宝马”---第三代
2021/03/02 无线电
如何对PHP程序中的常见漏洞进行攻击
2006/10/09 PHP
php print EOF实现方法
2009/05/21 PHP
PHP简单实现断点续传下载的方法
2015/09/25 PHP
php根据日期或时间戳获取星座信息和生肖等信息
2015/10/20 PHP
微信公众号支付之坑:调用支付jsapi缺少参数 timeStamp等错误解决方法
2016/01/12 PHP
简单解决微信文章图片防盗链问题
2016/12/17 PHP
Jquery简单分页实现方法
2015/07/24 Javascript
JS+Canvas 实现下雨下雪效果
2016/05/18 Javascript
Vue 单文件中的数据传递示例
2017/03/21 Javascript
vue过渡和animate.css结合使用详解
2017/06/14 Javascript
详解Axios统一错误处理与后置
2018/09/26 Javascript
微信小程序跳转到其他网页(外部链接)的实现方法
2019/09/20 Javascript
详解微信小程序「渲染层网络层错误」的解决方法
2021/01/06 Javascript
[01:18]PWL开团时刻DAY4——圣剑与抢盾
2020/11/03 DOTA
[10:05]DOTA2-DPC中国联赛 正赛 iG vs PSG.LGD 选手采访
2021/03/11 DOTA
尝试使用Python多线程抓取代理服务器IP地址的示例
2015/11/09 Python
深入浅析python继承问题
2016/05/29 Python
Python机器学习logistic回归代码解析
2018/01/17 Python
python 2.7.13 安装配置方法图文教程
2018/09/18 Python
django开发post接口简单案例,获取参数值的方法
2018/12/11 Python
pycharm配置pyqt5-tools开发环境的方法步骤
2019/02/11 Python
Python列表原理与用法详解【创建、元素增加、删除、访问、计数、切片、遍历等】
2019/10/30 Python
python画蝴蝶曲线图的实例
2019/11/21 Python
Python线程障碍对象Barrier原理详解
2019/12/02 Python
解决PyCharm不在run输出运行结果而不是再Console里输出的问题
2020/09/21 Python
PHP如何与mysql建立链接
2013/05/05 面试题
学习决心书范文
2014/03/11 职场文书
小学生期末评语大全
2014/04/21 职场文书
文明工地标语
2014/06/16 职场文书
债务纠纷委托书
2014/08/30 职场文书
2014院党委领导班子对照检查材料思想汇报
2014/09/24 职场文书
热血教师观后感
2015/06/10 职场文书
毕业实习证明范本
2015/06/16 职场文书
会计专业自荐信范文
2019/05/22 职场文书
Python可视化动图组件ipyvizzu绘制惊艳的可视化动图
2022/04/21 Python