i'm trying to pixelated image using nested for loops what wrong here?regardless it's in comments

1 view (last 30 days)
%img=imread(fileName);
%[rows,cols]=size(img);
%for i=1:rows
%for j=1:cols
%temp=img(i:pix,j:pix,[1 2 3]);
%img(temp)=mean(temp(:));
%%end
%end
  1 Comment
Matt J
Matt J on 18 Oct 2017
You haven't shown how 'pix' is defined, so we can't be sure what you are trying to do. It seems wrong, however. Did you mean,
temp=img(i:i+pix,j:j+pix,[1 2 3]);

Sign in to comment.

Answers (3)

Matt J
Matt J on 18 Oct 2017
img(i,j,1:3)=mean(temp(:));

Image Analyst
Image Analyst on 18 Oct 2017
Two more things wrong, other than what Matt said
  1. All lines are commented out
  2. The size call is wrong for a color image. It should be
[rows, cols, numberOfColorChannels] = size(img);
You can use conv2() or imfilter() to do it without loops if you're interested.

Matt J
Matt J on 18 Oct 2017
The code that you've shown tries to do sliding window averaging. I'm not sure that's what you want. That will smooth the image, not convert it to coarser, chunkier pixels. To get a more coarsely pixelized image, you can use MAT2TILES ( Download ).
Tiles=mat2tiles(img, [2,2,inf]);
img=cell2mat( cellfun(@(c) c*0+mean(c(:)) , Tiles, 'uni',0 ));
Note that both cell2mat and cellfun have internal for-loops, so this does use nested for-loop as you requested.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!