storing image blocks as rows in new matrix

I have the following code that takes ten images and divide every image to (16*16) block using im2col function.The question is I need to store all the blocks that have been generated from the ten images in a matrix so i can use it to apply k-means.
How can I do that?
clc;
close all; % Close all figures (except those of imtool.)
workspace; % Make sure the workspace panel is showing.
fontSize = 20;
% Read image.
TrnImgPath='G:\Iman\UIUC\PNGImages\TrainImages\';
for i=1:10
rgbImage=[TrnImgPath 'pos-' num2str(i) '.PNG'];%Reading positive Pictures
rgbImage=imread(rgbImage);
% Display image full screen.
imshow(rgbImage);
% Enlarge figure to full screen.
%Set Handle Graphics object properties
set(gcf, 'units','normalized','outerposition',[0 0 1 1]);
%gcf means'Current figure handle'
drawnow; %Flush event queue and update figure window
% Get the dimensions of the image. numberOfColorBands should be = 3.
[rows columns numberOfColorBands] = size(rgbImage)
blockSizeR = 16; % Rows in block.
blockSizeC = 16; % Columns in block.
TiledIm = im2col(rgbImage,[blockSizeR blockSizeC],'sliding');
% Now display all the blocks.
plotIndex = 1;
numPlotsR = size(TiledIm, 1);
numPlotsC = size(TiledIm, 2);
for r = 1 : numPlotsR
for c = 1 : numPlotsC
fprintf('plotindex = %d, c=%d, r=%d\n', plotIndex, c, r);
% Specify the location for display of the image.
subplot(numPlotsR, numPlotsC, plotIndex);
% Extract the numerical array out of the cell
% just for tutorial purposes.
rgbBlock = TiledIm(r,c);
imshow(rgbBlock); % Could call imshow(TiledIm{r,c}) if you wanted to.
[rowsB columnsB numberOfColorBandsB] = size(rgbBlock);
% Make the TiledImption the block number.
TiledImption = sprintf('Block #%d of %d\n%d rows by %d columns', ...
plotIndex, numPlotsR*numPlotsC, rowsB, columnsB);
title(TiledImption);
drawnow;
% Increment the subplot to the next loTiledImtion.
plotIndex = plotIndex + 1;
end
end
% Display the original image in the upper left.
subplot(4, 6, 1);
imshow(rgbImage);
title('Original Image');
end
msgbox('Done! Check out the figures.');
/////
Thanks

 Accepted Answer

not sure if this is what you want
so
TiledIm = im2col(rgbImage,[blockSizeR blockSizeC],'sliding');
rearranges image blocks in column.
if you want each block as one row then you need to just transpose the results:
TiledIm=TiledIm';
Now the blocks are in row.

5 Comments

Thanks Mohammad Abouali but what I need is to store the blocks for the ten images because the for loop store image by image. I need to keep the blocks of ten image in one matrix.I hope you can help me
So you need to do something like this:
TiledIm=[];
for i=1: number of images
I=read image i
tmpTile = im2col(I,[blockSizeR blockSizeC],'sliding');
tmpTile = tmpTile';
TileIm=[TileIm; tmpTile];
end
Note that for this to work your block sizes should be the same for all images. If all images are of the same size, you can pre calculate the size of TileIm.
Thanks a lot It works well.
You are welcome. If this answers your question, please accept the answer.

Sign in to comment.

More Answers (0)

Asked:

on 6 Dec 2014

Commented:

on 10 Dec 2014

Community Treasure Hunt

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

Start Hunting!