0

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

Picture of cells with one lymphocyte cell

After running my code, the result is

the result image

How can I save the image on subplot(2,1,2)?

Cecilia
  • 4,512
  • 3
  • 32
  • 75
  • Please do not post screenshots of code. Images cannot be searched, cannot be copied for testing, and impede accessibility. Instead, include relevant code as [formatted text](http://stackoverflow.com/help/formatting). – Meyer Dec 19 '16 at 15:10
  • thanks for your advice @Meyer – Zilvanhisna Fitri Dec 19 '16 at 15:24
  • Huh? You mean how to save `highResThumbnail`? `save myfile.mat highResThumbnail`? – Ander Biguri Dec 19 '16 at 15:47
  • as you can see, highResThumbnail shown in subplot (2,1,2). I want a picture on a subplot (2,1,2) can be save into 'file.jpg' but I do not know how, if anyone knows how to do it, I would be very grateful – Zilvanhisna Fitri Dec 19 '16 at 16:18

1 Answers1

0

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
Cecilia
  • 4,512
  • 3
  • 32
  • 75
  • I tried using the code that you suggest. I added B = imcrop A (starty: endy, startx: endx, :); but I could not crop the picture, did I miss something or there is something wrong, please correct back @Cecilia. thank you – Zilvanhisna Fitri Dec 20 '16 at 14:07
  • I didn't recommend using [`imcrop`](https://www.mathworks.com/help/images/ref/imcrop.html) and you didn't use it in the original code you posted. Are you trying something new? Using `imcrop` would be an alternative to `imrect` for creating the `highresthumbnail` matrix in your example code or the B matrix in my example code, but since you already have that working I'm not sure why you think you need it. – Cecilia Dec 21 '16 at 15:45