storing image blocks as rows in new matrix
2 次查看(过去 30 天)
显示 更早的评论
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
0 个评论
采纳的回答
Mohammad Abouali
2014-12-6
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 个评论
Mohammad Abouali
2014-12-9
You are welcome. If this answers your question, please accept the answer.
更多回答(0 个)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!