i'm trying to pixelated image using nested for loops what wrong here?regardless it's in comments
1 view (last 30 days)
Show older comments
%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
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]);
Answers (3)
Image Analyst
on 18 Oct 2017
Two more things wrong, other than what Matt said
- All lines are commented out
- The size call is wrong for a color image. It should be
[rows, cols, numberOfColorChannels] = size(img);
See Steve's blog: http://blogs.mathworks.com/steve/2011/03/22/too-much-information-about-the-size-function/
You can use conv2() or imfilter() to do it without loops if you're interested.
0 Comments
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.
0 Comments
See Also
Categories
Find more on Image Data Workflows 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!