How to extract only the object and get the extreme coordinates?

14 ビュー (過去 30 日間)
Rhandrey Maestri
Rhandrey Maestri 2023 年 11 月 26 日
コメント済み: Image Analyst 2024 年 3 月 13 日
Dear members, I would be truly grateful if you could assist me in extracting the object indicated in the image and obtaining the coordinates of its extreme points on the right and left. My goal is to calculate the distance between these extreme points and the centroid of the object. Could you please guide me on the correct approach for achieving this?
You can find the code I am working on, the tested image and a preliminar result with the elements that I am interested.
clc
clear all
close all
set(0, 'DefaultAxesFontName', 'Times New Roman');
set(0, 'DefaultUIControlFontName', 'Times New Roman');
set(0, 'defaultUitableFontName', 'Times New Roman');
set(0, 'defaultTextFontName', 'Times New Roman');
set(0, 'defaultUipanelFontName', 'Times New Roman');
font_size = 16;
line_width_size=2;
J=zeros;
number=1;
pixelTomm=50; %Physical scale conversion factor
%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%% importing Data %%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
for framenumbers=53:576
% defining the range of the number of the images
close all
clear_all_but('number','framenumbers','xcentroid1','ycentroid1','xfit_Leading','yfit_Leading','Rfit_Leading','xfit_trailing','yfit_trailing','Rfit_trailing','Thetaleading','Thetatrailing')
%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%% importing Data %%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
I = imread(sprintf('0000200test.jpg',framenumbers)); %Importing tif
I(:,:,3) = []; %%% removing the second and third layer of the image
I(:,:,2) = [];
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%% Plotting the original image %%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
figure(1), set(gcf,'color','w');
sz_x = 400; sz_y = 400; % size of the figure
y_pos = 600; x_pos =1; % position on the screen
set(1,'pos', [x_pos, y_pos, sz_x, sz_y]);
axes('position',[.1 .15 .8 .8]) % size of the plot in the figure
title('Original image')
figure (1)
hold on
imshow(I, []); %showing the raw image
axis on
%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%% Binarization of the image %%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
numberOfcolum = size(I, 1);% calculating the size of the raw image
numberOfrows = size(I, 2);% calculating the size of the raw image
J=zeros(numberOfcolum,numberOfrows);
% defining the matrix to store the Binarized image
for i=1:numberOfcolum
for j=1:numberOfrows
if I(i,j) < 37
%%%%%%%%%%%%%Masking the lower intensity%%%%%%%%%%
J(i,j)=0;
else
J(i,j)=1;
end
j=j+1;
end
i=i+1;
end
figure,imshow(J); %showing the black and white image
J = bwareaopen(J,20000); %Remove small objects from binary image
figure,imshow(J);
%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%% Detect Entire droplet and fill the hollow region %%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
[~, threshold] = edge(J, 'sobel'); %findig the edge of the bubble
fudgeFactor = .9;
BWs = edge(J,'sobel', threshold * fudgeFactor);
figure, imshow(BWs), title('binary gradient mask');
%showing the detected bubble
%%%%%%%%%%%%% Fill Interior Gaps %%%%%%%%%%%%
BWdfill = imfill(BWs, 'holes');
figure, imshow(BWdfill);
Preprocessed_Image = imcrop(BWdfill,[778.5 10.5 783 2038]);
%Cropping the original image to remove the white border
figure, imshow(BWdfill);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%% Plotting cropped image %%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
figure(2), set(gcf,'color','w');
sz_x = 400; sz_y = 400; % size of the figure
y_pos = 600; x_pos =401; % position on the screen
set(2,'pos', [x_pos, y_pos, sz_x, sz_y])
axes('position',[.1 .15 .8 .8]) % size of the plot in the figure
title('Pre-processed image')
figure(2)
imshow(Preprocessed_Image);
axis on
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% finding the centroid, leading and trailing edge of the bubble %%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
stats = regionprops('table',Preprocessed_Image,'Centroid',...
'MajorAxisLength','MinorAxisLength');
xcentroid=stats.Centroid(1,1); % x of the centroid
x_centroid(number)=xcentroid; % storing x-location
ycentroid=stats.Centroid(1,2); % y of the centroid
y_centroid(number)=ycentroid; % storing y-location
MinorAxisLength=stats.MinorAxisLength(1); % length of Minor axis
MajorAxisLength=stats.MajorAxisLength(1); % length of Minor axis
Thanks!
  3 件のコメント
Rhandrey Maestri
Rhandrey Maestri 2023 年 11 月 26 日
編集済み: Rhandrey Maestri 2023 年 11 月 26 日
can it be bmp? I tried .tif but the website did not allow me to upload it
Walter Roberson
Walter Roberson 2023 年 11 月 26 日
BMP is good.
It looks like there might be a background grid, possibly intended to help determine the sizes of objects? Is that grid 0.1mm spacing? At the moment I do not know how hard it would be to extract the grid, but if it is a known size and can be extracted, that would give us a way to calculate absolute sizes on the images instead of relative sizes.

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

採用された回答

Akira Agata
Akira Agata 2023 年 11 月 27 日
編集済み: Akira Agata 2023 年 11 月 27 日
How about the following?
% Load the image
I = imread('https://jp.mathworks.com/matlabcentral/answers/uploaded_files/1552757/2000000200.bmp');
% Binarize
BW = imbinarize(I);
% Extract the ROI
BW = ~BW;
se = strel("disk", 5);
BW = imopen(BW, se);
BW = imclearborder(BW);
BW = bwareafilt(BW, 1);
BW = imfill(BW, "holes");
% Calculate the centroid
s = regionprops(BW, "Centroid");
% Calculate the extreme points
idx = any(BW);
pt1 = find(idx, 1);
pt2 = find(idx, 1, "last");
% Visualize the result
imshow(I)
hold on
xline(pt1, "r", sprintf("X = %d", pt1), "FontSize", 18)
xline(pt2, "r", sprintf("X = %d", pt2), "FontSize", 18)
h = scatter(s.Centroid(1), s.Centroid(2), "r", "filled");
legend(h, "Centroid", "FontSize", 18)
  3 件のコメント
Fernando
Fernando 2024 年 3 月 13 日
I does not make sense to me, how "any" function get the extremes?
Image Analyst
Image Analyst 2024 年 3 月 13 日
@Fernando any() gives you the linear index of all white pixels in the binary image. It is a one dimensional list. Since the list is arranged column-wise, from upper left most pixel in left most column, to lower right pixel in the right most column, it will give you the leftmost and rightmost column, though not necessarily the uppermost and lower most row. That's why I suggested bwferet

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

その他の回答 (1 件)

Image Analyst
Image Analyst 2023 年 11 月 27 日
Try 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

カテゴリ

Help Center および File ExchangeRead, Write, and Modify Image についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by