How to divide images into blocks of size 8 * 8

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

Respuestas (4)

Kye Taylor
Kye Taylor el 29 de Mzo. de 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).

1 comentario

Vignesh
Vignesh el 30 de Mzo. de 2012
I guess now the images are divided into blocks of size 8 * 8. But when i access the individual blocks.
>> g{1}
ans(:,:,1) =
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
ans(:,:,2) =
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
i`m getting zeroes for all( even for g{2},g{3} etc).
When i divided the image into 240 * 320 size, i had 4 blocks of every image. Now, i dont understand how many blocks i have for each image. Help me to find the no of blocks(horizontal and vertical, if there is any formula) and also pls tell me how to access the individual blocks.

Iniciar sesión para comentar.

Image Analyst
Image Analyst el 30 de Mzo. de 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 comentarios

Vignesh
Vignesh el 30 de Mzo. de 2012
Hi image analyst :),
Does resizing the image help? From your code i got only 64 blocks. I need to access all the 4800 blocks of an image, since i want to calculate motion vectors for them.
Image Analyst
Image Analyst el 30 de Mzo. de 2012
No. I only resized it to 480x640 because that was the size of the image you said you were starting with but the MATLAB demo image was not that size initially. So I resized it only to be the size of the image you wanted, not for any other reason. I don't know where the other 4736 blocks would be coming from unless you'd be processing multiple frames and doing the above on each frame.
Vignesh
Vignesh el 30 de Mzo. de 2012
rgbImage = imresize(rgbImage, [64 64]);
what does that 64 mean? Could you please elaborate?
Image Analyst
Image Analyst el 30 de Mzo. de 2012
Sorry - I was remembering a different answer. But the above is the same - I just resized it to 64x64 to that we could fit an 8 by 8 array of 8x8 blocks on one screen. I didn't want to put up hundreds of blocks because they'd be so tiny and there would be so many of them that you couldn't see what was going on.
rest12
rest12 el 8 de En. de 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?
sujee y
sujee y el 30 de Oct. de 2019
how to do it on gray image?

Iniciar sesión para comentar.

Vignesh
Vignesh el 30 de Mzo. de 2012

0 votos

Like i mentioned i use a set of images seperated from a video, each of size 640 * 480. Now, by using Kye taylor`s formula, I think i managed to divide each image into blocks of size 8 * 8(horizontal blocks -60 and vertical - 80, total- 4800). But i dont know how to access each block, say for example, 4th image`s 4700 th block).

2 comentarios

Image Analyst
Image Analyst el 30 de Mzo. de 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.
Angga Lisdiyanto
Angga Lisdiyanto el 22 de En. de 2016
That's right.
r = row
c = column

Iniciar sesión para comentar.

Vignesh
Vignesh el 30 de Mzo. de 2012

0 votos

I`ll post my code again, since i missed a brace next to g. (g{j}) //Seperating frames from the video vid=VideoReader('projvideo.avi') no=vid.NumberofFrames; for x=1:no frame=read(vid,x); imwrite(frame,sprintf('D:\\Frames\\image%d.jpg',x)); end //Dividing all the frames into blocks of size 8 * 8 for j=1:n i=imread(sprintf('D:\\Frames\\image%d.jpg',j)); g{j} = mat2cell(i,8*ones(1,size(i,1)/8),8*ones(1,size(i,2)/8),3); end

3 comentarios

Image Analyst
Image Analyst el 30 de Mzo. de 2012
As long as your blocks are to be within each frame, not going across multiple frames, then the code we gave you above will work. You simply apply the code to each frame one at a time.
Vignesh
Vignesh el 30 de Mzo. de 2012
Ok :) But the code which u gave is only for 1 image. To get it working for all the images, i used another for loop like this.
plotIndex = 1;
for k=1:n
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{k}{r,c});
plotIndex = plotIndex + 1
end
end
end
Will that work? I tried it and got an error
.
.
.
plotIndex =
64
c=1, r=1
plotIndex =
65
c=1, r=1
??? Error using ==> subplot at 311
Index exceeds number of subplots.
Image Analyst
Image Analyst el 30 de Mzo. de 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.

Iniciar sesión para comentar.

Categorías

Preguntada:

el 29 de Mzo. de 2012

Comentada:

el 30 de Oct. de 2019

Community Treasure Hunt

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

Start Hunting!

Translated by