how to manual crop an ultrasound image

I have an ultrasound iamge and I should a rectangual ROI manually like Ithis image:
but when I use imcrop it does not plot the image to choose an ROI. However, by giving the location of the rectangle it will work:
to make it more clear I mean using this line of the code:
e1=imcrop(I,[0 1100 368 3000]);
but I need to do this ROI selection in the same way as the above image.
I attached my image.

 Accepted Answer

See my demo.
% Demo to show how drawrectangle can be used to draw a rectangular box on the image.
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 20;
rgbImage = imread('peppers.png');
hFig = figure;
imshow(rgbImage);
hFig.WindowState = 'maximized';
% Ask user to draw rectangle.
button = 'Redraw';
while contains(button, 'Redraw', 'IgnoreCase',true)
uiwait(helpdlg('Draw a box'));
% User draws a box. It exits as soon as they lift the mouse.
hBox = drawrectangle('Color', 'r');
% Get the coordinates in the form [xLeft, yTop, width, height].
roiPosition = hBox.Position;
% Delete the ROI object.
delete(hBox);
% and replace it with a rectangle in the graphical overlay.
hold on;
hRect = rectangle('Position', roiPosition, 'EdgeColor', 'r', 'LineWidth', 2);
% Ask user if the rectangle is acceptable.
message = sprintf('Is this good?');
button = questdlg(message, message, 'Accept', 'Redraw', 'Reject and Quit', 'Accept');
if contains(button, 'Quit','IgnoreCase',true)
delete(hRect); % Delete the box from the overlay.
roiPosition = [];
break;
elseif contains(button, 'Redraw','IgnoreCase',true)
% OPTIONAL If you want to delete the prior one before drawing the next one.
delete(hRect);
elseif contains(button, 'Accept','IgnoreCase',true)
break;
end
end
% If you want to delete the rectangle from the overlay, do this:
delete(hRect); % Delete the box from the overlay.
% Show roiPosition in command window
roiPosition
% Crop out the rectangle into a new image.
croppedImage = imcrop(rgbImage, roiPosition);
% Display original image
subplot(2, 1, 1);
imshow(rgbImage);
axis('on', 'image')
title('Original Image', 'FontSize', fontSize)
% Display box over original image.
hold on;
rectangle('Position', roiPosition, 'EdgeColor', 'r', 'LineWidth', 2);
% Display cropped image.
subplot(2, 1, 2);
imshow(croppedImage);
axis('on', 'image')
title('Cropped Image', 'FontSize', fontSize)
% Close down figure.
% close(hFig);

More Answers (1)

I always use this code to draw a roi:
figure; imshow(image); axis equal
title('Crop image based on first image taken')
set(gcf, 'WindowState', 'maximized');
% set(gcf, 'Position', get(0, 'Screensize'));
roi = drawrectangle;
f = msgbox('Press on OK if finished cropping','Finished?');
uiwait(f)
Hope this helps :)

7 Comments

then after selecting the ROI how can I do my other steps on it? for example applying a filter just on the roi.
cropped_image = imcrop(image, roi.Position)
maybe your problem is just inserting roi instead of roi.Position?
If this doesn't work try
roi = roi.roi;
cropped_image = imcrop(image, roi.Position);
2NOR_Kh
2NOR_Kh on 5 Aug 2022
Edited: 2NOR_Kh on 5 Aug 2022
Thank you Mathias.
This line of the code just produces a 2 rows matrix: cropped_image = imcrop(image, roi.Position)
The other line gives an error:
Unrecognized method, property, or field 'roi' for class 'images.roi.Rectangle'.
imcrop() supports interactive selection of a rectangular area without the need to use images.roi objects. Just call imcrop() using the short syntax:
[croppedimage roirect] = imcrop(myimage);
That will open a figure with the image displayed. You can mark out a rectangular area, adjust as needed. Right-click -> crop image. The output is the cropped image and the coordinates of the selected area. Said coordinates can be re-used if necessary.
2NOR_Kh
2NOR_Kh on 6 Aug 2022
Edited: 2NOR_Kh on 6 Aug 2022
But it's not working for all the images, I attached my image. The problem is when we plot image, there is just a white plot. I attached my image to this message.
The image is improperly-scaled for its class. It should be expected that it won't display correctly unless it's scaled appropriately. I can only guess what the scale is supposed to be.
[croppedimage roirect] = imcrop(mat2gray(e3)); % normalize to current extrema
or
[croppedimage roirect] = imcrop(uint8(e3)); % presume that the image is uint8() cast as double
Even if it's desired to keep the current class and scaling, you can use the above to get the rectangle coordinates and then apply that to the unaltered array.
[~,roirect] = imcrop(mat2gray(e3));
croppedimage = imcrop(e3,roirect);

Sign in to comment.

Categories

Products

Release

R2021a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!