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
image segmentationmatrix array
% Demo to divide a color image up into blocks.
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
workspace; % Make sure the workspace panel is showing.
fontSize = 20;% Read in a standard MATLAB color demo image.
folder = fullfile(matlabroot, '\toolbox\images\imdemos');baseFileName = 'peppers.png';fullFileName = fullfile(folder, baseFileName);% Get the full filename, with path prepended.
fullFileName = fullfile(folder, baseFileName);if ~exist(fullFileName, 'file') % Didn't find it there. Check the search path for it.
fullFileName = baseFileName; % No path this time.
if ~exist(fullFileName, 'file') % Still didn't find it. Alert user.
errorMessage = sprintf('Error: %s does not exist.', fullFileName); uiwait(warndlg(errorMessage)); return; endend% Read the image from disk.
rgbImage = imread(fullFileName);% Test code if you want to try it with a gray scale image.
% Uncomment line below if you want to see how it works with a gray scale image.
% rgbImage = rgb2gray(rgbImage);
% Display image full screen.
imshow(rgbImage);% Enlarge figure to full screen.
set(gcf, 'units','normalized','outerposition',[0 0 1 1]);drawnow;% Get the dimensions of the image. numberOfColorBands should be = 3.
[rows columns numberOfColorBands] = size(rgbImage)%==========================================================================
% The first way to divide an image up into blocks is by using mat2cell().
blockSizeR = 150; % Rows in block.
blockSizeC = 100; % Columns in block.
% Figure out the size of each block in rows.
% Most will be blockSizeR but there may be a remainder amount of less than that.
wholeBlockRows = floor(rows / blockSizeR);blockVectorR = [blockSizeR * ones(1, wholeBlockRows), rem(rows, blockSizeR)];% Figure out the size of each block in columns.
wholeBlockCols = floor(columns / blockSizeC);blockVectorC = [blockSizeC * ones(1, wholeBlockCols), rem(columns, blockSizeC)];% Create the cell array, ca.
% Each cell (except for the remainder cells at the end of the image)
% in the array contains a blockSizeR by blockSizeC by 3 color array.
% This line is where the image is actually divided up into blocks.
if numberOfColorBands > 1 % It's a color image.
ca = mat2cell(rgbImage, blockVectorR, blockVectorC, numberOfColorBands);else ca = mat2cell(rgbImage, blockVectorR, blockVectorC);end% Now display all the blocks.
plotIndex = 1;numPlotsR = size(ca, 1);numPlotsC = size(ca, 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 = ca{r,c}; imshow(rgbBlock); % Could call imshow(ca{r,c}) if you wanted to.
[rowsB columnsB numberOfColorBandsB] = size(rgbBlock); % Make the caption the block number.
caption = sprintf('Block #%d of %d\n%d rows by %d columns', ... plotIndex, numPlotsR*numPlotsC, rowsB, columnsB); title(caption); drawnow; % Increment the subplot to the next location.
plotIndex = plotIndex + 1; endend% Display the original image in the upper left.
subplot(4, 6, 1);imshow(rgbImage);title('Original Image');%==============================================================================
% Another way to split the image up into blocks is to use indexing.
% Read in a standard MATLAB gray scale demo image.
folder = fullfile(matlabroot, '\toolbox\images\imdemos');baseFileName = 'cameraman.tif';fullFileName = fullfile(folder, baseFileName);% Get the full filename, with path prepended.fullFileName = fullfile(folder, baseFileName);if ~exist(fullFileName, 'file') % Didn't find it there. Check the search path for it. fullFileName = baseFileName; % No path this time. if ~exist(fullFileName, 'file') % Still didn't find it. Alert user. errorMessage = sprintf('Error: %s does not exist.', fullFileName); uiwait(warndlg(errorMessage)); return; endendgrayImage = imread(fullFileName);% Get the dimensions of the image. numberOfColorBands should be = 1.
[rows columns numberOfColorBands] = size(grayImage);% Display the original gray scale image.
figure;subplot(2, 2, 1);imshow(grayImage, []);title('Original Grayscale Image', 'FontSize', fontSize);% Enlarge figure to full screen.set(gcf, 'units','normalized','outerposition',[0 0 1 1]);% Divide the image up into 4 blocks.
% Let's assume we know the block size and that all blocks will be the same size.
blockSizeR = 128; % Rows in block.blockSizeC = 128; % Columns in block.% Figure out the size of each block.
wholeBlockRows = floor(rows / blockSizeR);wholeBlockCols = floor(columns / blockSizeC);% Preallocate a 3D image
image3d = zeros(wholeBlockRows, wholeBlockCols, 3);% Now scan though, getting each block and putting it as a slice of a 3D array.
sliceNumber = 1;for row = 1 : blockSizeR : rows for col = 1 : blockSizeR : columns % Let's be a little explicit here in our variables
% to make it easier to see what's going on.
row1 = row; row2 = row1 + blockSizeR - 1; col1 = col; col2 = col1 + blockSizeC - 1; % Extract out the block into a single subimage.
oneBlock = grayImage(row1:row2, col1:col2); % Specify the location for display of the image. subplot(2, 2, sliceNumber); imshow(oneBlock); % Make the caption the block number. caption = sprintf('Block #%d of 4', sliceNumber); title(caption); drawnow; % Assign this slice to the image we just extracted.
image3D(:, :, sliceNumber) = oneBlock; sliceNumber = sliceNumber + 1; endend% Now image3D is a 3D image where each slice,
% or plane, is one quadrant of the original 2D image.
msgbox('Done with demo! Check out the two figures.');
meanFilterFunction = @(theBlockStructure) mean2(theBlockStructure.data(:)) * ones(2,2, class(theBlockStructure.data));blockSize = [32 32];blockyImage = blockproc(grayImage, blockSize, meanFilterFunction);
% Demo code to divide the image up into 16 pixel by 16 pixel blocks
% and replace each pixel in the block by the median, mean, or standard
% deviation of all the gray levels of 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 = [];endbaseFileName = '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 median gray value in the block
% and create an equal size block where all pixels have the median value.
% Image will be the same size since we are using ones() and so for each block
% there will be a block of 8 by 8 output pixels.
medianFilterFunction = @(theBlockStructure) median(theBlockStructure.data(:)) * ones(size(theBlockStructure.data), class(theBlockStructure.data));% Block process the image to replace every pixel in the
% 8 pixel by 8 pixel block by the median of the pixels in the block.
blockSize = [8 8];% Quirk: must cast grayImage to single or double for it to work with median().
% blockyImage8 = blockproc(grayImage, blockSize, medianFilterFunction); % Doesn't work.
blockyImage8 = blockproc(single(grayImage), blockSize, medianFilterFunction); % Works.
[rows columns] = size(blockyImage8);% Display the block median image.
subplot(2, 2, 2);imshow(blockyImage8, []);caption = sprintf('Block Median Image\n32 blocks. Input block size = 8, output block size = 8\n%d rows by %d columns', rows, columns);title(caption, 'FontSize', fontSize);% Block process the image to replace every pixel in the % 4 pixel by 4 pixel block by the mean of the pixels in the block.
% The image is 256 pixels across which will give 256/4 = 64 blocks.
% Note that the size of the output block (2 by 2) does not need to be the size of the input block!
% Image will be the 128 x 128 since we are using ones(2, 2) and so for each of the 64 blocks across
% there will be a block of 2 by 2 output pixels, giving an output size of 64*2 = 128.
% We will still have 64 blocks across but each block will only be 2 output pixels across,
% even though we moved in steps of 4 pixels across the input image.
meanFilterFunction = @(theBlockStructure) mean2(theBlockStructure.data(:)) * ones(2,2, class(theBlockStructure.data));blockSize = [4 4];blockyImage64 = blockproc(grayImage, blockSize, meanFilterFunction);[rows columns] = size(blockyImage64);% Display the block mean image.
subplot(2, 2, 3);imshow(blockyImage64, []);caption = sprintf('Block Mean Image\n64 blocks. Input block size = 4, output block size = 2\n%d rows by %d columns', rows, columns);title(caption, 'FontSize', fontSize);% Block process the image to replace every pixel in the % 8 pixel by 8 pixel block by the standard deviation
% of the pixels in the block.
% Image will be smaller since we are not using ones() and so for each block
% there will be just one output pixel, not a block of 8 by 8 output pixels.
blockSize = [8 8];StDevFilterFunction = @(theBlockStructure) std(double(theBlockStructure.data(:)));blockyImageSD = blockproc(grayImage, blockSize, StDevFilterFunction);[rows columns] = size(blockyImageSD);% Display the block standard deviation filtered image.
subplot(2, 2, 4);imshow(blockyImageSD, []);title('Standard Deviation Filtered Image', 'FontSize', fontSize);caption = sprintf('Block Standard Deviation Filtered Image\n32 blocks. Input block size = 8, output block size = 1\n%d rows by %d columns', rows, columns);title(caption, 'FontSize', fontSize);
Best Answer