Matlab:如何保存图像结果使用IMRECT选择的ROI的实时绘图

时间:2016-12-19 15:01:13

标签: matlab image-processing roi

我很担心如何通过调用imrect来节省ROI。我想将图片保存在subplot(2,1,2)

代码是:

function Zoomer
figure();

highResImage = imread('E:\My Work\THESISQ\FIX\Koding\data coba\Image_3060.jpg');
lowResImage = imresize(highResImage,0.5);

a1 = subplot(2,1,1);
a2 = subplot(2,1,2);

imshow(lowResImage,'Parent',a1);
initialPosition = [10 10 100 100];
lowResRect = imrect(a1,initialPosition);

lowResRect.addNewPositionCallback( @(pos)Callback(pos,a2,highResImage));

Callback( initialPosition , a2, highResImage);
end

function Callback(position,axesHandle, highResImage)
position = position * 2;
x1 = position(1);
y1 = position(2);
x2 = position(1) + position(3);
y2 = position(2) + position(4);

highResThumbnail = highResImage( round(y1:y2),round(x1:x2),:);

if isempty( get(axesHandle,'Children')) 
    imshow(highResThumbnail,'Parent',axesHandle);   
else
    imHandle = get(axesHandle,'Children');
    oldSize = size(get(imHandle,'CData'));
    if ~isequal(oldSize, size(highResThumbnail))
        imshow(highResThumbnail,'Parent',axesHandle);
    else
        set( imHandle,'CData', highResThumbnail);
    end     
end
end

这是我的照片,我想使用该代码在该图片上裁剪淋巴细胞

Picture of cells with one lymphocyte cell

运行我的代码后,结果是

the result image

如何将图像保存在subplot(2,1,2)

1 个答案:

答案 0 :(得分:0)

编辑:我最初过快地阅读了您的问题,看起来您已经知道如何使用imrect中的坐标从图像中提取子矩阵,因为您成功提取了显示highResThumbnail。您唯一缺少的是如何将矩阵保存为图像。

使用imwritehighResThumbnail保存为任何支持的图像格式

例如

imwrite(highResThumbnail, 'thumb.png');

如何使用imrect来获取图像

imrect命令仅为您提供x和y坐标以及宽度和高度。保存所选矩形。使用坐标索引到图像矩阵。然后,您可以使用imwrite保存子矩阵。

A = imread('peppers.png');
figure, imshow(A);
h = imrect;
position = wait(h); %Pause until user double clicks on rectangle

%position contains [x y width height]
startx = position(1);
endx = position(1)+position(3);
starty = position(2);
endy = position(2)+position(4);

%Use the coordinates to grab a submatrix of A
B = A(starty:endy, startx:endx, :);
imwrite(B, 'pepper_rect.png');

对于子图,您只能在活动子图上选择一个矩形。您可以通过再次调用subplot命令在子图之间切换。

%Plot all my images
figure;
subplot(2,1,1);
imshow('peppers.png');

subplot(2,1,2);
imshow('saturn.png');

%Active image is the most recently plotted
h = imrect;
position = wait(h); %Pause until user double clicks on rectangle

%Call subplot again to activate first image
subplot(2,1,1);
h = imrect;
position = wait(h); %Pause until user double clicks on rectangle