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实现抓取网页并且解析的实例
Sep 20 Python
Python实现模拟登录及表单提交的方法
Jul 25 Python
Python在Windows和在Linux下调用动态链接库的教程
Aug 18 Python
Python中使用asyncio 封装文件读写
Sep 11 Python
Python向日志输出中添加上下文信息
May 24 Python
python爬虫 批量下载zabbix文档代码实例
Aug 21 Python
Python socket非阻塞模块应用示例
Sep 12 Python
python机器学习实现决策树
Nov 11 Python
python 利用turtle模块画出没有角的方格
Nov 23 Python
pandas抽取行列数据的几种方法
Dec 13 Python
Python 文本滚动播放器的实现代码
Apr 25 Python
Python中Schedule模块使用详解 周期任务神器
Apr 19 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效率,提高php性能的一些方法
2011/03/24 PHP
curl不使用文件存取cookie php使用curl获取cookie示例
2014/01/26 PHP
ThinkPHP权限认证Auth实例详解
2014/07/22 PHP
php常用正则函数实例小结
2016/12/29 PHP
PHP在同一域名下两个不同的项目做独立登录机制详解
2017/09/22 PHP
laravel利用中间件防止未登录用户直接访问后台的方法
2019/09/30 PHP
javascript 获取网页参数系统
2008/07/19 Javascript
用RadioButten或CheckBox实现div的显示与隐藏
2013/09/21 Javascript
Vue.js组件tabs实现选项卡切换效果
2016/12/01 Javascript
JavaScript自定义文本框光标
2017/03/05 Javascript
jquery Ajax实现Select动态添加数据
2017/06/08 jQuery
使用vue构建一个上传图片表单
2017/07/04 Javascript
让你彻底掌握es6 Promise的八段代码
2017/07/26 Javascript
jQuery实现简单的下拉菜单导航功能示例
2017/12/07 jQuery
使用typescript开发angular模块并发布npm包
2018/04/19 Javascript
Vue2实时监听表单变化的示例讲解
2018/08/30 Javascript
vue+axios实现post文件下载
2019/09/25 Javascript
Python中json格式数据的编码与解码方法详解
2016/07/01 Python
梯度下降法介绍及利用Python实现的方法示例
2017/07/12 Python
Python实现的选择排序算法原理与用法实例分析
2017/11/22 Python
Python WSGI的深入理解
2018/08/01 Python
pytorch+lstm实现的pos示例
2020/01/14 Python
Python中文分词库jieba,pkusegwg性能准确度比较
2020/02/11 Python
中国电视购物:快乐购
2017/02/04 全球购物
Made in Design英国:设计家具、照明、家庭装饰和花园家具
2019/09/24 全球购物
俄罗斯香水和化妆品在线商店:Aroma-butik
2020/02/28 全球购物
接口可以包含哪些成员
2012/09/30 面试题
女大学生自我鉴定
2013/12/09 职场文书
施工人员岗位职责
2013/12/12 职场文书
会计系毕业生求职信
2014/05/28 职场文书
如何写早恋检讨书
2014/09/10 职场文书
股东授权委托书
2014/10/15 职场文书
闪闪的红星观后感
2015/06/08 职场文书
党员反邪教心得体会
2016/01/15 职场文书
Win11无法访问设备和打印机 如何解决页面空白
2022/04/09 数码科技
Github 使用python对copilot做些简单使用测试
2022/04/14 Python