S=imread(img2.gif);
for img=1:100;
C = mat2cell(S,[10 10],[10 10]);
plot(C);
end
I want to write a matlab code that divides an image into subimages 10x10 (original image is 100x100)
Asked
Active
Viewed 976 times
-1

lennon310
- 12,503
- 11
- 43
- 61
-
If your image is not colormap-based, see here: http://stackoverflow.com/questions/20336288/for-loop-to-split-matrix-to-equal-sized-sub-matrices – Luis Mendo Dec 03 '13 at 15:10
-
1so? do you want to ask something in particular? `mat2cell` is a good start - maybe you should read its documentation. The remainder of your code looks pretty confused... – sebastian Dec 03 '13 at 15:10
-
S=imread('img1.gif'); [x,y] = size(S); for i = 1:20:x for j = 1:20:y tmp = S(i:(i+19), j:(j+19)); % Do something interesting with "tmp" here. end end figure colormap((gray)); imagesc(tmp); this code is perfect but with a 10x10 not fuction. – user3061770 Dec 03 '13 at 18:09
1 Answers
0
The following code will separate the image into sub images of a specified size.
im = imread('peppers.png');
widthSub = 10;
heightSub = 10;
numHeightFull = floor(size(im,1)/heightSub);
numWidthFull = floor(size(im,2)/widthSub);
if mod(size(im,1),heightSub) == 0
heights = heightSub*ones(1,numHeightFull);
else
heights = [heightSub*ones(1,numHeightFull),mod(size(im,1),heightSub)];
end
if mod(size(im,2),widthSub) == 0
widths = widthSub*ones(1,numWidthFull);
else
widths = [widthSub*ones(1,numWidthFull),mod(size(im,2),widthSub)];
end
if ndims(im) < 2
subImages = mat2cell(im,heights,widths);
elseif ndims(im) > 2
subImages = mat2cell(im,heights,widths,size(im,3));
end

MZimmerman6
- 8,445
- 10
- 40
- 70