storing image blocks as rows in new matrix

조회 수: 2 (최근 30일)
eman
eman 2014년 12월 6일
댓글: eman 2014년 12월 10일
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

채택된 답변

Mohammad Abouali
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
Mohammad Abouali 2014년 12월 9일
You are welcome. If this answers your question, please accept the answer.
eman
eman 2014년 12월 10일
Done :)

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

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