今天来说说im2col和col2im函数,这是MATLAB中两个内置函数,经常用于数字图像处理中。其中im2col函数在《MATLAB中的im2col函数》一文中已经进行了简单的介绍。

一般来说:

  1. 如是将图像分割成块的时候用的im2col参数为'distinct',那么用col2im函数时参数也是'distinct',即可将转换后的数组复原。
  2. 如果将图像分割成块的时候用的im2col参数为'sliding',我目前还不知道MATLAB中使用内置函数是如何复原的。

今天,来看看Python中是如何实现这两个函数的(sliding类型)。

  1. 对于im2col的实现,我们沿着原始矩阵逐行计算,将得到的新的子矩阵展开成列,放置在列块矩阵中。
  2. 对于col2im的实现,我们沿着列块矩阵逐行计算,将得到的行展成子矩阵,然后将子矩阵放置在最终结果对应的位置(每次当前值进行相加),同时记录每个位置的值放置的次数。最后,将当前位置的值除以放置的次数,即可得到结果(原始矩阵)。
def im2col(mtx, block_size):
mtx_shape = mtx.shape
sx = mtx_shape[0] - block_size[0] + 1
sy = mtx_shape[1] - block_size[1] + 1
# 如果设A为m×n的,对于[p q]的块划分,最后矩阵的行数为p×q,列数为(m−p+1)×(n−q+1)。
result = np.empty((block_size[0] * block_size[1], sx * sy))
# 沿着行移动,所以先保持列(i)不动,沿着行(j)走
for i in range(sy):
for j in range(sx):
result[:, i * sx + j] = mtx[j:j + block_size[0], i:i + block_size[1]].ravel(order='F')
return result def col2im(mtx, image_size, block_size):
p, q = block_size
sx = image_size[0] - p + 1
sy = image_size[1] - q + 1
result = np.zeros(image_size)
weight = np.zeros(image_size) # weight记录每个单元格的数字重复加了多少遍
col = 0
# 沿着行移动,所以先保持列(i)不动,沿着行(j)走
for i in range(sy):
for j in range(sx):
result[j:j + p, i:i + q] += mtx[:, col].reshape(block_size, order='F')
weight[j:j + p, i:i + q] += np.ones(block_size)
col += 1
return result / weight

测试代码:

if __name__ == '__main__':
mtx = np.around(np.random.rand(5, 5) * 100)
print('原始矩阵:')
print(mtx) a1 = im2col(mtx, (2, 3))
print('im2col(分块大小2x3):')
print(a1)
b1 = col2im(a1, (5, 5), (2, 3))
print('col2im复原:')
print(b1) a2 = im2col(mtx, (3, 3))
print('im2col(分块大小3x3):')
print(a2)
b2 = col2im(a2, (5, 5), (3, 3))
print('col2im复原:')
print(b2)

运行结果:

原始矩阵:
[[ 48. 38. 38. 59. 38.]
[ 38. 11. 25. 52. 44.]
[ 60. 69. 49. 93. 66.]
[ 88. 8. 47. 14. 47.]
[ 96. 37. 56. 86. 54.]]
im2col(分块大小2x3):
[[ 48. 38. 60. 88. 38. 11. 69. 8. 38. 25. 49. 47.]
[ 38. 60. 88. 96. 11. 69. 8. 37. 25. 49. 47. 56.]
[ 38. 11. 69. 8. 38. 25. 49. 47. 59. 52. 93. 14.]
[ 11. 69. 8. 37. 25. 49. 47. 56. 52. 93. 14. 86.]
[ 38. 25. 49. 47. 59. 52. 93. 14. 38. 44. 66. 47.]
[ 25. 49. 47. 56. 52. 93. 14. 86. 44. 66. 47. 54.]]
col2im复原:
[[ 48. 38. 38. 59. 38.]
[ 38. 11. 25. 52. 44.]
[ 60. 69. 49. 93. 66.]
[ 88. 8. 47. 14. 47.]
[ 96. 37. 56. 86. 54.]]
im2col(分块大小3x3):
[[ 48. 38. 60. 38. 11. 69. 38. 25. 49.]
[ 38. 60. 88. 11. 69. 8. 25. 49. 47.]
[ 60. 88. 96. 69. 8. 37. 49. 47. 56.]
[ 38. 11. 69. 38. 25. 49. 59. 52. 93.]
[ 11. 69. 8. 25. 49. 47. 52. 93. 14.]
[ 69. 8. 37. 49. 47. 56. 93. 14. 86.]
[ 38. 25. 49. 59. 52. 93. 38. 44. 66.]
[ 25. 49. 47. 52. 93. 14. 44. 66. 47.]
[ 49. 47. 56. 93. 14. 86. 66. 47. 54.]]
col2im复原:
[[ 48. 38. 38. 59. 38.]
[ 38. 11. 25. 52. 44.]
[ 60. 69. 49. 93. 66.]
[ 88. 8. 47. 14. 47.]
[ 96. 37. 56. 86. 54.]]

Python中如何实现im2col和col2im函数(sliding类型)的更多相关文章

  1. Python实现im2col和col2im函数

    今天来说说im2col和col2im函数,这是MATLAB中两个内置函数,经常用于数字图像处理中.其中im2col函数在<MATLAB中的im2col函数>一文中已经进行了简单的介绍. 一 ...

  2. 【python】dir(__builtins__)查看python中所用BIF(内置函数)

    dir(__builtins__)查看python中所用BIF(内置函数)

  3. python中lambda,map,reduce,filter,zip函数

    函数式编程 函数式编程(Functional Programming)或者函数程序设计,又称泛函编程,是一种编程范型,它将计算机运算视为数学上的函数计算,并且避免使用程序状态以及易变对象.简单来讲,函 ...

  4. 【转】Python 中map、reduce、filter函数

    转自:http://www.blogjava.net/vagasnail/articles/301140.html?opt=admin 介绍下Python 中 map,reduce,和filter 内 ...

  5. python中列表和字典常用方法和函数

    Python列表函数&方法 Python包含以下函数: 序号 函数 1 cmp(list1, list2)比较两个列表的元素 2 len(list)列表元素个数 3 max(list)返回列表 ...

  6. python中通过字符串名来调用函数

    强调:eval()函数功能虽然强大,但是也很危险,这个方法需要慎重使用. 利用python中的内置函数 eval() ,函数说明: def eval(*args, **kwargs): # real ...

  7. python中的文件读写(open()函数、with open('file_directory','r') as f:、read()函数等)

    python中也有文件读写,通过调用内置的读写函数.可以完成文件的打开/关闭.读.写入.追加等功能. open()函数 open()函数为python中的打开文件函数,使用方式为: f = open( ...

  8. python中的作用域以及内置函数globals()-全局变量、locals()-局部变量

    在python中,函数会创建一个自己的作用域,也称为为命名空间.这意味着在函数内部访问某个变量时,函数会优先在自己的命名空间中寻找. 通过内置函数globals()返回的是python解释器能知道的变 ...

  9. Python中几个必须知道的函数

    Python中自带了几个比较有意思的函数,一般在面试或者笔试基础的时候会问到,其中3个就是map.filter.reduce函数. 1.map(function, iterable) 它第一个要传的元 ...

随机推荐

  1. 【题解】洛谷P1373 小a和uim之大逃离(坐标DP)

    次元传送门:洛谷P1373 思路 设f[i][j][t][1/0]表示走到(i,j)时 小a减去uim的差值为t 当前是小a取(0) uim取(1) 那么转移就很明显了 f[i][j][t][]=(f ...

  2. nRF5 SDK for Mesh(六) BLE MESH 的 基础概念

    Basic Bluetooth Mesh concepts The Bluetooth Mesh is a profile specification developed and published ...

  3. ibatis运行的SQL语句的输出——通过配置log4j

    将ibatis 的log4j运行级别调到DEBUG可以在控制台打印出ibatis运行的sql语句 ### 设置Logger输出级别和输出目的地 ###log4j.rootLogger=debug,st ...

  4. sql中查询某月某年内的记录

    假设表结构:用户名,日期,上班时间,下班时间.8月份记录:select * from 表名 where month(日期)=8 and 用户名 = '小张'8月份迟到早退次数:select sum(i ...

  5. 底层文件I/O操作中read()函数的缓存问题

    最近在学习Linux过程中看到文件I/O操作这里时,文件I/O操作的系统调用涉及的5个函数:open(),read(),write(),lseek(),close().在一开始就阐明这些函数的特点是不 ...

  6. vux使用过程中遇到的问题

    1.使用confirm.prompt组件时,ios下点击输入框很难获得焦点 解决思路:使用confirm.show方法,自定义content内容,show方法里面设置input的focus方法 let ...

  7. HTML标签速记整理W3C

    标题 <h1>段落<p>链接< href="">图像<img src="">自关闭元素,不需要结束标记换行标志& ...

  8. c++读取ini的Section节名

    // ConsoleApplication1.cpp : 定义控制台应用程序的入口点.// #include "stdafx.h"#include "iostream&q ...

  9. 大数据学习:Spark是什么,如何用Spark进行数据分析

    给大家分享一下Spark是什么?如何用Spark进行数据分析,对大数据感兴趣的小伙伴就随着小编一起来了解一下吧.     大数据在线学习 什么是Apache Spark? Apache Spark是一 ...

  10. ruby rspec+jenkins+ci_report持续集成生成junit测试报告

    1.加载ci_report gem install ci_reporter_rspec 2.给测试工程编写rakefile require 'ci/reporter/rake/rspec' requi ...