Matlab:如何保存图像结果 实时绘制用 IMRECT 选择的 ROI
Matlab: how to save the image result Real-time plotting of ROI selected with IMRECT
我对如何保存调用 imrect 产生的投资回报率感到困惑。我想将图像保存在 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
这是我的照片,我想使用该代码在这张图片上裁剪淋巴细胞
经过运行我的代码,结果是
如何在 subplot(2,1,2)
上保存图像?
编辑: 我最初读你的问题太快了,看起来你已经知道如何使用 imrect
中的坐标从图像中提取子矩阵因为您成功提取并显示 highResThumbnail
。您唯一缺少的是如何将矩阵保存为图像。
使用imwrite
将highResThumbnail
保存为任何支持的图像格式
例如
imwrite(highResThumbnail, 'thumb.png');
如何使用imrect获取一张图片
imrect
命令只提供 x 和 y 坐标以及宽度和高度。保存 selected 矩形。使用坐标索引到您的图像矩阵中。然后,你可以用 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');
对于子图,您只能在活动子图中 select 一个矩形。您可以通过再次调用 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
我对如何保存调用 imrect 产生的投资回报率感到困惑。我想将图像保存在 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
这是我的照片,我想使用该代码在这张图片上裁剪淋巴细胞
经过运行我的代码,结果是
如何在 subplot(2,1,2)
上保存图像?
编辑: 我最初读你的问题太快了,看起来你已经知道如何使用 imrect
中的坐标从图像中提取子矩阵因为您成功提取并显示 highResThumbnail
。您唯一缺少的是如何将矩阵保存为图像。
使用imwrite
将highResThumbnail
保存为任何支持的图像格式
例如
imwrite(highResThumbnail, 'thumb.png');
如何使用imrect获取一张图片
imrect
命令只提供 x 和 y 坐标以及宽度和高度。保存 selected 矩形。使用坐标索引到您的图像矩阵中。然后,你可以用 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');
对于子图,您只能在活动子图中 select 一个矩形。您可以通过再次调用 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