title: How to get the boundary of the bubble in the water and output the coordinate using MATLAB?
body: <p>everyone. I am trying to get to boundary dimension of the bubble inside the water using MATLAB. The code and result are shown below.</p>
  
  <pre><code>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)))&gt;threshold  ; 
                i3(i,j)=1;
          else abs((im2(i,j))-(im1(i,j)))&lt;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');
  </code></pre>
  
  <p><img src="https://i.stack.imgur.com/HUOtd.jpg" alt="Fig"> </p>
  
  <p>As the figure shows, the result is not very satisfactory. So can anyone give me some advice to improve my result? And how can I output the boundary coordinate to file and get the real dimension of the bubble? Thank you very much!</p>
  
  <p><img src="https://i.stack.imgur.com/5zhVG.jpg" alt="background"></p>
  
  <p><img src="https://i.stack.imgur.com/Wt9be.jpg" alt="bubble"></p>