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

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

Answers (3)

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 Comments

Thank you for the answers! Very helpfull, but im not quite there yet. I do want to have my original Image displayed within the circle, the outside of the circle as already mentioned, should get an alpha value of maybe 0.8.. Maybe i need to overlay the transparent image (whole image, still in rectangular shape)with the original one, which will be displayed in circular shape ontop... if there are thoughts out there, id appreciate any help, thank you
Can you just multiply your image outside the circle by 0.8 (easy to do), or do you need to have another overlay layer with a hole in it and a transparency of 0.8?
Hello Image Analyst, Nope simple as that, just how do i cut my image according to my plotted line (the circle>>>
plot(Xc+R*cos(2*pi*x),Yc+R*sin(2*pi*x),'Color','g','LineWidth',0.2);)
I would then just multiply the images, or overlay them.
I thought my code did show you how to cut/mask the image. What don't you like about it?
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
This is not an answer. You should follow up on my answers with a "comment", not post an answer of your own.

Sign in to comment.

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');
Try this:
if true
[x,y] = meshgrid(1024);
c = sqrt((x-Xc).^2+(y-Yc).^2);
img1 = (c>R).*img;
end
RaK

Categories

Asked:

on 9 Dec 2012

Moved:

DGM
on 12 Feb 2023

Community Treasure Hunt

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

Start Hunting!