Error Creating ROI mask

조회 수: 10 (최근 30일)
Cristhian Dejesus
Cristhian Dejesus 2022년 6월 21일
답변: Saffan 2023년 9월 7일
Im trying to creake multiple binary masks from a bathc of pictures but when i try to use the create mask function when inside a bigger loop an error saying pops out
Error using images.roi.internal.mixin.CreateMask/validateInputs
createMask expects a current figure containing an image.
Error in images.roi.Freehand/createMask
Error in multiplesegmentation (line 34)
BW = createMask(mask);
Here its my code to note when doing this process one by one it doesnt has this problem
clc;
close all;
imtool close all;
clear;
srcFile=dir('C:...\*.jpg');
for i=1:length(srcFile)
filename=strcat('C:...\',srcFile(i).name);
img=imread(filename);
%figure,imshow(k);
path=strcat('C:...\save\',srcFile(i).name);
I=rgb2gray(img);
se = strel('disk',45);
backgroun = imopen(I,se);
I2 = I - backgroun;
I3 = imadjust(I2);
segmentedLabels = imsegkmeans(I3,3);
boneMask = segmentedLabels==3;
%imshowpair(im, boneMask);
boneMask1 = bwareafilt(boneMask, 2);
%imshowpair(im, boneMask);
%title('SEG1 ');
blocations = bwboundaries(boneMask1,'noholes');
%figure,imshow(img, []);
for ind = 1:numel(blocations)
% Convert to x,y order.
pos = blocations{ind};
pos = fliplr(pos);
% Create a freehand ROI.
mask=drawfreehand('Position', pos);
BW = createMask(mask);
morph= imopen(BW,se);
%imshow(morph);
end
B = imoverlay(img,morph);
%imwrite(B,path);
figure,imshow(B);
title('nail found ');
end

답변 (1개)

Saffan
Saffan 2023년 9월 7일
Hi Cristhian,
The createMask function requires the current figure in MATLAB to contain the image on which the ROI is drawn. In the provided code, the error occurs because the function is being called within a loop where multiple ROIs are drawn. However, the image associated with each ROI is not present in the current figure, leading to the error.
To resolve this issue, we need to ensure that the image is displayed in the current figure before creating the masks. This can be achieved by creating a new figure for each image as shown in the following code snippet:
figure; % Create a new figure for each image
imshow(img, []);
hold on;
for ind = 1:numel(blocations)
pos = blocations{ind};
pos = fliplr(pos);
mask = drawfreehand('Position', pos);
BW = createMask(mask);
morph = imopen(BW, se);
end
hold off;
Please refer to this for more information:

태그

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by