Vertical binning of images
12 views (last 30 days)
Show older comments
Mandeep Kaur
on 17 Jan 2022
Commented: Mandeep Kaur
on 17 Jan 2022
I want to do vertical binning of .xim images, But I do not know the difference between resizing and binning of an images. I want keep the same number of pixels. However, in the vertical direction I want to average the two pixels in adjacent rows. So, for example, I want to average the pixels (vertically) in rows 1 and 2, and then replace the pixel values with the average values, then repeat for row 3 and 4, rows 5 and 6, etc. How I can do this?
0 Comments
Accepted Answer
Image Analyst
on 17 Jan 2022
Use blockproc():
% 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;
fontSize = 16;
%===============================================================================================================================
% Read in a standard MATLAB gray scale demo image.
folder = fileparts(which('cameraman.tif'));
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, 1, 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 2 pixel by 1 pixel block
% Block process the image to replace every pixel in the
% 2 pixel by 1 pixel block by a single pixel whose value is the mean of the pixels in the block.
blockSize = [2, 1];
blockyImage = blockproc(grayImage, blockSize, meanFilterFunction); % Works.
[rows, columns] = size(blockyImage);
% Display the Block Mean Output Image.
subplot(2, 1, 2);
imshow(blockyImage, []);
axis('on', 'image');
%impixelinfo;
caption = sprintf('Block Mean Output Image\nInput block size = %d x %d, output block size = %d x %d\nOutput image is %d rows by %d columns', ...
blockSize(1), blockSize(2), blockSize(1), blockSize(2), rows, columns);
title(caption, 'FontSize', fontSize);
drawnow; % Force screen to refresh immediately.
More Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!