views:

316

answers:

1

Hello,

I'm a beginner in MATLAB and image processing.

I encountered a problem while trying to use the batch processing and hope someone would be able to enlighten me. Thanks.

Following the example from MATLAB, I did these:

p = which('Picture1.tif');
filelist = dir([fileparts(p) filesep 'Picture*.tif']);
fileNames = {filelist.name}'

I = imread(fileNames{1});
imshow(I)

Because I wanted to select the region of interest,

BW = roipoly(I);
BW1 = not(BW);
N = roifill(I,BW1);

After I selected the ROI, I created a function in the editor:

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

Back to the command window, I typed;

Segout = DetectLines(N);
figure, imshow(Segout)

The figure that came out was what I expected.

The problem comes now when I try to loop over images. I'm not sure if I've done it correctly.

Following the example, I created another function in the editor;

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

At the command window, I typed:

SegoutSequence = BatchProcessFiles(fileNames, @DetectLines);
implay(SegoutSequence)

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.

Picture1:

alt text

Picture1 after selecting ROI:

alt text

+4  A: 

Lookin at your code, you only selected the ROI once for the image you tested individually.

However, when you call the BatchProcessFiles function, you dont select a region of interest, and the DetectLines function is applied to the raw images... Therefore you need to pass the mask you created with roipoly to your BatchProcessFiles function to do the same on all the images.

As a side note, you might get better results if you try Hough transformation to detect the lines.
Also your code breaks if the images are not grayscale (you can add a call to rgb2gray() to be on the safe side)

Sample solution:

MATLAB

I = imread(fileNames{1});
BW = not( roipoly(I) );

SegoutSequence = BatchProcessFiles(fileNames, BW, @DetectLines);
implay(SegoutSequence)

BatchProcessFiles.m

function SegoutSequence = BatchProcessFiles(fileNames, Mask, fcn)
    % ...
    parfor (k = 1:nImages)
        N = imread(fileNames{k});
        if ndims(N)==3, N=rgb2gray(N); end
        if ~isempty(Mask), N = roifill(N, Mask); end
        SegoutSequence(:,:,k) = fcn(N);
    end
end

or call it as: BatchProcessFiles(fileNames, [], @DetectLines) if you don't need to apply the mask

Amro
+1: Should solve the problem, and I also believe a hough transform would be a better approach to solve the problem.
kigurai