How to extract only the object and get the extreme coordinates?
5 次查看(过去 30 天)
显示 更早的评论
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
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1552717/image.jpeg)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1552722/image.png)
Thanks!
3 个评论
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
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 个评论
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 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Read, Write, and Modify Image 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!