gpt4 book ai didi

matlab - 如何使用MATLAB获取水中气泡的边界并输出坐标?

转载 作者:行者123 更新时间:2023-12-02 08:06:05 25 4
gpt4 key购买 nike

大家。我正在尝试使用 MATLAB 获得水中气泡的边界尺寸。代码和结果如下所示。

clear;
clc;
i1=imread('1.jpg');
i2=imread('14.jpg');
% i1=rgb2gray(i1);
% i2=rgb2gray(i2);
[m,n]=size(i1);
im1=double(i1);
im2=double(i2);
i3=zeros(size(i1));
threshold=29;
for i=1:m;
for j=1:n;
if abs((im2(i,j))-(im1(i,j)))>threshold ;
i3(i,j)=1;
else abs((im2(i,j))-(im1(i,j)))<threshold;
i3(i,j)=0;
end
end;
end;
se = strel('square', 5);
filteredForeground = imopen(i3, se);
figure; imshow(filteredForeground); title('Clean Foreground');
BW1 = edge(filteredForeground,'sobel');
subplot(2,2,1);imshow(i1);title('BackGround');
subplot(2,2,2);imshow(i2);title('Current Frame');
subplot(2,2,3);imshow(filteredForeground);title('Clean Foreground');
subplot(2,2,4);imshow(BW1);title('Edge');

Fig

如图所示,结果不是很理想。那么有人可以给我一些建议来改善我的结果吗?以及如何将边界坐标输出到文件并获得气泡的真实尺寸?非常感谢!

background

bubble

最佳答案

首先请注意,您的背景删除几乎没有用。

如果我们绘制diffI=i2-i1; imshow(diffI,[]);colorbar,我们可以看到差异几乎和图像本身一样大。您需要了解它在视觉上与您相似的东西,但不一定在数值上相似,这就是一个很好的例子。

enter image description here

因此,您并不拥有您认为拥有的东西。背景在你的阈值中。然后,请注意您要分割的对象,它不仅仅是更白。在某些区域,它绝对和背景一样暗。这意味着按值阈值进行简单分割将行不通。您需要更好的分割技术。

我碰巧有一份 this level set algorithm在我的 MATLAB 中,“Distance Regularized Level Set Evolution”。

当我使用您的图像运行代码 demo_1 时,我得到以下信息(漂亮的 gif!):

enter image description here

( Uncompressed )

演示的完整代码:

%  This Matlab code demonstrates an edge-based active contour model as an application of 
% the Distance Regularized Level Set Evolution (DRLSE) formulation in the following paper:
%
% C. Li, C. Xu, C. Gui, M. D. Fox, "Distance Regularized Level Set Evolution and Its Application to Image Segmentation",
% IEEE Trans. Image Processing, vol. 19 (12), pp. 3243-3254, 2010.
%
% Author: Chunming Li, all rights reserved
% E-mail: lchunming@gmail.com
% li_chunming@hotmail.com
% URL: http://www.imagecomputing.org/~cmli//

clear all;
close all;


Img=imread('/image/Wt9be.jpg');

Img=double(Img(:,:,1));

%% parameter setting
timestep=1; % time step
mu=0.2/timestep; % coefficient of the distance regularization term R(phi)
iter_inner=5;
iter_outer=300;
lambda=5; % coefficient of the weighted length term L(phi)
alfa=-3; % coefficient of the weighted area term A(phi)
epsilon=1.5; % papramater that specifies the width of the DiracDelta function

sigma=.8; % scale parameter in Gaussian kernel
G=fspecial('gaussian',15,sigma); % Caussian kernel
Img_smooth=conv2(Img,G,'same'); % smooth image by Gaussiin convolution
[Ix,Iy]=gradient(Img_smooth);
f=Ix.^2+Iy.^2;
g=1./(1+f); % edge indicator function.

% initialize LSF as binary step function
c0=2;
initialLSF = c0*ones(size(Img));
% generate the initial region R0 as two rectangles
initialLSF(size(Img,1)/2-5:size(Img,1)/2+5,size(Img,2)/2-5:size(Img,2)/2+5)=-c0;
% initialLSF(25:35,40:50)=-c0;
phi=initialLSF;



potential=2;
if potential ==1
potentialFunction = 'single-well'; % use single well potential p1(s)=0.5*(s-1)^2, which is good for region-based model
elseif potential == 2
potentialFunction = 'double-well'; % use double-well potential in Eq. (16), which is good for both edge and region based models
else
potentialFunction = 'double-well'; % default choice of potential function
end

% start level set evolution
for n=1:iter_outer
phi = drlse_edge(phi, g, lambda, mu, alfa, epsilon, timestep, iter_inner, potentialFunction);
if mod(n,2)==0
figure(2);
imagesc(Img,[0, 255]); axis off; axis equal; colormap(gray); hold on; contour(phi, [0,0], 'r');
drawnow

end
end

% refine the zero level contour by further level set evolution with alfa=0
alfa=0;
iter_refine = 10;
phi = drlse_edge(phi, g, lambda, mu, alfa, epsilon, timestep, iter_inner, potentialFunction);

finalLSF=phi;
figure(2);
imagesc(Img,[0, 255]); axis off; axis equal; colormap(gray); hold on; contour(phi, [0,0], 'r');
hold on; contour(phi, [0,0], 'r');
str=['Final zero level contour, ', num2str(iter_outer*iter_inner+iter_refine), ' iterations'];
title(str);

关于matlab - 如何使用MATLAB获取水中气泡的边界并输出坐标?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51439632/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com