使用drawfreehand绘制感兴趣区域后如何裁剪图像?

时间:2019-07-23 21:25:34

标签: matlab

我有一张图片,我想根据ROI进行裁剪。 https://imgur.com/rsoCXsf

我使用了freefreehand函数绘制感兴趣区域(ROI)。我想在ROI下裁剪区域并将其保存在其他文件中。

I = imread('Intensity1.jpg');
imshow(I);
h = drawfreehand; % now pick ROI

BW = createMask(h); % get BW mask for that ROI
pos = images.roi.Freehand(); % get position for that ROI

% define bounding box
x1 =  round(min(pos(:,2)));
y1 =  round(min(pos(:,1)));
x2 =  round(max(pos(:,2)));
y2 =  round(max(pos(:,1)));

I2 = I.*uint8(BW); % apply mask to image
I2 = I2(x1:x2,y1:y2);

figure;
subplot(1,2,1);
imshow(I);
subplot(1,2,2);
imshow(I2);
I3 = imcrop(I2);
imshow(I3)

显示以下错误消息。

类“ images.roi.Freehand”的无法识别的方法,属性或字段“ roi”。

fh4错误(第6行) pos = images.roi.Freehand(); %获得该投资回报率的职位

1 个答案:

答案 0 :(得分:0)

请注意,如果有人问这样的问题,最好用每个人都可以使用的某些功能替换图像文件的加载

ROI的坐标存储在Position返回的对象的drawfreehand属性中:

imagesc(peaks(128))
h = drawfreehand;
pos = h.Position

更新:以下是使用您的图片的完整示例:

I = imread('https://i.imgur.com/rsoCXsf.jpg');

f = figure();
imshow(I);
roi = drawfreehand();


mask = uint8(roi.createMask());
I2 = I .* mask; % apply mask
f2 = figure();
subplot(1,2,1);
image(I2);

% get BB
pos = roi.Position;
x1 =  round(min(pos(:,2)));
y1 =  round(min(pos(:,1)));
x2 =  round(max(pos(:,2)));
y2 =  round(max(pos(:,1)));

Icropped = I2(x1:x2, y1:y2, :);

subplot(1,2,2);
image(Icropped);