How to divide images into blocks of size 8 * 8

5 次查看(过去 30 天)
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

回答(4 个)

Kye Taylor
Kye Taylor 2012-3-29
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 个评论
Vignesh
Vignesh 2012-3-30
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.

请先登录,再进行评论。


Image Analyst
Image Analyst 2012-3-30
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 个评论
rest12
rest12 2014-1-8
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
Vignesh 2012-3-30
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 个评论
Image Analyst
Image Analyst 2012-3-30
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
Vignesh 2012-3-30
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 个评论
Vignesh
Vignesh 2012-3-30
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 2012-3-30
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.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Computer Vision with Simulink 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by