How to crop an image in a circular shape ?

14 visualizaciones (últimos 30 días)
Mohamed Khaled
Mohamed Khaled el 14 de Abr. de 2021
Comentada: DGM el 14 de Dic. de 2023
I'm trying to crop an MRI image (shown below) that has an excess (unwanted) pixels that will affect my calculations, using [Icropped = imcrop(I)] command still affects my calculations as it only crops the image in rectangular shape that leaves unwanted areas around the corners of the image. Is there any command or code that could help crop an image in circular shape ?
Image:

Respuesta aceptada

Image Analyst
Image Analyst el 15 de Abr. de 2021
Try this:
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;
%--------------------------------------------------------------------------------------------------------
% READ IN IMAGE
folder = pwd;
baseFileName = 'image.png';
grayImage = imread(baseFileName);
% Get the dimensions of the image.
% numberOfColorChannels should be = 1 for a gray scale image, and 3 for an RGB color image.
[rows, columns, numberOfColorChannels] = size(grayImage)
if numberOfColorChannels > 1
% It's not really gray scale like we expected - it's color.
% Use weighted sum of ALL channels to create a gray scale image.
grayImage = min(grayImage, [], 3);
end
%--------------------------------------------------------------------------------------------------------
% Display the image.
subplot(2, 2, 1);
imshow(grayImage, []);
axis('on', 'image');
title('Gray Scale Image', 'FontSize', fontSize, 'Interpreter', 'None');
impixelinfo;
hFig = gcf;
hFig.WindowState = 'maximized'; % May not work in earlier versions of MATLAB.
drawnow;
% Show the histogram
subplot(2, 2, 2);
imhist(grayImage);
grid on;
title('Histogram of Image', 'FontSize', fontSize, 'Interpreter', 'None');
% Turn into a binary image
threshold = 40;
xline(threshold, 'color', 'r', 'LineWidth', 3);
% binarize the image.
binaryImage = grayImage < 40;
% Get rid of blobs touching border.
binaryImage = imclearborder(binaryImage);
% Fill holes
binaryImage = imfill(binaryImage, 'holes');
% Take the largest blob
binaryImage = bwareafilt(binaryImage, 1);
% Display the image.
subplot(2, 2, 3);
imshow(binaryImage, []);
axis('on', 'image');
title('Binary Image', 'FontSize', fontSize, 'Interpreter', 'None');
impixelinfo;
drawnow;
% Mask the gray scale image so that it's black outside.
grayImage(~binaryImage) = 0;
% Display the image.
subplot(2, 2, 4);
imshow(grayImage, []);
axis('on', 'image');
title('Masked Image', 'FontSize', fontSize, 'Interpreter', 'None');
impixelinfo;
drawnow;
% Find boundaries
% Plot the borders of all the blobs in the overlay above the original grayscale image
% using the coordinates returned by bwboundaries().
% bwboundaries() returns a cell array, where each cell contains the row/column coordinates for an object in the image.
% Here is where we actually get the boundaries for each blob.
boundaries = bwboundaries(binaryImage);
% boundaries is a cell array - one cell for each blob.
% In each cell is an N-by-2 list of coordinates in a (row, column) format. Note: NOT (x,y).
% Column 1 is rows, or y. Column 2 is columns, or x.
numberOfBoundaries = size(boundaries, 1); % Count the boundaries so we can use it in our for loop
% Here is where we actually plot the boundaries of each blob in the overlay.
hold on; % Don't let boundaries blow away the displayed image.
for k = 1 : numberOfBoundaries
thisBoundary = boundaries{k}; % Get boundary for this specific blob.
x = thisBoundary(:,2); % Column 2 is the columns, which is x.
y = thisBoundary(:,1); % Column 1 is the rows, which is x.
plot(x, y, 'r-', 'LineWidth', 2); % Plot boundary in red.
end
hold off;
caption = sprintf('%d Outlines, from bwboundaries()', numberOfBoundaries);
title(caption, 'FontSize', fontSize);
axis('on', 'image'); % Make sure image is not artificially stretched because of screen's aspect ratio.
  3 comentarios
Image Analyst
Image Analyst el 14 de Dic. de 2023
@Alexandre if you have x and y of the boundary, you can extract the rectangle inside
xMin = min(x);
xMax = max(x);
yMin = min(y);
yMax = max(y);
croppedImage = grayImage(yMin:yMax, xMin:xMax);
Or alternatively if you have the mask, you can do
props = regionprops(mask, 'BoundingBox');
croppedImage = imcrop(grayImage, props.BoundingBox);
DGM
DGM el 14 de Dic. de 2023
It's not clear how what you're asking for is any different than what @Image Analyst already demonstrated.
Do you mean that you want to crop the image to the smallest circumscribed rectangle containing the masked area?
% a single-channel image and a logical mask
inpict = imread('cameraman.tif'); % I, uint8
mask = imread('cmancircmask.png'); % logical mask
montage({inpict mask}) % show them
% get the mask extents and crop the mask
[outmask,rows,cols] = crop2box(mask);
% crop the image and apply the mask
outpict = double(inpict(rows,cols)).*outmask;
outpict = cast(outpict,class(inpict));
% show the result
imshow(outpict,'border','tight')
The attached version of MIMT crop2box() is simplified so as to work without MIMT tools.

Iniciar sesión para comentar.

Más respuestas (0)

Community Treasure Hunt

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

Start Hunting!

Translated by