MATLAB Answers

0

Circle in a matrix

Asked by Melissa Buechlein on 27 Jun 2017
Latest activity Edited by Image Analyst
on 27 Jun 2017
Hello,
I want to make a matrix that has a weight of 1 inside a circle and 0 outside the circle. Along the edges in portions that are not fully inside the circle, I want it to have a partial weight based on how inside the circle it is. I have been able to successfully make a crude circle that only has 1 if it is fully in the circle and 0 otherwise, but I am not sure how to do the rest of the circle.
This is how I did it:
ref1 = (double((I-r).^2+(J-r).^2<=r^2));
Any hints would be extremely appreciated!
Thanks, Melissa

  0 Comments

Sign in to comment.

4 Answers

Answer by David Goodmanson on 27 Jun 2017
 Accepted Answer

Hi Melissa,
Here is a fairly simple method that makes a softer edge by adding up contributions from the point itself and its eight nearest neighbors. It's a weighted average of the 1's and 0's you get according to whether each of those nine points are inside or outside of the circle. You can experiment around with the weights. The array of grid points has to be large enough in extent so that the edge of the circle does not touch the boundary. That's because you have to have room to shift the grid indices by +- 1.
I used a different but similar method to yours to find whether the points were inside or outside.
x = -110:110;
y = -110:110;
[xx yy] = meshgrid(x,y);
u = zeros(size(xx));
u((xx.^2+yy.^2)<100^2)=1; % radius 100, center at the origin
% hard boundary
figure(1)
imagesc(u)
% weight the points: point itself; average of nearest neighbors;
% averaged of diagonal neighbors. These must add up to 1.
wp = .4; wn = .4; wd = .2;
ind = 2:length(x)-1;
u(ind,ind) = wp*u(ind,ind) ...
+ (wn/4)*(u(ind-1,ind ) + u(ind+1,ind ) + u(ind ,ind-1) + u(ind ,ind+1) ) ...
+ (wd/4)*(u(ind-1,ind-1) + u(ind-1,ind+1) + u(ind+1,ind-1) + u(ind+1,ind+1) );
% extended boundary
fig(2)
imagesc(u)

  0 Comments

Sign in to comment.


Image Analyst
Answer by Image Analyst
on 27 Jun 2017
Edited by Image Analyst
on 27 Jun 2017

Then call bwdist() to get an image where the value in the disk is it's distance from the edge towards the center of the circle.
% Get Euclidean Distance Transform
distanceImage = bwdist(~circlePixels);
% Normalize it so that it's 1 in the center.
distanceImage = distanceImage / max(distanceImage(:));
Below is a full demo. The value of the circle goes from 0 at the edge to 1.0 at the very center.
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 20;
% Create a logical image of a circle with specified
% diameter, center, and image size.
% First create the image.
imageSizeX = 640;
imageSizeY = 480;
[columnsInImage rowsInImage] = meshgrid(1:imageSizeX, 1:imageSizeY);
% Next create the circle in the image.
centerX = 320;
centerY = 240;
radius = 200;
circlePixels = (rowsInImage - centerY).^2 ...
+ (columnsInImage - centerX).^2 <= radius.^2;
% circlePixels is a 2D "logical" array.
% Now, display it.
subplot(2, 1, 1);
imshow(circlePixels) ;
colormap([0 0 0; 1 1 1]);
title('Binary image of a circle', 'FontSize', fontSize);
% Get Euclidean Distance Transform
distanceImage = bwdist(~circlePixels);
% Normalize it so that it's 1 in the center.
distanceImage = distanceImage / max(distanceImage(:));
% Display it.
subplot(2, 1, 2);
imshow(distanceImage, []) ;
title('Euclidean Distance Transform', 'FontSize', fontSize);
hp = impixelinfo(); % Let user mouse around and see values.

  1 Comment

Andrei Bobrov
on 27 Jun 2017
+1

Sign in to comment.


Andrei Bobrov
Answer by Andrei Bobrov
on 27 Jun 2017

M = zeros(1001);
M(500,500) = 1;
R = bwdist(M);
T = R >= 300;
imagesc(T)

  0 Comments

Sign in to comment.


KSSV
Answer by KSSV
on 27 Jun 2017
Edited by KSSV
on 27 Jun 2017

Method 1
%%Make circle
th = linspace(0,2*pi) ;
R = 1. ; % Radius of circle
C = [0 0] ; % Center of circle
xc = C(1)+R*cos(th) ; yc = C(2)+R*sin(th) ;
%%Make mesh
N = 100 ;
x = C(1)+linspace(-R,R,N) ;
y = C(2)+linspace(-R,R,N) ;
[X,Y] = meshgrid(x,y) ;
Z = (X.^2+Y.^2) ;
%%Get points lying inside circle
[in,on] = inpolygon(X(:),Y(:),xc,yc) ;
%%Plot
figure
hold on
plot(X,Y,'r') ;
plot(X',Y','r') ;
%
plot(X(in),Y(in),'.b')
%%Make my matrix
iwant = zeros(size(Z)) ;
iwant(in) = 1 ;
iwant(on) = 1 ;
figure
surf(iwant)
view(2)
shading interp
Method 2
%%Make circle
th = linspace(0,2*pi) ;
R = 1. ; % Radius of circle
C = [0 0] ; % Center of circle
xc = C(1)+R*cos(th) ; yc = C(2)+R*sin(th) ;
%%Make mesh
N = 100 ;
x = C(1)+linspace(-R,R,N) ;
y = C(2)+linspace(-R,R,N) ;
[X,Y] = meshgrid(x,y) ;
Z = (X.^2+Y.^2) ;
%%Make matrix
iwant = zeros(size(Z)) ;
iwant(Z<=R) = 1 ;

  2 Comments

Thanks KSSV for responding! This looks very close to what I want, but I was hoping to be able to include the edges as a fraction of the grid cell that is inside the circle. Is there a way to make the iwant(in) calculate that fraction?
Image Analyst
on 27 Jun 2017
Melissa, did you see my answer? The function bwdist() in the Image Processing Toolbox, will give you "how inside the circle it is".

Sign in to comment.