I am confused about how to save the ROI resulting from calling imrect. I want to save the image on subplot(2,1,2)
The code is:
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
This is my picture and i want to crop the lymphocyte cell on this picture using that code
After running my code, the result is
How can I save the image on subplot(2,1,2)
?
Edit: I initially read your question too quickly, it looks like you already know how to extract the sub-matrix from your image using the coordinates from
imrect
because you successfully extract and displayhighResThumbnail
. The only piece you are missing is how to save a matrix as an image.Use
imwrite
to savehighResThumbnail
to any supported image formatFor example
How to use imrect to get a piece of an image
The
imrect
command only gives you x and y coordinates and width and height. To save that the selected rectangle. Use the coordinates to index into your image matrix. Then, you can save the sub matrix withimwrite
.For subplots, you can only select a rectangle on the active subplot. You can switch between subplots by calling the
subplot
command again.