0

I have data set from images and each image dim is 240x180 , I want to make some processes on each image but after dividing each image into sub-images (blocks) and each block must have this size 20x20 so I should have 108 blocks from each image and I wrote this code but I don't know why the size of result only contain 84 blocks from the original image , it must 108 blocks and the conditions inside loop is correctly .

Img=imread('imagefile.extension'); % read image 240x180
[r c]=size(Img);  % r=180 , c=240
bs=20; % Each Block Size (20x20)

nob=(r/bs)*(c/bs); %The total number of 20x20 Blocks = 108

% Dividing the image into 20x20 Blocks
BNo=0;
for i=1:(r/bs)
for j=1:(c/bs)
Block(:,:,BNo+j)=Img((bs*(i-1)+1:bs*(i-1)+bs),(bs*(j-1)+1:bs*(j-1)+bs));
end
BNo=BNo+(r/bs);
end
Alaa Khaled
  • 301
  • 1
  • 3
  • 10
  • 1
    Do you really need the blocks, or just the result of applying some processing to each block? To get the blocks as slices of a 3D or 4D array, see [here](http://stackoverflow.com/questions/20336288/for-loop-to-split-matrix-to-equal-sized-sub-matrices) – Luis Mendo Mar 21 '16 at 19:30
  • 4
    duplicate? http://stackoverflow.com/q/11238828/97160 – Amro Mar 21 '16 at 19:31
  • @LuisMendo I need to 108 blocks from each image to make some process(MAD) on each block and store the result at array – Alaa Khaled Mar 21 '16 at 19:37
  • @AlaaKhaled Check my link then. It does just that. If that's what you need, let me know to close this as duplicate – Luis Mendo Mar 21 '16 at 19:37
  • @LuisMendo I tried to use mat2cell before that but it gives to me error because the length of width and height of my image is not the same and all examples that are seen for mat2cell talk the same dim for width and height , this is true ? – Alaa Khaled Mar 21 '16 at 19:42
  • @AlaaKhaled The linked answers assume all blocks have the same size. But that also happens in your case, right? – Luis Mendo Mar 21 '16 at 19:45
  • @LuisMendo in my case the dim is 240x180 and it gives error to me when try to applying mat2cell , I know mat2cell is more easy and better but I don't know if it's working with different dimensions or not because it's not working with me – Alaa Khaled Mar 21 '16 at 19:48
  • 1
    @AlaaKhaled it should, your image is 240x180 and you want non-overlapping blocks of size 20x20, right? since both dimensions are divisible by 20 you should get `prod([240 180] ./ [20 20]) == 108` blocks. pretty straightforward. – Amro Mar 21 '16 at 20:00
  • @LuisMendo I solved that by starting BNo with 1 and then replace BNo+j with only BNo and at each loop increase BNo by 1 , Thanks – Alaa Khaled Mar 21 '16 at 21:25

0 Answers0