How to reduce an image given a factor?
답변 (1개)
댓글 수: 14
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);
end카테고리
도움말 센터 및 File Exchange에서 Image Processing Toolbox에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!