![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1090155/image.png)
how to manual crop an ultrasound image
5 次查看(过去 30 天)
显示 更早的评论
I have an ultrasound iamge and I should a rectangual ROI manually like Ithis image:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1086165/image.png)
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.
0 个评论
采纳的回答
Image Analyst
2022-8-6
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);
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1090155/image.png)
0 个评论
更多回答(1 个)
Mathias Smeets
2022-8-2
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 个评论
DGM
2022-8-6
编辑:DGM
2022-8-6
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);
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Ultrasound Imaging 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!