Divide Image into Overlapping blocks and save it

4 ビュー (過去 30 日間)
Tahir Mahmood
Tahir Mahmood 2019 年 3 月 25 日
編集済み: Ashish Uthama 2022 年 9 月 8 日
Hi, I want to divide an image into 3 overlapping block.
input image size=2084 by 2084, output image sizes should be 2084 by 1042 with 50% overlapping. I tried using blockproc but encountering an error, "Too many output arguments.".
How to divide this image into blocks and save each by different name?.
I followed the demo, but these demos are for non-overlapping. I want overlapping blocks. Thank you.
This is my code:
for i=1:1
img=double(imread(strcat(folder,num2str(i),'.jpg')));
aa=blockproc(img, [2084 1042], cropSaveBlock,'BorderSize', [2084 521])
end
function cropSaveBlock()
end

回答 (2 件)

Ashish Uthama
Ashish Uthama 2022 年 9 月 8 日
編集済み: Ashish Uthama 2022 年 9 月 8 日
input image size=2084 by 2084, output image sizes should be 2084 by 1042% with 50% overlapping
Here is some sample code:
im = ones(2084);
% blockedImage can directly load an image too.
bim = blockedImage(im);
% See help selectBlockLocations for more examples.
blockSize = [2084 1042];
overlapPct = 0.5;
blockOffsets = round(blockSize.*overlapPct);
bls = selectBlockLocations(bim,...
'BlockSize', blockSize,...
'BlockOffSets', blockOffsets,...
'ExcludeIncompleteBlocks', true);
% Create a datastore from this set of blocks
bimds = blockedImageDatastore(bim, 'BlockLocationSet', bls);
%{
% Visualize the block locations to confirm logic
bigimageshow(bim)
% Block size is in row-col (height-width) order
blockWH = fliplr(bls.BlockSize(1,1:2));
colors = prism(size(bls.BlockOrigin,1));
for ind = 1:size(bls.BlockOrigin,1)
blockColor = colors(ind,:);
% BlockOrigin is already in x-y order
drawrectangle('Position', [bls.BlockOrigin(ind,1:2), blockWH],'Color', blockColor);
end
%}
% You can use this datastore (bimds) directly depending on your workflow.
% Read each block and write it out
while hasdata(bimds)
[data, info] = read(bimds);
imwrite(data{1}, "blockStart_"+num2str(info.Start)+".jpg")
end
It looks like you may have multiple images, you can extend this workflow by making an array of blockedImages (see here)

Benjamin Thompson
Benjamin Thompson 2022 年 6 月 13 日
An image is just a matrix. If you want to save a part, then
imwrite(img(1:2084,1:1042), 'file1.jpg', 'jpg');
imwrite(img(1042:2084,524:1042), 'file2.jpg', 'jpg');

カテゴリ

Help Center および File ExchangeImage Processing and Computer Vision についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by