How to divide a bmp image into blocks?

2 ビュー (過去 30 日間)
Chandvi Arora
Chandvi Arora 2017 年 7 月 18 日
編集済み: Guillaume 2017 年 7 月 21 日
I want to divide a bmp image to a 8*8 blocks. Is there any way out? 'resize' only resizes the image but does not divide the image into blocks. Also, when I read the image( using imread), it should give me a 8*8 matrix.

採用された回答

Guillaume
Guillaume 2017 年 7 月 18 日
編集済み: Guillaume 2017 年 7 月 18 日
Assuming your image size is a multiple of 8:
img = imread('C:\somewhere\someimage.bmp');
numblockheight = size(img, 1) / 8;
numblockwidth = size(img, 2) / 8;
assert(all(mod([numblockwidth, numblockheight], 1) == 0), 'Image size is not a multiple of 8')
imageblocks = mat2cell(img, ones(1, numblockheight) * 8, ones(1, numblockwidth) * 8, size(img, 3))
And to save all these:
destinationfolder = 'C:\somewhere';
for col = 1:size(imageblocks, 2)
for row = 1:size(imageblocks, 1)
imwrite(imageblocks{row, col}, fullfile(destinationfolder, sprintf('block%03d_%03d.bmp', row, col)));
end
end
  10 件のコメント
Chandvi Arora
Chandvi Arora 2017 年 7 月 21 日
@Guillaume Your code shows error as shown in the image attached. How can it be resolved?
Guillaume
Guillaume 2017 年 7 月 21 日
Please copy and paste errors instead of posting screenshots.
There was a missing bracket in my code. You (or matlab suggestion) added it in the wrong spot. I've fixed it above
Spending some time trying to understand what the code is doing would have allowed you to fix it yourself.

サインインしてコメントする。

その他の回答 (1 件)

Image Analyst
Image Analyst 2017 年 7 月 18 日

Community Treasure Hunt

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

Start Hunting!

Translated by