How to split a large image into many small images?

28 ビュー (過去 30 日間)
Syed JABBAR SHAH
Syed JABBAR SHAH 2021 年 6 月 17 日
回答済み: David Willingham 2021 年 6 月 17 日
Hi, I am working on CNN and I have dataset of large images. I want to split each image into many small images to perform training. Could you please tell me how to do it? To be exact, I want 24 small samples from one 1080 x 1920 image.
Further, is it possible to perform splitting in a imageDatastore? To be exact, I want 24 small samples from one 1080 x 1920 image.
Thanks
  2 件のコメント
Image Analyst
Image Analyst 2021 年 6 月 17 日
I don't believe imageDatastore() does any image processing -- it's just basically a fancy way of doing dir().
Do you want the samples to be tiled and non-overlapping? Or do you want them taken from random locations?
What I'm confused about is if you're going to use all these small sub-images as training images, how are you going to create your ground truth labels from them?
Syed JABBAR SHAH
Syed JABBAR SHAH 2021 年 6 月 17 日
Thanks for your comment. You have asked a valid question. Unfortunately. i am still a beginner in MATLAB CNN.
I explain my objective, that would help to better understand the problem. I have a dataset of 160 images of same size with two classes and I want to use CNN for classification. Since the input size is too big, I need to split them in tiled.
I have python code, and I am trying to repicate it in matlab. Please check the below code.
....
# Four splits in row and six splits in column -> 24 small samples from one 1080 x 1920 image
n_row = 4
n_col = 6
# Resize image to 256 x 256 pixels
img_size = 256
SMALL_IMG = []
for img_array, label, name in CNT_IMG:
for i in range(n_row):
for k in range(n_col):
height = int(img_array.shape[0]/n_row)
width = int(img_array.shape[1]/n_col)
small_img = img_array[i*height:(i+1)*height, k*width:(k+1)*width] # Split -> 270 x 320
small_img = cv2.resize(small_img, (img_size, img_size)) # Resize -> 256 x 256
# Normalization
small_img = small_img / 255.0
SMALL_IMG.append([small_img, label, name])
....
I would really appricate if you point me to the right direction and resources to solve the problem.
Thanks.

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

採用された回答

DGM
DGM 2021 年 6 月 17 日
編集済み: DGM 2021 年 6 月 17 日
Blockwise filtering has already been mentioned; since I don't know if that applies to your needs and I have no familiarity with IMDS, I'll just throw this out there.
If you just want to split an image, there are a bunch of ways. You could do it the long way.
inpict = imread('somerandompicture.jpg');
inpict = imresize(inpict,[1080 1920]); % you assert that it's this size
s = size(inpict);
tiling = [4 6]; % i'm assuming this is what you want
f=1;
sout=s(1:2)./tiling;
outpict=zeros([sout,size(inpict,3),prod(tiling)],class(inpict));
for n=1:tiling(2)
for m=1:tiling(1)
outpict(:,:,:,f)=inpict((1:sout(1))+((m-1)*sout(1)),(1:sout(2))+((n-1)*sout(2)),:);
f=f+1;
end
end
In this case, the output is a 4D array. You could use a cell array just the same, though if the goal is to use a cell, you could just do this:
inpict = imread('somerandompicture.jpg');
inpict = imresize(inpict,[1080 1920]); % you assert that it's this size
s = size(inpict);
tiling = [4 6]; % i'm assuming this is what you want
sout=s(1:2)./tiling;
C = mat2cell(inpict,ones(1,tiling(1))*sout(1),ones(1,tiling(2))*sout(2),3)
It's worth noting that both of these will break if your image geometry isn't integer-divisible by the tiling. MIMT imdetile() handles geometry mismatches of the sort, but I doubt you need to deal with it. Just check the geometry and resize as needed.

その他の回答 (2 件)

David Willingham
David Willingham 2021 年 6 月 17 日
Hi Syed,
I'd encourge you to use blockedImage along with blockedImagedatastore, it will help you perform the block operations for you.
David Willingham

Sulaymon Eshkabilov
Sulaymon Eshkabilov 2021 年 6 月 17 日
  1 件のコメント
DGM
DGM 2021 年 6 月 17 日
To reinforce the distinction, nlfilter() is a rectangular sliding-window filter, whereas blockproc() works on non-overlapping blocks.

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

カテゴリ

Help Center および File ExchangeImage Data Workflows についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by