今天来说说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. toad调用存储过程,存储过程调用sql 类

    1.定义一个sql 类Hello DROP JAVA SOURCE NEWXZXT."Hello"; CREATE OR REPLACE AND RESOLVE JAVA SOUR ...

  2. sdn测量综述

    一 文章名称:A Survey on Security-Aware Measurement in SDN 发表时间:2018 来源:<Security & Communication N ...

  3. f-stack中nginx配置后make出现error: ignoring return value of ‘ftruncate’

    问题 Nginx 配置后 make 出现error: src/os/unix/ngx_process_cycle.c: In function 'ngx_start_worker_processes' ...

  4. 浏览器内多个标签页之间的通信之storage

    在一个标签页里面使用 localStorage.setItem(key,value)添加(修改.删除)内容: 在另一个标签页里面监听 storage 事件. 即可得到 localstorge 存储的值 ...

  5. C# 操作word 模板 值 替换

    1.引用 aspose.words   dll 2.word 使用doc 3.给word 模板中添加要替换位置的 书签 .引用 aspose.words dll .word 使用doc .给word ...

  6. VB错误说明

    1001 800A03E9 内存不足 1002 800A03EA 语法错误 1003 800A03EB 缺少“:” 1005 800A03ED 需要 '(' 1006 800A03EE 需要 ')' ...

  7. Home Assistant系列--之树莓派安装Samba 和 Jupyter Notebook

    1.什么是Samba? Samba是在Linux和UNIX系统上实现SMB协议的一个免费软件,由服务器及客户端程序构成.SMB(Server Messages Block,信息服务块)是一种在局域网上 ...

  8. 网易云易盾CTO朱浩齐:我们是如何用AI赋能内容安全?

    本文由  网易云发布. 5月19日,LiveVideoStack携手网易云易盾,共同打造了“娱乐多媒体开发应用实践”专题,帮助开发者和泛娱乐平台运营人员,提升技术能力,突破难点,拓展思路与视野. 在专 ...

  9. C语言实现可复用栈

    一.思考 最开始写的栈,通过宏来改变元素数据类型,在同一程序中只能用于一种数据类型,想要用于多种数据类型则要复制代码并改名.那么,有没有方法不复制代码就可以用于多种数据类型? 二.基本思路 在我的经验 ...

  10. LIS(单调队列优化 C++ 版)(施工ing)

    #include <iostream> using namespace std; #include <cstdio> ; ,x,stack[MaxN]; int main(){ ...