How to divide images into blocks of size 8 * 8
9 ビュー (過去 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
0 件のコメント
回答 (4 件)
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).
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
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
2012 年 3 月 30 日
2 件のコメント
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
2012 年 3 月 30 日
3 件のコメント
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 Center および File Exchange で Computer Vision with Simulink についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!