How to reduce an image given a factor?
Answers (1)
14 Comments
If you want to code it from scratch, then the simplest way is to loop over entire image, ectract blocks and do average as you explained in the question. Following resources will help you to do this.
https://www.mathworks.com/help/matlab/ref/for.html
https://www.mathworks.com/company/newsletters/articles/matrix-indexing-in-matlab.html
output=zeros(image(1)/factor, image(2)/factor, class(input));
@Carlos, Although there can much efficient implementations, based on the code you gave, here is one implementation from top of my head,
function [output]=im_sample(input, factor)
input = double(input);
% image=size(entrada);
output = nan(size(input, 1), size(input,2));
for i=1:factor:size(input, 1)-factor
for j=1:factor:size(input, 2)-factor
output(i,j)= mean(mean(input(i:i+factor,j:j+factor)));
end
end
nans = isnan(output);
output(all(nans'), :) = []; output(:, all(nans)) = [];
output = uint8(output);
But this will only work with integer values of factor. For better implementation, you might want to look into some other algorithms.
Here is a better implementation based on interp2 function and can take real numbers as factor input.
function [output]=im_sample(input, factor) rows = size(input, 1); cols = size(input, 2); rowsFactor = resample(1:cols, floor(cols/factor), cols); colsFactor = resample(1:rows, floor(rows/factor), rows);
[x, y] = meshgrid(1:cols, 1:rows);
[x_, y_] = meshgrid(rowsFactor, colsFactor); output = interp2(x, y, double(input), x_, y_, 'nearest');
output = uint8(output);
endCategories
Find more on Computer Vision with Simulink in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!