how to make it short codes?

3 次查看(过去 30 天)
hi all.. if i have value of each block 8x8 pixels. i'm looking for how to random each block? any idea for it? but it's not effective. here's my codes.. could you fix it?
B=imread('filename');
a = reshape(B(1:128, 1:128), 8, 16, 8, 16);
a = permute(a, [1,3,2,4]);
%example for scramble positions
a(:,:,1,1)=a(:,:,16,2);
a(:,:,1,2)=a(:,:,2,1);
a(:,:,1,3)=a(:,:,4,1);
a(:,:,1,4)=a(:,:,6,1);
a(:,:,1,5)=a(:,:,7,4);
a(:,:,1,6)=a(:,:,9,16);
a(:,:,1,7)=a(:,:,5,10);
a(:,:,1,8)=a(:,:,7,1);
a(:,:,1,9)=a(:,:,5,1);
a(:,:,1,10)=a(:,:,5,1);
a(:,:,1,11)=a(:,:,4,4);
a(:,:,1,12)=a(:,:,3,1);
a(:,:,1,13)=a(:,:,2,3);
a(:,:,1,14)=a(:,:,2,5);
a(:,:,1,15)=a(:,:,5,4);
a(:,:,1,16)=a(:,:,15,2);
a(:,:,2,1)=...
a(:,:,..,..)=...
and so on until a(:,:,16,16). so we have 256 blocks to random without any change in the value of each block.
how to scramble it? i hope it'll easier
thanks..

采纳的回答

Andrei Bobrov
Andrei Bobrov 2013-7-25
编辑:Andrei Bobrov 2013-7-25
Use tricks by Peter Acklam
B = randi(300,128,128); % Let your data
b = [8 8]; % size of small block
s = size(B);
m = s./b;
B1 = permute(reshape(B,b(1),m(1),m(2),b(2)),[1 3 2 4]);
B2 = reshape(B1,[b prod(m)]);
idx3 = randperm(size(B2,3));
B3 = B2(:,:,idx3);
a = reshape(permute(reshape(B3,b(1),s(2),[]),[1 3 2]),s(1),[]);
OR with use of cell arrays
B = randi(300,128,128); % Let your data
b = [8 8]; % size of small block
m = size(B)./b;
C0 = mat2cell(B,b(1)*ones(m(1),1),b(2)*ones(m(2),1));
ii = randperm(numel(C0));
a = cell2mat(reshape(C0(ii),m));

更多回答(1 个)

Roger Stafford
Roger Stafford 2013-7-25
If I have understood your question correctly, you can keep the first three lines:
B = imread('filename');
a = reshape(B(1:128, 1:128), 8, 16, 8, 16);
a = permute(a, [1,3,2,4]);
Then do this:
p = bsxfun(@plus,64*randperm(256),(-63:0)'); % Calculate 16384 linear indices
a = reshape(a(p),8,8,16,16); % Permute the various 8 x 8 blocks in 'a'
(Note: Your method of operating on 'a' one 8 x 8 block at a time has the possibility of changing a block and then erroneously using those changed values to change another block instead of using its original values. To avoid this, you would have had to use a new variable temporarily and when finished, copy it back into 'a'. In any case, writing 256 lines of code would be rather tedious.)

类别

Help CenterFile Exchange 中查找有关 Performance and Memory 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by