创建一个循环以创建手绘 ROI

Create a loop in order to create freehand ROIs

我完成了以下代码,从胶片中导入帧,转换为灰度并徒手绘制感兴趣的区域。然后,我使用 Chan-Vese 区域填充来获取我感兴趣的区域,并以此为基础创建一个蒙版。我终于可以得到我正在寻找的二进制图像,在代码中称为 BW3。现在这是愚蠢的部分。我如何创建一个循环,以便代码 运行(加载帧 1 到 58)向我显示帧 1 的灰度图像,允许我绘制感兴趣的区域,然后创建并保存最终的二进制文件图片BW3? 问候, J

    % Select Initial Image
for n = 5:87
frame = read(obj,n);


%Isolate the Blade
imggray = rgb2gray(frame);
h_im=imshow(imggray);

%Region of interest
% r = imrect(gca,[646,188,18,-648]);
% BW2 = createMask(r,h_im);

hROI = imfreehand(gca);
Position = getPosition(hROI);
BW2 = createMask(hROI);

%Get blade Binary

BW3 = activecontour(imggray, BW2, 1000, 'Chan-Vese');

% Fill Holes
BW3 = imfill(BW3, 'holes');

% Form masked image from input image and segmented image.
maskedImage = h_im;
maskedImage(~BW3) = 0;

%Save binary frame
filename = sprintf('C:..........\binaryimage%d.png', n);
imwrite(BW3,filename,'png');
end

假设您的帧数有限(即最多 58 个),您可以只使用 for 循环来完成此操作。在您的代码中,您必须更改初始图像代码,以便它在循环中迭代。

for counter=1:58
    frame=read(obj,counter); 
    imwrite(frame,strcat('frame',num2str(counter),'.png'))
    .....% the remainder of your code likely remains unchanged.
end

在编写结果时,您可能需要将其放入某种结构中,以便循环遍历,除非您在每个循环中将结果写入文件。