storing image blocks as rows in new matrix

1 Ansicht (letzte 30 Tage)
eman
eman am 6 Dez. 2014
Kommentiert: eman am 10 Dez. 2014
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

Akzeptierte Antwort

Mohammad Abouali
Mohammad Abouali am 6 Dez. 2014
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 Kommentare
Mohammad Abouali
Mohammad Abouali am 9 Dez. 2014
You are welcome. If this answers your question, please accept the answer.
eman
eman am 10 Dez. 2014
Done :)

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Community Treasure Hunt

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

Start Hunting!

Translated by