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 display highResThumbnail
. The only piece you are missing is how to save a matrix as an image.
Use imwrite
to save highResThumbnail
to any supported image format
For example
imwrite(highResThumbnail, 'thumb.png');
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 with 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');
For subplots, you can only select a rectangle on the active subplot. You can switch between subplots by calling the subplot
command again.
%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