MATLAB Answers

0

How to cover an irregular closed area with square pixels

Asked by Maura Monville on 4 Jun 2019
Latest activity Commented on by Maura Monville on 5 Jun 2019
I have a set of closed curves described by the (x,y) coordinates of the approximating polygon.
My task is to cover the area enclosed by each curve with square pixel whose side is input.
No piece of the area must be left uncovered. That implies the squares covering the curve rim will lie partially inside and partially outside the curve.
In the following I describe what I have done.
  1. Generate a square grid of points inside or on the edge of the curve
  2. Generate new points lying outside the closed curve by subtracting the pixel side in the x or in the y direction
  3. Use Matlab function "rectangle" to build the square pixel.
  4. Eventually I will store to a file the coordinates of each pixel in the form X_min X_max Y_min Y_max
My problem is that some points, generated outside the closed curve, give rise to pixels that are completely outside the closed area. They do not intersect it.
I am attaching my script "Pixelize_Collimator_Aperture.m", my function "CollimatorGrid.m", and a zipped file containing the pictures of the resulting pixelized closed areas
I am pretty sure there are smarter ways to address this task. I would appreciate any suggestion and/or comment.
Thank you in advance.
Maura E.

  0 Comments

Sign in to comment.

1 Answer

Answer by KSSV
on 4 Jun 2019

Check the code:
R = 1. ;
C = [0. 0.] ;
th = linspace(0,2*pi) ;
xc = C(1)+R*cos(th) ;
yc = C(1)+R*sin(th) ;
% Make grid
m = 50 ; n = 50 ;
xi = linspace(min(xc),max(xc),m) ;
yi = linspace(min(yc),max(yc),n) ;
[X,Y] = meshgrid(xi,yi) ;
idx = inpolygon(X(:),Y(:),xc,yc) ;
X(~idx) = NaN ;
Y(~idx) = NaN ;
figure
hold on
plot(xc,yc,'b')
plot(X,Y,'r')
plot(X',Y','r')

  3 Comments

Nice self-contained code. Nevertheless, there are tract on the closd curve that are not covered by any pixel
That also can be taken care. You can use fill or patch eh.
The reason why I am covering the closed areas with square pixels is because these areas are the axial sections of custom made collimators used for proton therapy of eye tumors.
The custom made colimator has to be added as a component of the synchrotron Monte Carlo model implemented with Fluka (non my choice).
Unluckily, Fluka cannot import CAD files which most modern Monte Carlo packages can.
Fluka approximates the custom made collimator reading a text file that contains the coordinates of the pixel in the form (Xmin Xmax Ymin Ymax). Then Fluka adds the same depth coordinate for each pixel, therefore transforming pixels into voxels.
Pixels must be square for Fluka to read them in.
The size of the square pixels does not need to be the same all over the closed area.
Actually, it would be desirable to make the pixels across the rim of the area smaller than the pixels completely contained inside the area.

Sign in to comment.