Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Request the code of block permutation algorithm on picture in matlab,please help me.

Asked by nazan rad on 13 Aug 2012

hello. I need the code of block permutation algorithm on picture in matlab, please help me,thank you.

1 Comment

Walter Roberson on 13 Aug 2012

Which block permutation algorithm ?

nazan rad

Products

No products are associated with this question.

3 Answers

Answer by Image Analyst on 18 Aug 2012
Accepted answer

For what it's worth, here's a demo I have that splits an image up into blocks. We don't know what your algorithm is but maybe you can use randperm like Walter suggested, or some other randomization scheme to scramble these blocks and stitch them together again.

% Demo to divide a color image up into blocks.
clc;    % Clear the command window.
close all;  % Close all figures (except those of imtool.)
workspace;  % Make sure the workspace panel is showing.
fontSize = 20;
% Read in a standard MATLAB color demo image.
folder = fullfile(matlabroot, '\toolbox\images\imdemos');
baseFileName = 'peppers.png';
fullFileName = fullfile(folder, baseFileName);
% Get the full filename, with path prepended.
fullFileName = fullfile(folder, baseFileName);
if ~exist(fullFileName, 'file')
	% Didn't find it there.  Check the search path for it.
	fullFileName = baseFileName; % No path this time.
	if ~exist(fullFileName, 'file')
		% Still didn't find it.  Alert user.
		errorMessage = sprintf('Error: %s does not exist.', fullFileName);
		uiwait(warndlg(errorMessage));
		return;
	end
end
% Read the image from disk.
rgbImage = imread(fullFileName);
% Test code if you want to try it with a gray scale image.
% Uncomment line below if you want to see how it works with a gray scale image.
% rgbImage = rgb2gray(rgbImage);
% Display image full screen.
imshow(rgbImage);
% Enlarge figure to full screen.
set(gcf, 'units','normalized','outerposition',[0 0 1 1]);
drawnow;
% Get the dimensions of the image.  numberOfColorBands should be = 3.
[rows columns numberOfColorBands] = size(rgbImage)
%==========================================================================
% The first way to divide an image up into blocks is by using mat2cell().
blockSizeR = 150; % Rows in block.
blockSizeC = 100; % Columns in block.
% Figure out the size of each block in rows. 
% Most will be blockSizeR but there may be a remainder amount of less than that.
wholeBlockRows = floor(rows / blockSizeR);
blockVectorR = [blockSizeR * ones(1, wholeBlockRows), rem(rows, blockSizeR)];
% Figure out the size of each block in columns. 
wholeBlockCols = floor(columns / blockSizeC);
blockVectorC = [blockSizeC * ones(1, wholeBlockCols), rem(columns, blockSizeC)];
% Create the cell array, ca.  
% Each cell (except for the remainder cells at the end of the image)
% in the array contains a blockSizeR by blockSizeC by 3 color array.
% This line is where the image is actually divided up into blocks.
if numberOfColorBands > 1
	% It's a color image.
	ca = mat2cell(rgbImage, blockVectorR, blockVectorC, numberOfColorBands);
else
	ca = mat2cell(rgbImage, blockVectorR, blockVectorC);
end
% Now display all the blocks.
plotIndex = 1;
numPlotsR = size(ca, 1);
numPlotsC = size(ca, 2);
for r = 1 : numPlotsR
	for c = 1 : numPlotsC
		fprintf('plotindex = %d,   c=%d, r=%d\n', plotIndex, c, r);
		% Specify the location for display of the image.
		subplot(numPlotsR, numPlotsC, plotIndex);
		% Extract the numerical array out of the cell
		% just for tutorial purposes.
		rgbBlock = ca{r,c};
		imshow(rgbBlock); % Could call imshow(ca{r,c}) if you wanted to.
		[rowsB columnsB numberOfColorBandsB] = size(rgbBlock);
		% Make the caption the block number.
		caption = sprintf('Block #%d of %d\n%d rows by %d columns', ...
			plotIndex, numPlotsR*numPlotsC, rowsB, columnsB);
		title(caption);
		drawnow;
		% Increment the subplot to the next location.
		plotIndex = plotIndex + 1;
	end
end
% Display the original image in the upper left.
subplot(4, 6, 1);
imshow(rgbImage);
title('Original Image');
%==============================================================================
% Another way to split the image up into blocks is to use indexing.
% Read in a standard MATLAB gray scale demo image.
folder = fullfile(matlabroot, '\toolbox\images\imdemos');
baseFileName = 'cameraman.tif';
fullFileName = fullfile(folder, baseFileName);
% Get the full filename, with path prepended.
fullFileName = fullfile(folder, baseFileName);
if ~exist(fullFileName, 'file')
	% Didn't find it there.  Check the search path for it.
	fullFileName = baseFileName; % No path this time.
	if ~exist(fullFileName, 'file')
		% Still didn't find it.  Alert user.
		errorMessage = sprintf('Error: %s does not exist.', fullFileName);
		uiwait(warndlg(errorMessage));
		return;
	end
end
grayImage = imread(fullFileName);
% Get the dimensions of the image.  numberOfColorBands should be = 1.
[rows columns numberOfColorBands] = size(grayImage);
% Display the original gray scale image.
figure;
subplot(2, 2, 1);
imshow(grayImage, []);
title('Original Grayscale Image', 'FontSize', fontSize);
% Enlarge figure to full screen.
set(gcf, 'units','normalized','outerposition',[0 0 1 1]);
% Divide the image up into 4 blocks.
% Let's assume we know the block size and that all blocks will be the same size.
blockSizeR = 128; % Rows in block.
blockSizeC = 128; % Columns in block.
% Figure out the size of each block. 
wholeBlockRows = floor(rows / blockSizeR);
wholeBlockCols = floor(columns / blockSizeC);
% Preallocate a 3D image
image3d = zeros(wholeBlockRows, wholeBlockCols, 3);
% Now scan though, getting each block and putting it as a slice of a 3D array.
sliceNumber = 1;
for row = 1 : blockSizeR : rows
	for col = 1 : blockSizeR : columns
		% Let's be a little explicit here in our variables
		% to make it easier to see what's going on.
		row1 = row;
		row2 = row1 + blockSizeR - 1;
		col1 = col;
		col2 = col1 + blockSizeC - 1;
		% Extract out the block into a single subimage.
		oneBlock = grayImage(row1:row2, col1:col2);
		% Specify the location for display of the image.
		subplot(2, 2, sliceNumber);
		imshow(oneBlock);
		% Make the caption the block number.
		caption = sprintf('Block #%d of 4', sliceNumber);
		title(caption);
		drawnow;
		% Assign this slice to the image we just extracted.
		image3D(:, :, sliceNumber) = oneBlock;
		sliceNumber = sliceNumber + 1;
	end
end
% Now image3D is a 3D image where each slice, 
% or plane, is one quadrant of the original 2D image.
msgbox('Done with demo!  Check out the two figures.');

0 Comments

Image Analyst
Answer by nazan rad on 18 Aug 2012

Walter dear. The block permutation algorithm is the basic permutation for encryption images. in this technique the image can be decomposed into blocks. and I need this algorithm.please help me.

4 Comments

nazan rad on 18 Aug 2012

thanks, but I need this algorithm,in this algorithm when the image decompose into blocks a groups of blocks are permuted horizontally and vertically,but I can 't write the code. could you please tell me the algorithm and write it for me.

Oleg Komarov on 18 Aug 2012

Any good reason why Walter (or anybody else) should do it for you (on a weekend)?

Walter Roberson on 18 Aug 2012

nazan, you are responsible for researching the algorithm for your assignment. We can assist you if you have specific difficulties in the MATLAB implementation (e.g., we can help with tracking problems with index out of bounds.)

Depending exactly which permutations the algorithm allows, randperm() is likely to turn out to be mathematically equivalent.

nazan rad
Answer by nazan rad on 19 Aug 2012

Walter,thank you very much for your code.I 'm beginner in coding in matlab, It is very usefull for me.

3 Comments

Oleg Komarov on 19 Aug 2012

Please use comments instead of posting new answers. Also, it's IA who posted the code. Finally, accept the answer if it solved your issue.

Walter Roberson on 19 Aug 2012

"Walter" is known not to be Image Analyst's name.

Jan Simon on 22 Nov 2012

I have accepted IA's answer, because the OP claimed, that it had helped. I personally like Walter's answer more. But this is a personal opinion only.

nazan rad

Contact us