# Generate binary image of geometric shapes?

조회 수: 7 (최근 30일)
Yashlin Naidoo 2015년 5월 6일
댓글: Image Analyst 2015년 5월 6일
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.

댓글을 달려면 로그인하십시오.

### 채택된 답변

Image Analyst 2015년 5월 6일
편집: Image Analyst 2015년 5월 6일
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;
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없음 표시없음 숨기기
Yashlin Naidoo 2015년 5월 6일
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 2015년 5월 6일
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.

댓글을 달려면 로그인하십시오.

### 추가 답변 (1개)

Alka Nair 2015년 5월 6일
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이전 댓글 -1개 표시이전 댓글 -1개 숨기기
Yashlin Naidoo 2015년 5월 6일
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

댓글을 달려면 로그인하십시오.

### 카테고리

Help CenterFile Exchange에서 3-D Volumetric Image Processing에 대해 자세히 알아보기

### Community Treasure Hunt

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

Start Hunting!

Translated by