title: morphological opening by reconstruction matlab code
body: <p>I should do morphological opening by reconstruction by my own Matlab code not <code>imreconstruct</code>. this is my code but it is not work well:</p>
  
  <pre><code>S = input('Enter the structuring element: ');
  Im = input('Enter the input image: ');
  
  marker = imerode(Im,S);
  mask = Im;
  
  Im2 = imdilate(marker,S);
  Im3 = min(Im2,Im);
  
  i=1;
  
  while Im3(i+1)~= Im3(i)
      i=i+1;
      Im2 = imdilate(Im3(i),S);
      Im3(i+1) = min(Im2,Im);
  end
  
  imrecon = Im3;
  </code></pre>
  
  <p>has anyone a better code or can edit my code? please help me.
  Thank you in advance.</p>