Search code examples
matlabgeometryimage-masking

Circular mask in Matlab


I create six circle for masking in Matlab. Each of mask's inner and outer radiuses are different. These masks is used to detect parasites on the slide. I have this code (one of the masks) but I want to do white area between to circle that in shared image. How can I do that? or Have another way to do mask that shared picture? MidpointCircle.m

resize_factor = 1;
inner_rad = 15*4/resize_factor;
outer_rad = 20*4/resize_factor;

ec_2 = floor(0.5*(outer_rad+inner_rad)*2*pi);

center = outer_rad+2; 
mask1_size = center*2;

circleimg = zeros(mask1_size,mask1_size);
circleimg = MidpointCircle(circleimg, outer_rad, center, center, 1);
circleimg = MidpointCircle(circleimg, inner_rad, center, center, 1);
mask1 = circleimg;

enter image description here


Solution

  • Ok, now I get it.

    Your function MidpointCircle only creates the borders of a circle, not the whole circle filled. The following code calculates the distance to the center and selects all values that are smaller than the outer and bigger than the inner radius:

    clear all;
    
    resize_factor = 1;
    inner_rad = 15*4/resize_factor;
    outer_rad = 20*4/resize_factor;
    
    ec_2 = floor(0.5*(outer_rad+inner_rad)*2*pi);
    
    center = outer_rad+2; 
    mask1_size = center*2;
    
    [x,y] = meshgrid(1:mask1_size,1:mask1_size);
    
    distance = (x-center).^2+(y-center).^2;
    mask = distance<outer_rad^2 & distance>inner_rad^2;
    
    figure(1);
    imshow(mask)
    

    Result:

    enter image description here