首页 > 解决方案 > 使用drawfreehand绘制感兴趣区域后如何裁剪图像?

问题描述

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

我使用 drawfreehand 函数来绘制感兴趣区域(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(); % 获得该投资回报率的位置

标签: matlab

解决方案


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

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);

推荐阅读