Putting cropped images in seperate folder

조회 수: 1 (최근 30일)
Abdussalam Elhanashi
Abdussalam Elhanashi 2021년 3월 3일
답변: Shivang Srivastava 2021년 3월 24일
Hi,
I cropped images with size 200 x 200 into 4 tiles with 50 x 50 for each tile and i want to make these generated tiles into seperated folder
Herein the code
close all;
clc;
%% Initalize the data
dataDir= fullfile('Data/');
exts = {'.jpg','.png','.tif','BMP'};
imds = imageDatastore(fullfile(dataDir),...
'IncludeSubfolders',true,'FileExtensions','.jpg','LabelSource','foldernames');
countEachLabel(imds);
[TrainData, TestData] = splitEachLabel(imds,0.5);
size(TrainData);
countEachLabel(TrainData);
numImages = numel(TrainData.Files);
dx = 50;
dy = 50;
w = dx - 1;
h = dy - 1;
for i = 1:numImages
img = readimage(TrainData, i);
img=rgb2gray(img);
img3= im2double(img);
scale = 0.5;
img8 = imresize(img3,scale);
for x = 0:2
for y = 0:2
ofs_x = 1 + x * dx;
ofs_y = 1 + y * dy;
img_crop = imcrop(img8,[ofs_x ofs_y w h]);
%img4 = imshow(img_crop, 'InitialMagnification',800);
%drawnow;
idx = (i-1)*4 + y*2 + x + 1;
% 1 2 4 5
% 6 7 8 9
% 10 11 12 13
% 14 15 16 17
Train{idx} = (img_crop); %#ok<SAGROW>
end
end
end
The code Train{idx} is the for generated tiles 50 x 50
Looking for your support

답변 (1개)

Shivang Srivastava
Shivang Srivastava 2021년 3월 24일
Hi Abdussalam,
As per my understanding you need to put the cropped tiles in Train variable into another folder. I am assuming that you are trying to do that as an .jpg file. The below code would help you to the same.
mkdir croppedImages
for i = [1:size(Train,2)]
K = mat2gray(Train{i});
imwrite(K,"croppedImages/"+string(i)+".jpg",'jpg');
end

카테고리

Help CenterFile Exchange에서 Image Data Workflows에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by