Display image circular by defining the outside of the circle, how?

조회 수: 6 (최근 30일)
nadine
nadine 2012년 12월 9일
이동: DGM 2023년 2월 12일
Hello, with the following i have plotted a big circle on my image
img = imread('image');
figure,imshow(img)
hold on
Xc= 512; %Center for x
Yc = 512; %Center for y
R = 510; %This is the radius
x=0:0.01:1; %degree
circle=plot(Xc+R*cos(2*pi*x),Yc+R*sin(2*pi*x),'Color','g','LineWidth',0.2);
Now i want to define the outside area of this circle to be transparent (Alpha). How do i tell matlab to only take the outside of the circle and not the hole image. With kind regards, thank you

답변 (3개)

Image Analyst
Image Analyst 2012년 12월 9일
Feel free to adapt my masking demo:
% Puts up an ellipse and masks and crops the image to the ellipse.
clc; % Clear command window.
% clear; % Delete all variables.
close all; % Close all figure windows except those created by imtool.
imtool close all; % Close all figure windows created by imtool.
workspace; % Make sure the workspace panel is showing.
fontSize = 14;
% Change the current folder to the folder of this m-file.
% (The line of code below is from Brett Shoelson of The Mathworks.)
if(~isdeployed)
cd(fileparts(which(mfilename)));
end
% Change the current folder to the folder of this m-file.
if(~isdeployed)
cd(fileparts(which(mfilename)));
end
% Display images to prepare for the demo.
monochromeImage = imread('pout.tif');
subplot(2, 3, 1);
imshow(monochromeImage);
title('Original Image', 'FontSize', fontSize);
subplot(2, 3, 2);
imshow(monochromeImage);
title('Original Image with ellipse in overlay', 'FontSize', fontSize);
set(gcf, 'units','normalized','outerposition',[0 0 1 1]); % Maximize figure.
set(gcf,'name','Image Analysis Demo','numbertitle','off')
%----- Burn ellipse into image -----
% Create elliptical mask, h, as an ROI object over the second image.
subplot(2, 3, 2);
hEllipse = imellipse(gca,[70 15 90 140]); % Second argument defines ellipse shape and position.
% Create a binary image ("mask") from the ROI object.
maskImage = hEllipse.createMask();
% Display the ellipse mask.
subplot(2, 3, 3);
imshow(maskImage);
title('Binary mask of the ellipse', 'FontSize', fontSize);
% Mask the image with the ellipse.
maskedImage = monochromeImage .* cast(maskImage, class(monochromeImage));
% Display the image with the "burned in" ellipse.
subplot(2, 3, 4);
imshow(maskedImage);
title('New image masked by the ellipse', 'FontSize', fontSize);
% Find the bounding box
column1 = find(sum(maskImage, 1), 1, 'first')
column2 = find(sum(maskImage, 1), 1, 'last')
row1 = find(sum(maskImage, 2), 1, 'first')
row2 = find(sum(maskImage, 2), 1, 'last')
croppedImage = maskedImage(row1:row2, column1:column2);
subplot(2, 3, 5);
imshow(croppedImage);
title('Ellipse portion cropped out', 'FontSize', fontSize);
  댓글 수: 6
nadine
nadine 2012년 12월 12일
이동: DGM 2023년 2월 12일
And it does, i wasnt reading thouroughly. THANK YOU very much! My next would then be to take the cropped out part of the last image, the circular one and place it onto the original image, which i will put an alpha value on , is that correct? Hope i explained clearly
Image Analyst
Image Analyst 2012년 12월 12일
이동: DGM 2023년 2월 12일
This is not an answer. You should follow up on my answers with a "comment", not post an answer of your own.

댓글을 달려면 로그인하십시오.


Image Analyst
Image Analyst 2012년 12월 9일
Or you can try this demo which is pretty much straight from the FAQ: http://matlab.wikia.com/wiki/FAQ#How_do_I_create_a_circle.3F
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
imtool close all; % Close all imtool figures.
clear; % Erase all existing variables.
workspace; % Make sure the workspace panel is showing.
format longg;
format compact;
fontSize = 20;
img = imread('moon.tif');
subplot(2,2,1);
imshow(img)
axis on;
% Create a logical image of a circle with specified
% diameter, center, and image size.
% First create the image.
[imageSizeY, imageSizeX] = size(img);
[columnsInImage rowsInImage] = meshgrid(1:imageSizeX, 1:imageSizeY);
% Next create the circle in the image.
centerX = 150;
centerY = 225;
radius = 51;
circlePixels = (rowsInImage - centerY).^2 ...
+ (columnsInImage - centerX).^2 <= radius.^2;
% circlePixels is a 2D "logical" array.
% Now, display it.
subplot(2,2,2);
imshow(circlePixels, []);
axis on;
axis image;
title('Binary image of a circle');
% Mask the image. Blacken inside the circle
maskedImage = img; % Initialize
maskedImage(circlePixels) = 0;
subplot(2,2,3);
image(maskedImage) ;
axis on;
axis image;
title('Masked Image');

Rafal Kasztelanic
Rafal Kasztelanic 2012년 12월 9일
편집: Walter Roberson 2012년 12월 10일
Try this:
if true
[x,y] = meshgrid(1024);
c = sqrt((x-Xc).^2+(y-Yc).^2);
img1 = (c>R).*img;
end
RaK

카테고리

Help CenterFile Exchange에서 Images에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by