MATLAB: How to rearrange each block into a column vector

image segmentationmatrix array

example, i have an image,it's 256x256pixel,then i split it into 8×8. How to rearrange each block 8×8 into column vector and form it as 256×256 size image matrix?
Thank you

Best Answer

An 8 by 8 block is 64 elements, so your columns will be 64 rows tall. And with a block size of 8, you are going to have 32 of these columns for each 8 row tall strip. And there will be 32 8-row-tall strips going down your image. So you're going to have 32*32 = 1024 of these 64 row tall columns. Explain to me how these 1024 columns are supposed to be reorganized into a 256x256 matrix.
You may be after something like this, which uses blockproc():
% Demo code to divide the image up into 8 pixel by 8 pixel blocks
% and replace each pixel in the block by the column vector
% of all the pixels in the block.
%
clc;
clearvars;
close all;
workspace;
fontSize = 16;
% Read in a standard MATLAB gray scale demo image.
folder = fullfile(matlabroot, '\toolbox\images\imdemos');
if ~exist(folder, 'dir')
% If that folder does not exist, don't use a folder
% and hope it can find the image on the search path.
folder = [];
end
baseFileName = 'cameraman.tif';
fullFileName = fullfile(folder, baseFileName);
grayImage = imread(fullFileName);
% Get the dimensions of the image. numberOfColorBands should be = 1.
[rows, columns, numberOfColorBands] = size(grayImage)
% Display the original gray scale image.
subplot(2, 2, 1);
imshow(grayImage, []);
title('Original Grayscale Image', 'FontSize', fontSize);
% Enlarge figure to full screen.
set(gcf, 'Position', get(0,'Screensize'));
set(gcf,'name','Image Analysis Demo','numbertitle','off')
% Define the function that we will apply to each block.
% First in this demo we will take the block and shape it into a column vector.
myFunction = @(theBlockStructure) theBlockStructure.data(:);
% Block process the image to replace every pixel in the
% 8 pixel by 8 pixel block by the column vector of the pixels in the block.
blockSize = [8 8];
blockyImage8 = blockproc(single(grayImage), blockSize, myFunction); % Works.
[rows, columns] = size(blockyImage8);
% Display the block image.
subplot(2, 2, 2);
imshow(blockyImage8, []);
caption = sprintf('Block Image\n32 by 32 blocks. Input block size = 8\n%d rows by %d columns', rows, columns);
title(caption, 'FontSize', fontSize);