error in box averaging filter of an image using matlab -
i need downsize image using box averaging filter , tried write code there error : unknown command option.
, error , right algorithm box filter know thought of ,the new pixel = averaging 4 neighboring pixels . code:
clear, clc image=imread('p128.jpg'); old_size=size(image); out_image=zeros(old_size(1)/2 , old_size(2)/2); = 1 : old_size(1) - 1 j= 1 : old_size(2) - 1 k= : i+1 t= j : j+1 out_image(k,t)=(image(i,j)+image(i+1,j)+image(i,j+1)+... image(i+1,j+1))/4 ; end end end end figure(1), imshow(out_image)
you can utilize im2cols
'distinct'
image processing toolbox re-arrange windows elements columns , calculate average of each column, represent average of each window. now, in comments said don't want utilize function ip toolbox, have replaced im2cols
our own custom made implementation. thus, assuming im
input grayscale image data, can utilize -
box_len = 2; %// length of box im_cols = im2col_distinct(im,[box_len box_len]); im_downsized = uint8(reshape(mean(im_cols,1),size(im,1)/box_len,[]));
associated function -
function out = im2col_distinct(a,blocksize) nrows = blocksize(1); ncols = blocksize(2); nele = nrows*ncols; row_ext = mod(size(a,1),nrows); col_ext = mod(size(a,2),ncols); padrowlen = (row_ext~=0)*(nrows - row_ext); padcollen = (col_ext~=0)*(ncols - col_ext); a1 = zeros(size(a,1)+padrowlen,size(a,2)+padcollen); a1(1:size(a,1),1:size(a,2)) = a; t1 = reshape(a1,nrows,size(a1,1)/nrows,[]); t2 = reshape(permute(t1,[1 3 2]),size(t1,1)*size(t1,3),[]); t3 = permute(reshape(t2,nele,size(t2,1)/nele,[]),[1 3 2]); out = reshape(t3,nele,[]); return;
thus, avoiding messy nested loops original code.
input :
output :
image matlab matlab-figure
No comments:
Post a Comment