# Generate binary image of geometric shapes?

15 views (last 30 days)

Show older comments

Yashlin Naidoo
on 6 May 2015

Commented: Image Analyst
on 6 May 2015

##### 0 Comments

### Accepted Answer

Image Analyst
on 6 May 2015

Edited: Image Analyst
on 6 May 2015

I thought I helped you in your prior posting of this question. Here is the code from there:

xCenter = 12;

yCenter = 10;

% Modification to the FAQ is the next two lines.

numSides = 6; % <=== CHANGE THIS NUMBER

theta = linspace(0, 2*pi, numSides + 1);

% Rotate the shape by subtracting an offset.

theta = theta - pi/3;

radius = 5;

x = radius * cos(theta) + xCenter;

y = radius * sin(theta) + yCenter;

plot(x, y);

axis square;

xlim([0 20]);

ylim([0 20]);

grid on;

Feel free to adapt as necessary to change center, radius, rotation angle, etc.

You can make a binary image of it with poly2mask()

binaryImage = poly2mask(x, y, rows, columns);

You have to specify the number of rows and columns you want in the image.

##### 2 Comments

Image Analyst
on 6 May 2015

### More Answers (1)

Alka Nair
on 6 May 2015

Hi, For generating geometric shapes in binary images use STREL to create the structuring element, for example, suppose you want to generate a square. The steps are as follows: >>img = zeros(100,100); >>imgLogical = logical(img); >>img(50,50)=1; % fixing the initial seed point >>se1 = strel('square',12); >>im2 = imdilate(f,se1);

Please refer to the documentation for STREL at the following location: http://www.mathworks.com/help/images/ref/strel.html

### See Also

### Categories

### Community Treasure Hunt

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

Start Hunting!