How to crop and save objects from many images?
Show older comments
Hi! I generated the code to threshold objects from an image using the colorThresholder. Can someone help me on cropping each seed per image and save them as individual seed in a designated folder? I'll be using the cropped images as trained images later on. Thank you so much! :)))
function [BW, masked RGBImage] = createMask(RGB)
%createMask Threshold RGB image using auto-generated code from colorThresholder app.
% [BW,MASKEDRGBIMAGE] = createMask(RGB) thresholds image RGB using
% auto-generated code from the Thresholderapp. The colorspace and
% range for each channel of the colorspace were set within the app. The
% segmentation mask is returned in BW, and a composite of the mask and
% original RGB images is returned in maskedRGBImage.
% Auto-generated by colorThresholder app on 18-May-2023
% Convert RGB image to chosen color space
I = RGB;
% Define thresholds for channel 1 based on histogram settings
channel1Min = 31.000;
channel1Max = 122.000;
% Define thresholds for channel 2 based on histogram settings
channel2Min = 19.000;
channel2Max = 96.000;
%Define thresholds for channel 3 based on histogram settings
channel3Min = 3.000;
channel3Max = 75.000;
% Create mask based on chosen histogram thresholds
sliderBW = (I(:,:,1) >= channelMin ) & (I(:,:,1) <= channel1Max) &
(I(:,:,2) >= channel2Min ) & (I(:,:,2) <= channel2Max) &
(I(:,:,3) >= channel3Min ) & (I(:,:,3) <= channel3Max);
BW = sliderBW;
%Initialize output masked image based on input image.
maskedRGBImage = RGB;
%Set background pixels where BW is false to zero.
maskedRGBImage(repmat(~BW,[1 1 3])) = 0;
end
Accepted Answer
More Answers (1)
Antoni Garcia-Herreros
on 18 May 2023
Hello Maria,
You have two options, either crop the seed manually using drawrectangle or other ROI functions (drawpolygon)
Or create a cropping algorithm that creates a small image for each seed, the following would be a way to start:
clear all
folder_smallimage='C:\Users\...'; % Folder where your images will be stored
thr=50; % Threshold to filter small pixels, only retain the seed
RGB=imread('SB1.JPG');
% Convert RGB image to chosen color space
I = RGB;
% Define thresholds for channel 1 based on histogram settings
channel1Min = 31.000;
channel1Max = 122.000;
% Define thresholds for channel 2 based on histogram settings
channel2Min = 19.000;
channel2Max = 96.000;
%Define thresholds for channel 3 based on histogram settings
channel3Min = 3.000;
channel3Max = 75.000;
% Create mask based on chosen histogram thresholds
sliderBW = (I(:,:,1) >= channel1Min ) & (I(:,:,1) <= channel1Max) & ...
(I(:,:,2) >= channel2Min ) & (I(:,:,2) <= channel2Max) &(I(:,:,3) >= channel3Min ) &...
(I(:,:,3) <= channel3Max);
BW = sliderBW;
R=table2array(regionprops('table',BW,'Centroid','EquivDiameter','Area'));
R=R(R(:,1)>thr,:); % Filter out the small particles
Side_length=floor(1.5*max(R(:,4))); % Side length of the ROI
for i= 1:length(R) % Loop through seed
Small_IMG=BW(floor(R(i,3)-Side_length):floor(R(i,3)+Side_length),floor(R(i,2)-Side_length):floor( R(i,2)+Side_length));
% imshow(Small_IMG) % Plot if you want to doble check each ROI
% pause(0.1)
%
name=fullfile(folder_smallimage,[num2str(i) '.mat']);
save(name,'Small_IMG')
end
Hope this helps!
Categories
Find more on Image Processing and Computer Vision in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
