MATLAB Answers


Image Processing trouble and masking. Is there a way to mask multiple images with a new mask every time in a loop?

Asked by Matt
on 18 Jun 2013

I have a stack of dicom images(about 600) in a 4-D array and based on the image I want to create a mask and apply it to that image for segmentation of the image. I want to have this done in a loop so I dont have to manually do it for each image.Can this be done? I have converted all the images to bw so to create a mask. I have tried but I am getting a error. Below Is part of the code that I have been using and the error. I get the error at qline1 = imline(gca, [x1(i,3) y1(i,3) ; x1(i,2) y1(i,2)]);

     % Drawing of lines for quadrants
for i = prim:fin;
    BW4(:,:,:,i) = imshow(BW3(:,:,:,i));
    qline1(i,1) = imline(gca, [x1(3,1,i) y1(3,1,i) ; x1(2,1,i) y1(2,1,i)]);
    M1(i,1) = createMask(qline1(i,1), BW4(:,:,:,i));
    qline2(i,1) = imline(gca, [x2(3,1,i) y2(3,1,i) ; x2(2,1,i) y2(2,1,i)]);
    M2(i,1) = createMask(qline2(i,1), BW4(:,:,:,i));
BW3(M1) = 0;
BW3(M2) = 0;
% Defining Ellipse 
r{i} = {tot,1};
h{i} = {tot,1};
ratio = 0.8;
for i = prim:fin;
    r{i} = regionprops(BW3(:,:,:,i), 'MajorAxisLength');
    h{i} = regionprops(BW3(:,:,:,i), 'MinorAxisLength');
    R(i,1) = ((extractfield(r{i}, 'MajorAxisLength'))*ratio);
    H(i,1) = ((extractfield(h{i}, 'MinorAxisLength'))*ratio);
% Drawing of Ellipse
loc1 = zeros(tot,1);
loc2 = zeros(tot,1);
for i = prim:fin;
   loc1(i,1) = cent(i,1);
   loc2(i,1) = cent(i,2); 
lc1 = loc1 - (R/2);
lc2 = loc2 - (H/2);
for i = prim:fin;
    vbEllipse(i) =  imellipse(gca, [lc1(i,1) lc2(i,1) R(i,1) H(i,1)]);
    xy(i) = vbEllipse(i).getVertices();
    axesHandlesToChildObjects = findobj(gca, 'Type', 'line');
    if ~isempty(axesHandlesToChildObjects);
    hold on;
    x(i,:) = xy(:,1,i);
    y(i,:) = xy(:,2,i);
    xCenter(i) = mean(x(i,:));
    yCenter(i) = mean(y(i,:));
    x(i,:) = x(i,:) - xCenter(i);
    y(i,:) = y(i,:) - yCenter(i);
    xy(:,:,i) = [x(i,:) y(i,:)];
    theta =  -rot(mid);
    rotationArray = [cosd(theta) -sind(theta); sind(theta) cosd(theta)];
    rotated_xy(:,:,i) = xy(:,:,i) * rotationArray;
    x(:,:,i) = rotated_xy(:,1,i) + xCenter(i);
    y(:,:,i) = rotated_xy(:,2,i) + yCenter(i);
    VBEllipse(:,:,i) = [x(:,:,i) y(:,:,i)];
    ellip(:,:,i) = impoly(gca, VBEllipse(:,:,i));
    M3(i) = ellip(:,:,i).createMask();
    BW3(M3) = 0;
BW5(M3) = 1;
BW5(~M3) = 0;
BW5(M1) = 0;
BW5(M2) = 0;
Error using iptSetPointerBehavior>parseInputs (line 135)
First input argument, h, contains one or more invalid handles.
Error in iptSetPointerBehavior (line 119)
[h, pointerBehavior] = parseInputs(varargin{:});
Error in manageInteractivePlacement/resetChildPointerManagement (line
Error in manageInteractivePlacement (line 106)
Error in imline>imlineAPI (line 258)
    placement_aborted =
Error in imline (line 89)
            [h_group,draw_api] = imlineAPI(varargin{:});
Error in im_seg2 (line 155)
    qline1(i,1) = imline(gca, [x1(3,1,i) y1(3,1,i) ; x1(2,1,i)

Thanks for the help



1 Answer

Answer by Sean de Wolski
on 19 Jun 2013

Make sure you use the:


syntax to draw the image on the existing axes. Otherwise, a new axes is created which is why your imroi disappears (I believe).


on 20 Jun 2013

ax is supposed to be the array of axes correct? But I'm not exactly sure how to create that array of axes from my array of images. Any advice?

Thank you for your time.


Sean de Wolski
on 21 Jun 2013

ax is supposed to be the handle to the axes you want to plot to.

Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi test

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

MATLAB Academy

New to MATLAB?

Learn MATLAB today!