필터 지우기
필터 지우기

Assign values outside a circle in an image to zero

조회 수: 4 (최근 30일)
Nilesh
Nilesh 2022년 11월 23일
댓글: Nilesh 2022년 11월 25일
Hello everyone,
I have this image generated from a camera, which is a 2048 by 2048 matrix, called meanB. Each cell in the matrix has its own value, but I have to set the values outside the circle, as shown in the diagram to zero.
I know the centre coordinate of the image (1024,1024), and I know the radius of the circle (1024).
Does any one have any suggestion?
Kind regards,
Nilesh

채택된 답변

Image Analyst
Image Analyst 2022년 11월 23일
편집: Image Analyst 2022년 11월 23일
That's answered in the FAQ:
% Create a logical image of a circle with specified
% diameter, center, and image size.
% First create the image.
imageSizeX = 2048;
imageSizeY = 2048;
[columnsInImage rowsInImage] = meshgrid(1:imageSizeX, 1:imageSizeY);
% Next create the circle in the image.
centerX = 1024;
centerY = 1024;
radius = 1024;
circlePixels = (rowsInImage - centerY).^2 ...
+ (columnsInImage - centerX).^2 <= radius.^2;
% circlePixels is a 2D "logical" array.
% Now, display it.
image(circlePixels) ;
colormap([0 0 0; 1 1 1]);
title('Binary image of a circle');
Now that you have the circle mask, you can use it to blacken outside of the original RGB or gray scale image.
% Mask image by multiplying each channel by the mask.
maskedRgbImage = rgbImage .* cast(~mask, 'like', rgbImage); % R2016b or later. Works for gray scale as well as RGB Color images.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Image Segmentation and Analysis에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by