how to manual crop an ultrasound image

4 views (last 30 days)
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

Image Analyst
Image Analyst on 6 Aug 2022
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)

Mathias Smeets
Mathias Smeets on 2 Aug 2022
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
DGM
DGM on 6 Aug 2022
Edited: DGM on 6 Aug 2022
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.

Community Treasure Hunt

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

Start Hunting!

Translated by