convert larger image into patches
5 views (last 30 days)
Show older comments
hi there,
I currently have a large image sized 600x1000 and would like to instead read this as patches of 51x51, how can I do this?
have tried to reshape but number of elements in each doesn't match to an integer
thanks very much
0 Comments
Answers (2)
Ameer Hamza
on 1 Dec 2020
You can use this FEX package: https://www.mathworks.com/matlabcentral/fileexchange/35085-mat2tiles-divide-array-into-equal-sized-sub-arrays
img = rand(600,1000);
img_parts = mat2tiles(img, [51 51])
2 Comments
Ameer Hamza
on 2 Dec 2020
You can remove those cells
img = rand(600,1000);
img_parts = mat2tiles(img, [51 51]);
idx = ~cellfun(@(x) all(size(x)==[51 51]), img_parts);
img_parts(:,all(idx,1)) = [];
img_parts(all(idx,2),:) = [];
Image Analyst
on 1 Dec 2020
What exactly do you mean by "read this"? Do you want to resize the entire image to 51x51 with imresize(yourImage, [51, 51]) or do you want to scan the image with a 51x51 window, moving in jumps of 51 pixels, and process the image inside, like you'd do with blockproc(). I'm attaching images for blockproc.
If the image is not a perfect multiple of 51, what do you want to do with the left over sliver?
2 Comments
Image Analyst
on 2 Dec 2020
OK, so simply crop the image before blockproc() with indexing. Just compute the number of blocks.
numBlocksR = size(yourImage, 1);
numBlocksC = size(yourImage, 2);
yourImage = yourImage(1 : (floor(numBlocksR/51) * 51), 1 : (floor(numBlocksC/51) * 51), :);
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!