Compressing pixelated image blocks into array

4 views (last 30 days)
I have the following function
function pixd = pixellate(fileName, stepSize)
I=imread(fileName);
fun = @(block_struct) mean2(block_struct.data)*ones(size(block_struct.data));
I2=uint8(blockproc(I,[stepSize stepSize],fun));
imshow(I2);
pixd = I2;
that pixellates an image into stepSize number of blocks returned in the array I2. Does anyone have any suggestions on how to take each block in I2 and store its value as a single element in a new array that will be stepSize x stepSize in dimension? Thanks!

Answers (3)

Raag
Raag on 7 Feb 2025
Hi Sushruta,
I am assuming that you want to know the procedure to store the average values of each block from a pixelated image into a new array.
Here are steps, to achieve the above-mentioned workflow:
  • For a coloured image, convert it to grayscale.
  • Use ‘blockMeans’ to store mean values of each block.
  • Divide the dimensions of image by ‘stepsize’ to get the size of the matrix.
  • For mean calculation across the entire image which created a pixelated effect use the ‘blockproc’ function.
Here is an example code for the steps mentioned above:
function [pixd, blockMeans] = pixellate(fileName, stepSize) I = imread(fileName); % Ensure the image is in grayscale if size(I, 3) == 3 I = rgb2gray(I); end
% Calculate block means and create a new matrix to store them
[rows, cols] = size(I);
blockMeans = zeros(floor(rows / stepSize), floor(cols / stepSize));
fun = @(block_struct) mean2(block_struct.data) * ones(size(block_struct.data));
I2 = uint8(blockproc(I, [stepSize stepSize], fun));
% Fill the blockMeans matrix with the mean of each block
for i = 1:stepSize:rows
for j = 1:stepSize:cols
block = I(i:min(i+stepSize-1, rows), j:min(j+stepSize-1, cols));
blockMean = mean2(block);
blockMeans(ceil(i/stepSize), ceil(j/stepSize)) = blockMean;
end
end
imshow(I2);
pixd = I2;
end
For more information on blockproc function, use this command to open the documentation:
>> web(fullfile(docroot, "images/ref/blockproc.html"))

Image Analyst
Image Analyst on 8 Feb 2025
Just get rid of the ones() in your fun definition:
fun = @(block_struct) mean2(block_struct.data)*ones(size(block_struct.data));
becomes
fun = @(block_struct) mean2(block_struct.data);
I believe that should do it.

Image Analyst
Image Analyst on 8 Feb 2025
Get rid of ones(). Hopefully this well commented demo will explain since it covers three scenarios. @Sushruta Chandramouli I think you asked for the lower left example.
% Uses blockproc() to get mean of image blocks for a variety of input block sizes and output block sizes.
% Demo code to divide the image up into N pixel by N pixel blocks
% and replace each pixel in the block by the mean of all the gray levels of the pixels in the block,
% then replicate each pixel a number of times to make the output image also have the appearance of blocks.
% Initialization steps.
clc;
clearvars;
close all;
workspace;
fontSize = 25;
%===============================================================================================================================
% Read in a standard MATLAB gray scale demo image.
folder = fileparts(which('cameraman.tif'));
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, []);
axis('on', 'image');
impixelinfo;
caption = sprintf('Original Grayscale Input Image\n%d rows by %d columns', rows, columns);
title(caption, 'FontSize', fontSize);
% Enlarge figure to full screen.
g = gcf;
g.WindowState = 'maximized';
set(gcf,'name','Image Analysis Demo','numbertitle','off')
drawnow; % Force screen to refresh immediately.
%===============================================================================================================================
% Define the function that we will apply to each block.
% First in this demo we will take the mean gray value in the block
% and create an equal size block where all pixels have the mean 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.
meanFilterFunction = @(theBlockStructure) mean(theBlockStructure.data(:)) * ones(size(theBlockStructure.data), class(theBlockStructure.data));
% Note: it's the ones(size(theBlockStructure.data), class(theBlockStructure.data)) that
% makes the output be the same size as the input rather than 1/8th the size of the input. It basically replicates the mean pixel into an 8x8 square.
%===============================================================================================================================
% MEAN of 8 pixel by 8 pixel block
% Block process the image to replace every pixel in the
% 8 pixel by 8 pixel block by a single pixel whose value is the mean of the pixels in the block.
blockSize = [8, 8];
blockyImage8888 = blockproc(grayImage, blockSize, meanFilterFunction); % Works.
[rows, columns] = size(blockyImage8888);
% Display the Block Mean Output Image.
subplot(2, 2, 2);
imshow(blockyImage8888, []);
axis('on', 'image');
impixelinfo;
caption = sprintf('Block Mean Output Image\n32 blocks. Input block size = %d, output block size = 8\nOutput image is %d rows by %d columns', blockSize(1), rows, columns);
title(caption, 'FontSize', fontSize);
drawnow; % Force screen to refresh immediately.
%===============================================================================================================================
% MEAN of 8 pixel by 8 pixel block
% Block process the image to replace every pixel in the
% 8 pixel by 8 pixel block by the mean 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.
% Define a filter function that will give 1 pixel as output for every 2-D block of input.
meanFilterFunction = @(theBlockStructure) mean(theBlockStructure.data(:));
blockSize = [8, 8];
blockyImage8811 = blockproc(grayImage, blockSize, meanFilterFunction);
[rows, columns] = size(blockyImage8811);
% Display the block filtered image.
subplot(2, 2, 3);
imshow(blockyImage8811, []);
axis('on', 'image');
impixelinfo;
caption = sprintf('Block Mean Output Image\n32 blocks. Input block size = %d, output block size = 1\nOutput image is %d rows by %d columns', blockSize(1), rows, columns);
title(caption, 'FontSize', fontSize);
drawnow; % Force screen to refresh immediately.
%===============================================================================================================================
% MEAN of 4 pixel by 4 pixel block
% 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, and then enlarge each mean to a 2x2 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.
% Define a filter function that will give a 2x2 pixel block as output for every 2-D block of input.
meanFilterFunction = @(theBlockStructure) mean2(theBlockStructure.data(:)) * ones(2,2, class(theBlockStructure.data));
blockSize = [4, 4];
blockyImage4422 = blockproc(grayImage, blockSize, meanFilterFunction);
[rows, columns] = size(blockyImage4422);
% Display the Block Mean Output Image.
subplot(2, 2, 4);
imshow(blockyImage4422, []);
axis('on', 'image');
impixelinfo;
caption = sprintf('Block Mean Output Image\n64 blocks. Input block size = %d, output block size = 2\nOutput image is %d rows by %d columns', blockSize(1), rows, columns);
title(caption, 'FontSize', fontSize);

Products


Release

R2019a

Community Treasure Hunt

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

Start Hunting!