How do I generate eight distinct shapes from a set of specified variables using MATLAB image processing.
You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Show older comments
How do I generate eight distinct shapes from a set of specified variables using MATLAB image processing.
Myshape generator algorithm should be able to generate the following shapes: 1. Circle (Input radius in pixels) 2. Square (Input length of 1 side in pixels. Adjacent sides must be parallel to both borders of the image respectively) 3. Triangle (Input length of base in pixels and both base angles in degrees. The base is associated with column span, which is parallel to the top border, and the apex of triangle must be located at a point above the base line) 4. Rectangle (Input length of 2 adjacent sides in pixels. For display purposes the smaller value is associated with the row span and the larger values is associated with the column span. Adjacent sides must be parallel to both borders of the image respectively) 5. Pentagon (Input length of 1 side in pixels. The base is associated with column span, which is parallel to the top border, and the apex of pentagon must be located at a point above the base line) 6. Hexagon (Input length of 1 side in pixels. At least 2 sides must be parallel to the top and bottom borders of the image) 7. Ellipse (Input length of semi-minor (rows) and semi-major (columns) axes in pixels. For display purposes the semi-minor axis is associated with the row span, and must be parallel to the left and right borders of the image, and the semi-major axis is associated with column span, and must be parallel to the top and bottom borders of the image) 8. Octagon (Input length of 1 side in pixels. At least two sides must be parallel to the top and bottom borders of the image) In addition to the input specifications described above, a colour must be attributed to every shape i.e. every shape must have an R, G and B value associated with it. The final product/algorithm must output 2 separate images: 1. A binary (black and white) image with all the shapes in black and background in white. (50%) a. Each shape must be located centrally within 8 square boxes of equal size (as shown below).
Please assist in this question.
1 Comment
Is there a specific matlab question? otherwise read http://www.mathworks.com/matlabcentral/answers/8626-how-do-i-get-help-on-homework-questions-on-matlab-answers
Accepted Answer
Image Analyst
on 28 Apr 2015
This is the same question you asked before and I answered here. I thought it was a good answer, Yashlin - why didn't you?
18 Comments
Good day image analsyt,my apologies. I didn't understand what you meant.Could you please explain to me again,I will really appreciate it.
It meant go to the link on the FAQ, look at the 4th code sample in the FAQ and make a change to theta so that it has exactly the number of sides you want. Add an angle to the whole theta array change the rotation of the shape. I'll leave that simple change to you. So here is the answer for a hexagon. You just need to change one character to do the others:
xCenter = 12;
yCenter = 10;
% Modification to the FAQ is the next two lines.
numSides = 6; % <=== CHANGE THIS NUMBER
theta = linspace(0, 2*pi, numSides);
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;
Hi image analyst,I tried using your code for the hexagon.A hexagon is a 6 sided shape.This code produces a 5 sided shape eventhough the numsides=6,could you please explain why?Thanx
To "close" the shape you'll need the last point to be the same as the first point, so you need one extra point otherwise you get an "open" shape. Just add 1 then. So if you have 6 sides, you'll have 7 vertices since vertex 1 and 1 need to be the same point.
Thank you for that response,I wanted to know how do I make the shape be black and the background black,will it just be colormap([0 0 0;1 1 1]) ;
plot(x, y, 'k-');
They both can't be black or else you won't see anything. You can use patch() or fill() if you want a solid shape instead of an outline.
sorry I meant the shape to be black and the background to be white
Use patch() or fill() to create solid graphical elements.
Hi image anaylst,im still having problems generating this code for the shapes,this is the full question briefly.
Aim: To be able to generate eight distinct shapes from a set of specified variables using MATLAB image processing.
Your shape generator algorithm should be able to generate the following shapes:
1. Circle
(Input radius in pixels)
2. Square
(Input length of 1 side in pixels. Adjacent sides must be parallel to both borders of the image respectively)
3. Triangle
(Input length of base in pixels and both base angles in degrees. The base is associated with column span, which is parallel to the top border, and the apex of triangle must be located at a point above the base line)
4. Rectangle
(Input length of 2 adjacent sides in pixels. For display purposes the smaller value is associated with the row span and the larger values is associated with the column span. Adjacent sides must be parallel to both borders of the image respectively)
5. Pentagon
(Input length of 1 side in pixels. The base is associated with column span, which is parallel to the top border, and the apex of pentagon must be located at a point above the base line)
6. Hexagon
(Input length of 1 side in pixels. At least 2 sides must be parallel to the top and bottom borders of the image)
7. Ellipse
(Input length of semi-minor (rows) and semi-major (columns) axes in pixels. For display purposes the semi-minor axis is associated with the row span, and must be parallel to the left and right borders of the image, and the semi-major axis is associated with column span, and must be parallel to the top and bottom borders of the image)
8. Octagon
(Input length of 1 side in pixels. At least two sides must be parallel to the top and bottom borders of the image)
In addition to the input specifications described above, a colour must be attributed to every shape i.e. every shape must have an R, G and B value associated with it.
The final product/algorithm must output 2 separate images:
1. A binary (black and white) image with all the shapes in black and background in white.
a. Each shape must be located centrally within 8 square boxes of equal size (as shown below).
Circle Square Triangle Rectangle Pentagon Hexagon Ellipse Octagon
Please assist me,as I researched and tried my best to generate a code,im still having problems
I gave you the code. Here it is again:
xCenter = 12;
yCenter = 10;
% Modification to the FAQ is the next two lines.
numSides = 6; % <=== CHANGE THIS NUMBER
theta = linspace(0, 2*pi, numSides);
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;
To get the side lengths from the radii, and vice versa, are just simple trig formulas well within your capabilities.
When I generate a square,It is showing not directly by the centre,I used "imrotate" to rotate the image and It didnt work,what should I do?
You can rotate by adding or subtracting an offset to theta. See this code:
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;
Did you get an appropriate answer to this question, as I have the same question, which is how to generate different geometric shapes using only the radius. Can you help me if you have the answer?
@Haytham Ali there is a new built-in function nsidedpoly() that you can check out.
how can I make it can you explain more please??
@Haytham Ali, did you notice the examples in the help documentation? Did you try them? If it's not what you want, then explain in a lot more detain what you want.
More Answers (0)
Categories
Find more on Programming in Help Center and File Exchange
Products
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)