フィルターのクリア

How do I save masks in roipoly with their original size?

3 ビュー (過去 30 日間)
Marco Beccarini
Marco Beccarini 2018 年 4 月 17 日
移動済み: DGM 2022 年 11 月 27 日
I'm using roipoly to segment the same structure (a vessel) in many frames of a video. Using my code, I often need to zoom in the image to draw the polygon on the contour of the structure. When I save the mask created, what I get is a mask of the zoomed in image, not a mask of the original one. When I open all the masks created I therefore have the "white area" always in a different position, depending on how I zoomed in. What I would like is to always get the original size image mask. How can I do that?
Here's my code:
clear all
close all
clc
images = dir('*.jpg');
N=length(images);
for i=1:N
I=imread(images(i).name);
mask=roipoly(I);
imshow(I);
hold on;
contour(mask) ;
imshow(mask);
filename = ['Segmented ', num2str(i), '.png'];
saveas(1, filename)
hold off
imshow(I);
hold on
contour(mask, 'y', 'LineWidth', 2);
filename=['Contour ', num2str(i), '.jpg'];
saveas(1, filename);
end
Thank you!
  2 件のコメント
Arun Mathamkode
Arun Mathamkode 2018 年 4 月 20 日
I tried your code in R2017b and it works properly. Mask is also in the proper size. I have zoomed-in using the zoom option in the figure for zooming. Did you also followed the same workflow?
Marco Beccarini
Marco Beccarini 2018 年 4 月 20 日
編集済み: Marco Beccarini 2018 年 4 月 20 日
Yes, we followed the same workflow. But we managed to save them correctly by clicking Ctrl+Z before double clicking the contour to confirm the segmentation. Thank you for your answer!

サインインしてコメントする。

採用された回答

Marco Beccarini
Marco Beccarini 2018 年 4 月 21 日
移動済み: DGM 2022 年 11 月 27 日

Actually, doing that it wouldn't save the mask zoomed in, but it would save it with the hosting figure's dimensions. That's because "saveas" saves the figure and not the image in it. So to have a mask with the same dimensions of the original image, I changed the code into (using imwrite):

 clear all
close all 
clc
 images = dir('*.jpg');
 N=length(images);
 for i=1:N
    I=imread(images(i).name);
    mask=roipoly(I);
    imshow(I);
    hold on;
    contour(mask);
    if size(mask)==size(I(:,:,1))
        filename = ['Segmented ', num2str(i+1), '.png'];
        imwrite(mask, filename);
    else 
        fprintf("errore");
    end
    hold off
    imshow(I);
    hold on
    contour(mask, 'y', 'LineWidth', 2);
    filename=['Contour ', num2str(i+1), '.jpg'];
    saveas(1, filename);
end

その他の回答 (0 件)

Community Treasure Hunt

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

Start Hunting!

Translated by