How to lower resolution of a picture?
Show older comments
Write a program to upload an image in Matlab and reduce its size averaging a number (NxN) of pixels specified by the user. For example, if the image is 6x12 pixels and the user specifies N = 3, the new image will be 2x4 pixels and it will get averaging squares 3x3 pixels as shown in the figure. If the user specifies a value of N that is not a common divisor of In the 2 dimensions of the figure, the program should approximate N to the closest common divisor. In the title of his figure specify what was the value of N specified by the user and what was the value that was finally used.
1 Comment
Edgar Rivera
on 8 May 2018
Moved: Image Analyst
on 5 May 2023
Answers (2)
Image Analyst
on 8 May 2018
Edited: Image Analyst
on 8 May 2018
There is no figure attached.
Use imresize to specify the final output image size
outputImage = imresize(inputImage, [2, 4]);
or
[rows, columns, numColorChannels] = size(inputImage)
numOutputRows = round(rows/N);
numOutputColumns = round(columns/N);
outputImage = imresize(inputImage, [numOutputRows, numOutputColumns]);
if you have the N value from your user.
2 Comments
Casey
on 4 May 2023
imresize doesn't perform the averaging as requested. For example,
A =
0 0 0 0 0 1
0 0 0 0 0 0
0 0 0 0 0 0
0 1 1 1 0 0
B = imresize(A,[3,2])
Yields
B =
-0.0218 -0.0716 0.2965
0.2542 0.5384 0.0043
Whereas by simple averaging it should be
0 0 .25
.25 .5 0
Casey
on 4 May 2023
erfan's answer on this site seems to perform as requested:
https://stackoverflow.com/questions/39975720/matlab-scale-down-an-image-using-an-average-of-four-pixels
You can use blockproc to move in "jumps" of 2 pixels:
A = [
0 0 0 0 0 1
0 0 0 0 0 0
0 0 0 0 0 0
0 1 1 1 0 0];
%===============================================================================================================================
% MEAN of 2 pixel by 2 pixel block
% Block process the image to replace every pixel in the
% 2 pixel by 2 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 2 by 2 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 = [2, 2]; % Whatever window size you want.
B = blockproc(A, blockSize, meanFilterFunction)
Categories
Find more on Neighborhood and Block Processing in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!