Filter löschen
Filter löschen

Extract RGB values, shape and size of many objects in all images inside a folder.

1 Ansicht (letzte 30 Tage)
Hi! May I know how to extract RGB values, major and minor diameter, and area of many seeds from different images in a folder using a single script? Please see attached sample images. Any help will be appreciated. Thank you so much!
  1 Kommentar
Rik
Rik am 9 Mai 2023
Start writing the code to do each task separately and then put those together. I would discourage the use of scripts for actual work. Use scripts only for debugging and use functions for code you plan to use next week or later.

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Antoni Garcia-Herreros
Antoni Garcia-Herreros am 9 Mai 2023
Bearbeitet: Antoni Garcia-Herreros am 9 Mai 2023
Hello Maria,
You can start by taking a look at the function regionprops.
But the general idea would be to:
  1. Read an image
  2. Make the appropiate transformations to input it to regionprops
  3. Filter the data from regionprops to avoid capturing small particles
  4. Store those results.
Something like this:
clear all
close all
%% Input parameters
filefolder='C:\Users\...'; % Folder where your images are stored
Area_thr=1000; % Area threshold, only seed larger than this value [pixels] will be considered
plot_options=1; % 1 If plot is to be shown, 0 otherwise
%% Code
files=dir([filefolder '*.jpg']); % Structure containing all the .jpg images
MinD=[]; % Array with Min diameters values
MaxD=[]; % Array with Max diameters values
Area=[]; % Array with Area values
for i=1:numel(files) % Loop through the images in the folder
I=imread(fullfile(files(i).folder,files(i).name)); % read image
if size(I,3)>1 % Convert to grayscale
I=rgb2gray(I);
end
BW=imbinarize(I); % Convert to binary
BW=imcomplement(BW); % Make the seeds white instead of black
se = strel('disk',20);
closeBW = imclose(BW,se);
r=regionprops('table',closeBW,'Area','Centroid','MajorAxisLength','MinorAxisLength'); % Find all regions
M=table2array(r);
M=M(M(:,1)>Area_thr,:); % Filter rgions based on Area threshold
MinD=[MinD;M(:,5)]; % Store data
MaxD=[MaxD;M(:,4)];
Area=[Area;M(:,1)];
if plot_options==1
imshow(closeBW)
hold on
plot(M(:,2),M(:,3),'*')
hold off
end
end

Weitere Antworten (1)

Image Analyst
Image Analyst am 9 Mai 2023
It's a generic, general purpose demo of how to threshold an image to find blobs, and then measure things about the blobs, and extract certain blobs based on their areas or diameters.
  2 Kommentare
Maria Pauline Capiroso
Maria Pauline Capiroso am 9 Mai 2023
Yes @Image Analyst. I'm currently studying your demo. Thank you so much for the guidance :)
Image Analyst
Image Analyst am 9 Mai 2023
If you want diameter (Equivalent Circular Diameter) then you'd want to ask regionprops for "EquivDiameter".
I would not use MajorAxisLength because it's the length of an ellipse fitted to the seed, not the length of the actual seed itself. For that you'd need to call bwferet
help bwferet
BWFERET Measure Feret diameters and angles of image regions. OUT = BWFERET(I) measures the maximum Feret Properties of each component (object) in the image. I can be a binary image, connected component or labeled matrix. OUT = BWFERET(BW, PROPERTIES) measures a set of Feret properties of each connected component (object) in the binary image BW, which is a logical array. OUT = BWFERET(CC, PROPERTIES) measures a set of Feret properties of each connected component (object) in CC, which is a struct returned by BWCONNCOMP. OUT = BWFERET(L, PROPERTIES) measures a set of Feret properties of each labeled component (object) in the label matrix L. Positive integer elements of L correspond to different regions. For example, the set of elements of L equal to 1 corresponds to region 1; the set of elements of L equal to 2 corresponds to region 2; and so on. [OUT, L] = BWFERET(...) measures a set of Feret properties of each component (object) in the binary image, connected component or labeled matrix. This also returns the corresponding label matrix L such that the first value in the table OUT corresponds to the labeled region 1, the second value with the labeled region 2 and so on. PROPERTIES can be an array of strings, a single character vector, a cell array of character vectors, or 'all'. If PROPERTIES is set to 'all' it returns all the Feret properties mentioned below. If no argument is provided, all maximum Feret properties are given as outputs. The set of valid measurement strings or character vectors includes 'MaxFeretProperties', 'MinFeretProperties' and 'all'. 'MaxFeretProperties' - Outputs all the properties related to maximum Feret Diameter. These properties are: MaxDiameter - Maximum Feret diameter length. MaxAngle - Angle of maximum Feret diameter with respect to X axis in degrees. The value lies between 180 to -180 degrees. MaxCoordinates - Endpoint coordinates of maximum Feret diameter. 'MinFeretProperties' - Outputs all the properties related to minimum Feret Diameter. These properties are: MinDiameter - Minimum Feret diameter length. MinAngle - Angle of minimum Feret diameter with respect to X axis in degrees. The value lies between 180 to -180 degrees. MinCoordinates - Endpoint coordinates of minimum Feret diameter. Class Support ------------- If the first input is BW, BW must be a logical array and it should be 2D. If the first input is CC, CC must be a structure returned by BWCONNCOMP. If the first input is L, L must be real, nonsparse and 2D. L can have any numeric class. The output OUT is returned as a table. Example 1 --------- % Calculate the minimum Feret diameter for objects in image I = imread('toyobjects.png'); bw = imbinarize(I, 'adaptive'); % Retain the two biggest objects in the image bw = bwareafilt(bw, 2); bw = imfill(bw, 'holes'); % Calculate the Feret properties of the objects in the image along with % the label matrix [out, L] = bwferet(bw, 'MinFeretProperties'); Example 2 --------- % Plot the maximum Feret diameter for objects in the image % Read an image I = imread('toyobjects.png'); % Binarize the image B = imbinarize(I, 'adaptive'); % Fill in the holes in the binary image B = imfill(B, 'holes'); % Show the image h = imshow(B) ax = h.Parent; % Convert to connected component struct using bwconncomp C = bwconncomp(B); % Calculate Feret Properties F = bwferet(C, 'MaxFeretProperties'); hold on % Display maximum Feret Diameters with their values for each object imdistline(ax, F.MaxCoordinates{1}(:,1), F.MaxCoordinates{1}(:,2)); imdistline(ax, F.MaxCoordinates{2}(:,1), F.MaxCoordinates{2}(:,2)); imdistline(ax, F.MaxCoordinates{3}(:,1), F.MaxCoordinates{3}(:,2)); imdistline(ax, F.MaxCoordinates{4}(:,1), F.MaxCoordinates{4}(:,2)); See also BWCONNCOMP, BWLABEL, BWLABELN, LABELMATRIX, REGIONPROPS. Documentation for bwferet doc bwferet

Melden Sie sich an, um zu kommentieren.

Community Treasure Hunt

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

Start Hunting!

Translated by