title: Batch Processing Image Files in MATLAB
body: <p>I'm a beginner in MATLAB and image processing.</p>
  
  <p>I encountered a problem while trying to use the batch processing and hope someone would be able to enlighten me. Thanks.</p>
  
  <p>Following the example from MATLAB, I did these:</p>
  
  <pre><code>p = which('Picture1.tif');
  filelist = dir([fileparts(p) filesep 'Picture*.tif']);
  fileNames = {filelist.name}'
  
  I = imread(fileNames{1});
  imshow(I)
  </code></pre>
  
  <p>Because I wanted to select the region of interest,</p>
  
  <pre><code>BW = roipoly(I);
  BW1 = not(BW);
  N = roifill(I,BW1);
  </code></pre>
  
  <p>After I selected the ROI, I created a function in the editor:</p>
  
  <pre><code>function Segout = DetectLines(N)
      [junk threshold] = edge(N, 'sobel');
      fudgeFactor = .5;
      BWs = edge(N, 'sobel', threshold*fudgeFactor);
      se90 = strel('line', 3, 90);
      se0 = strel('line', 3, 0);
      BWsdil = imdilate(BWs, [se90 se0]);
      BWdfill = imfill(BWsdil, 'holes');
      BWnobord = imclearborder(BWdfill, 4);
      seD = strel('diamond', 1);
      BWfinal = imerode(BWnobord, seD);
      BWfinal = imerode(BWfinal, seD);
      BWoutline = bwperim(BWfinal);
      Segout = N;
      Segout(BWoutline) = 255;
  end
  </code></pre>
  
  <p>Back to the command window, I typed;</p>
  
  <pre><code>Segout = DetectLines(N);
  figure, imshow(Segout)
  </code></pre>
  
  <p>The figure that came out was what I expected. </p>
  
  <p>The problem comes now when I try to loop over images. I'm not sure if I've done it 
  correctly.</p>
  
  <p>Following the example, I created another function in the editor;</p>
  
  <pre><code>function SegoutSequence = BatchProcessFiles(fileNames, fcn)
      N = imread(fileNames{1});
      [mrows, ncols] = size(N);
      nImages = length(fileNames);
      SegoutSequence = zeros(mrows, ncols, nImages, class(N));
      parfor (k = 1:nImages)
          N = imread(fileNames{k});
          SegoutSequence(:,:,k) = fcn(N);
      end
  end
  </code></pre>
  
  <p>At the command window, I typed:</p>
  
  <pre><code>SegoutSequence = BatchProcessFiles(fileNames, @DetectLines);
  implay(SegoutSequence)
  </code></pre>
  
  <p>However, the result was not what I wanted it to be. It was not the ROI that I wanted. Can anyone help me with this? Thank you very much.</p>
  
  <h3>Picture1:</h3>
  
  <p><img src="https://i182.photobucket.com/albums/x11/veronicafmy/FYP/Picture1.jpg" alt="alt text"></p>
  
  <h3>Picture1 after selecting ROI:</h3>
  
  <p><img src="https://i182.photobucket.com/albums/x11/veronicafmy/FYP/picture1-2afterroipoly.jpg" alt="alt text"></p>