Generate binary image of geometric shapes?

20 views (last 30 days)
I need a code where I can generate different geometric shapes in a form of a binary image. Please assist me as I am not very good with Matlab Image Processing.

Accepted Answer

Image Analyst
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
Yashlin Naidoo
Yashlin Naidoo on 6 May 2015
The problem I have is to change the radius to side lengths like width,height etc.An example for a rectangle,it has a width and height,I cant seem to figure out how to change the radius to side lengths to get the other shapes.
Image Analyst
Image Analyst on 6 May 2015
Well, let's try to take this one simple step at a time. Let's say we have a square. 4 sides. Put a dot at the middle and draw lines out to the 4 vertices. Now, what is the angle from the center? It's 90 degrees, which is just 360/4. So now draw a line from the center to the middle of the side. What is the angle now? It's half that, or 360/(2*#sides). Let's call it theta. And from the middle of the side to the vertex is s/2 if s is full side length. So now we have sind(theta) = (s/2)/radius = s / (2*radius). Or radius = s / (2 * sind(theta)), where theta = 180/numSides. sind() is the version of sin() that works in degrees rather than radians. So plug in that equation to get the radius from the side length and you should have it.

Sign in to comment.

More Answers (1)

Alka Nair
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
  1 Comment
Yashlin Naidoo
Yashlin Naidoo on 6 May 2015
I need to generate 8 distinct shapes from specified variables,hopefully I'll get to understand your method to generate these shapes:
1. Circle
2. Square
3. Triangle
4. Rectangle
5. Pentagon
6. Hexagon
7. Ellipse
8. Octagon

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!