Generate binary image of geometric shapes?
20 次查看(过去 30 天)
显示 更早的评论
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.
0 个评论
采纳的回答
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;
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 个评论
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
另请参阅
类别
在 Help Center 和 File 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!