认知与分析分享 http://blog.sciencenet.cn/u/AlecXu 为创造价值而奋斗

博文

对图片进行拆分并随机排列的快速算法(MATLAB)

已有 7809 次阅读 2014-2-18 00:44 |个人分类:程序分享|系统分类:科研笔记| 图像拆分随机排布

   我手头快完成的一个改进荧光显微镜图像处理的研究项目需要用到拆分一个图片,并对其进行随机排列的功能。之前我写的程序虽然能够得到想要的结果,但是运行太慢,非常耗时。原方法是计算图片被拆分成的各个方格的位置,然后随机地挑选一个小方格,再随机挑选另外一个方格,并对两者进行替换,再使用循环语句将所有的方格都依次替换一遍。图片如果很大的话,这个替换就变得极其耗时。最近我想出来一个可以将运行速度数倍提高的方法,这个新方法采取的思路是先把代表图像的矩阵转换成所需大小的元胞数组(cell),之后随机生成一个打乱了顺序的,代表各个元胞数组的序列,再按照新的序列将元胞数组重新转换成矩阵即可。我写的这个函数,如果有需要的话也可以按照自定义的顺序进行排列,否则默认的情况(在不给出第三个参数的情况下),是进行随机排布。

   秉着共享原则,我把我写好的MATLAB函数分享给大家,希望对需要用到这个功能的人有帮助.

function [xre] = imrand(x,bsize,bid)

%imrand function is used to break original matrix into a number of blocks
%and re-position the blocks.
%   x is the original image. bsize specifies the size of a block, which is
%   a two-element vector. "imrand" is similar to "imScramble" and the  
%   difference is that in this function, the randomization is done by first
%   break down the original matrix representing the image to smaller cells
%   of size "bsize" and randomize the index of each cell. Then the cells
%   are transformed to a matrix. "bid" is a vector with a length of the  
%   total number of divided blocks, and it contains information regarding
%   the ordering of each cell.

[nr,nc]=size(x);
xs = mat2cell(x,bsize(1)*ones(1,nr/bsize(1)),bsize(2)*ones(1,nc/bsize(2)));
nb = nr*nc/bsize(1)/bsize(2);
if nargin <3
   bid = randperm(nb);
end
xre = xs;
for i=1:nb
   xre{i}=xs{bid(i)};
end
xre = cell2mat(xre);
end

效果图




https://blog.sciencenet.cn/blog-3503-768443.html

上一篇:年轻学生现实一点并不是件太坏的事情
下一篇:米歇尔访华清场是USA政府要求的,不是中国政府的特别服务
收藏 IP: 213.103.216.*| 热度|

2 王春艳 严璘璘

该博文允许注册用户评论 请点击登录 评论 (3 个评论)

数据加载中...
扫一扫,分享此博文

Archiver|手机版|科学网 ( 京ICP备07017567号-12 )

GMT+8, 2024-5-10 16:49

Powered by ScienceNet.cn

Copyright © 2007- 中国科学报社

返回顶部