How to divide a 1024 * 1024 image into 128 *128 equal parts?

Image size is 1024*1024. I need to divide this image into equal parts of size 128*128 and then i need to calculate local variance map for each parts. Can anyone please help me to do this. Thanks in advance.

1 Comment

How cool is this! It would be useful to learn, how accepting two answers works intentionally.

Sign in to comment.

More Answers (1)

Here's how to do it:
% Demo of blockproc.
% Uses an anonymous function to return a block of pixels
% the same size as the sliding window block.
% Blocksize is 128 and image is 1024 wide so there will be 8 values across.
try
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
workspace; % Make sure the workspace panel is showing.
fontSize = 20;
% Change the current folder to the folder of this m-file.
if(~isdeployed)
cd(fileparts(which(mfilename)));
end
% Read in standard MATLAB demo image.
grayImage = imread('moon.tif');
[rows, columns, numberOfColorChannels] = size(grayImage);
% Resize it to be 1024 by 1024 for demo for Aynesh, who has that size images.
grayImage = imresize(grayImage, [1024, 1024]);
[rows, columns, numberOfColorChannels] = size(grayImage);
% Display the original image.
subplot(2, 2, 1);
imshow(grayImage, []);
caption = sprintf('Original Image\n%d by %d pixels', ...
rows, columns);
title(caption, 'FontSize', fontSize);
% Enlarge figure to full screen.
set(gcf, 'Position', get(0,'Screensize'));
set(gcf, 'name','Image Analysis Demo', 'numbertitle','off')
% Now let's use an anonymous function.
% We'll take the standard deviation in the blocks.
windowSize = 128;
myFilterHandle2 = @(block_struct) ...
std2(block_struct.data(:));
blockyImageSD = blockproc(grayImage, [windowSize windowSize], myFilterHandle2);
[rowsSD, columnsSD, numberOfColorChannelsSD] = size(blockyImageSD);
subplot(2, 2, 2);
imshow(blockyImageSD, []);
caption = sprintf('Image Processed in %d by %d Blocks\nStd. Dev. Output Image is %d by %d pixels\nAnonymous Standard Deviation Filter', ...
windowSize, windowSize, rowsSD, columnsSD);
title(caption, 'FontSize', fontSize);
% Note: the image size of blockyImageSD is 8x8, smaller than the original.
% Convert to variance image by squaring:
varianceImage = blockyImageSD .^2;
subplot(2, 2, 3);
imshow(varianceImage, []);
caption = sprintf('Image Processed in %d by %d Blocks\nVariance Output Image is %d by %d pixels\nIt equals Std. Dev. Image Squared.', ...
windowSize, windowSize, rowsSD, columnsSD);
title(caption, 'FontSize', fontSize);
uiwait(msgbox('Done with demo'));
catch ME
errorMessage = sprintf('Error in blockproc_demo():\n\nError Message:\n%s', ME.message);
uiwait(warndlg(errorMessage));
end

Community Treasure Hunt

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

Start Hunting!