How to divide images into blocks of size 8 * 8
8 views (last 30 days)
Show older comments
I have a set of images, seperated from a video. Each image`s size is 640 * 480. I want to divide it into blocks of size 8*8.
I used the following code to divide the image into blocks of size 240 *320.
vid=VideoReader('projvideo.avi')
n=vid.NumberofFrames;
for x=1:n
frame=read(vid,x);
imwrite(frame,sprintf('D:\\Frames\\image%d.jpg',x));
end
a=240;
b=320;
for j=1:n
i=imread(sprintf('D:\\Frames\\image%d.jpg',j));
g=mat2cell(i,[a a],[b b],3);
end
But if try to divide them into blocks of size 8*8,(a=8, b=8) i get an error.
??? Error using ==> mat2cell at 113
Input arguments, D1 through D3, must sum to each dimension of the input
matrix size, [480 640 3].
Help me to divide the blocks into 8*8 size
0 Comments
Answers (4)
Kye Taylor
on 29 Mar 2012
Replace
g = mat2cell(i,[a,a],[b,b],3);
with
g = mat2cell(i,8*ones(1,size(i,1)/8),8*ones(1,size(i,2)/8),3);
effectively changing the 2nd and 3rd inputs into vectors that look like
[8 8 8 8 ...]
with the proper number of elements. Note that the above command will only work provided size(i,1) and size(i,2) are divisible by 8 (as are 640 and 480).
Image Analyst
on 30 Mar 2012
vignesh: I used Kye's formula in this demo. It uses a standard MATLAB demo image. See if you can run it and see the array of small images ("blocks") it creates.
clc; % Clear the command window.
format compact;
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
rgbImage = imread(fullFileName);
% For demo purposes, let's resize it to be 64 by 64;
rgbImage = imresize(rgbImage, [64 64]);
% Get the dimensions of the image. numberOfColorBands should be = 3.
[rows columns numberOfColorBands] = size(rgbImage)
ca = mat2cell(rgbImage,8*ones(1,size(rgbImage,1)/8),8*ones(1,size(rgbImage,2)/8),3);
plotIndex = 1;
for c = 1 : size(ca, 2)
for r = 1 : size(ca, 1)
fprintf('c=%d, r=%d\n', c, r);
subplot(8,8,plotIndex);
imshow(ca{r,c});
plotIndex = plotIndex + 1
end
end
% Enlarge figure to full screen.
set(gcf, 'units','normalized','outerposition',[0 0 1 1]);
6 Comments
rest12
on 8 Jan 2014
Can any body tell me, If I want to apply hanning window to all the blocks then What changes do I need to make in the above code?
Vignesh
on 30 Mar 2012
2 Comments
Image Analyst
on 30 Mar 2012
I don't know what you mean by access. I gave you the variable with the little chunk of image - it's ca{r,c}. So there it is, just "access" that in whatever way you want.
Vignesh
on 30 Mar 2012
3 Comments
Image Analyst
on 30 Mar 2012
Well you have to adapt it if you have more images than 1. If you fill up the screen and then move on to another image, you have to reset plotIndex back to 1 for new images.
See Also
Categories
Find more on Computer Vision with Simulink in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!