How to make [cx cy] the center of a user defined mesh grid of size [ix iy]

[EDIT: 20110629 12:13 CDT - reformat - WDR]
What I am trying is to find area of a polygon i.e. ractangle using conic sectors. For this I create a conic mask (extracted from a circle) and then rotate it over the rectangle. Let say center of the rectangle is (x, y) = [76.6, 44.1], then I would create a meshgrid with the center [cx cy] = [76.6, 44.1] and then find the conic mask with this base/origan. I have managed to make a conic mask at say [76.6, 44.1] but unfortunatly it is not the center of the mesh. So when I rotate the meshgrid using imrotate, the conic mask loses its center/base. Consequently after first iteration of theta, I am not able to caclulate the area of rectangle for the rest of conic sections. SOLUTION is that if I am able to draw cx and cy as the center of the meshgrid, then by simply imrotate my mask will rotate and I will get the area of the rectangle by rotating the mask all around 360'.
My code is as follows for creating the mask:
% --------------------------------------------------------
r = 40; % the radius of the circle from which the conic mask is to be extracted, it is large enough to accomodate the rectangle boundariesl, depends upon the size of the rectangle
cx = 76.6;
cy = 44.1;
Theta = 30; % Angle of conic section/sector
Theta1 = Theta / 2; % Get the angle divided between two linesegment slopes
ix = 3*r; iy = 3*r; % To get big enough meshgrid/image, three times the radius !! @Sean de: HERE THE PROBLEM IS!!
[X, Y] = meshgrid(-(cx - 1):(ix - cx), -(cy - 1): (iy - cy));
c_mask = ((X.^2 + Y.^2) <= r^2); % Get a circulat mask
Slope = tand(Theta1); % Create a line segment with the slope Theta1
l1 = Y - Slope * X;
L1 = (l1 < 0.0);
Slope = tand(-Theta1); % Create another line segment with slope -Theta1
l2 = Y - Slope * X;
L2 = (l2 > 0.0);
Sector = immultiply(L1,L2); % Get the sector common between two line segments
c_mask = immultiply(Sector, c_mask); % Get the conic section of the circular mask at the defined x-y
% DISPLAY MASK
figure ,
subplot(311), imshow(L1), title('Line 1')
axis on, hold on
subplot(312), imshow(L2), title('Line 2')
axis on, hold on
subplot(313), imshow(Sector), title('Conic sector')
axis on, hold on
h = imcontour( Sector, 1, 'r');

9 commentaires

It's still not clear to me what your goal is. Much less what's wrong with the code I have:
X(1,61)
ans =
76.6
Y(61,1)
ans =
44.1
running the line you have commented out. X and Y are both 121x121 so 61 is the middle element. Please clarify the problem.
@Sean de: Thanks for your keen intrest. Probally it may take a few minutes for an expert to figure it out but this is my problem statment.
My goal is to:
1. Draw a meshgrid with a circle in it of radius r (user given, it accomodates the targeted shape as explained underneath), such that the center of the meshgrid and circle is the same (user defined cx &cy)
2. Take out a conic sector with an angle Theta as a mask, out of this circle
3. Rotate this mask arround a certain shape(whose center is the same as center of the mesh and circle, that was the reason cx & cy are user defined and are the center of the targeted shape, in this case its center of a rectangle). i.e. if Theta = 45' then we have 7 more rotations to cover the whole cycle of 360'.
4. Thus by revolving the sector arround the shape we find its area thats the intersection of the conic mask and the shape(rectangle).
SO THE OVERALL GOAL IS TO FIND AREA OF A CERTAIN SHAPE USING CONIC SECTIONS.
In your implementation,
1. cx & cy are the center of the meshgrid but if ix and iy change i.e. 10*r instead 3*r then its no more the center
2. Tough cx and cy are the center of the meshgrid for the above; they are not the center of circle to get the correct conic section
I would reallpy appreciate if you help me out of this
I'm at home on my first cup of coffee not; do you think you could draw out a picture in paint or powerpoint that shows the steps, and points toward the end result? I'll look at this more when I get to work.
Are you required to use conic sections? Otherwise if the shape is convex you could just use the area of the convex hull.
Thanks Sean. So kind of you.
How am I going to post the picture?
Convex Hull... that's intresting. I will look more into it.
http://www.dropdo.com/
or others:
http://www.mathworks.com/matlabcentral/answers/7924-where-can-i-upload-images-and-files-for-use-on-matlab-answers
This is how I want to find the area
http://www.dropdo.com/5bt/Cone%20area
This is an example where I have changed the size of rectangle (ultimately radius r then ix & iy) and still I get the correct area for that portion of the cone. Now I cant rotate it, becouse the center of the meshgrid and the circle is not the same.
http://www.dropdo.com/5bu/Rectangle%20Intersection
I don't see how a conic sector would calculate the area of the blob in the first image... You can privately email me the data you have if you want. This may be a thread better posed for the newsreader (http://www.mathworks.com/matlabcentral/newsreader/). There are more computational geometry pros there.
Thanks Sean de. Thanks for your help and coopration, hope to be incontact with you in future.

Connectez-vous pour commenter.

Réponses (1)

Paulo Silva
Paulo Silva le 29 Juin 2011

12 commentaires

I dont understand your answar. I want to rotate it arround specified x y points and not arround the center
To rotate it about a specific axis, zero-pad the array so xa,ya is in the center.
@ Sean de: Thanks. How can I acheive a meshgrid with the center cx and cy? I am using
[X, Y] = meshgrid(-(cx - 1):(ix - cx), -(cy - 1): (iy - cy));
ix and iy is the size of the mesh grid.
Why do you need the meshgrid?
iy = 10;
ix = 10;
cx = 4;
cy = 7;
[X, Y] = meshgrid((cx-fix(ix/2)):(cx + fix(ix/2)),(cy-fix(iy/2)):(cy + fix(iy/2)))
Reason for meshgrid is, I then have to draw a circle of radius r and then extract a conic section (circulat sector) of angle theta as mask. Then I want to rotate this mask all arround the circle to cover 360'.
Sumerizing: I am trying to create a conic mask of angle theta and rotate it arround a circle. (Sorry new to matlab)
@ Sean de: Sorry but its not working. It dosent gove me the center of the meshgrid at cx and cy
It did for me with that example. Can you provide a counter example? With negative numbers you may need an absolute value somewhere.
Wouldn't rotating the mask 360 just create an annulus? Could you perhaps find a google image of the resultant shape?
What I am trying is to find area of a polygon i.e. ractangle using conic sectors. For this I create a conic mask (extracted from a circle) and then rotate it over the rectangle. Let say center of the rectangle is (x, y) = [76.6, 44.1], then I would create a meshgrid with the center [cx cy] = [76.6, 44.1] and then find the conic mask with this base/origan. I have managed to make a conic mask at say [76.6, 44.1] but unfortunatly it is not the center of the mesh. So when I rotate the meshgrid using imrotate, the conic mask loses its center/base. Consequently after first iteration of theta, I am not able to caclulate the area of rectangle for the rest of conic sections. SOLUTION is that if I am able to draw cx and cy as the center of the meshgrid, then by simply imrotate my mask will rotate and I will get the area of the rectangle by rotating the mask all around 360'.
My code is as follows for creating the mask:
% --------------------------------------------------------
r = 40; % the radius of the circle from which the conic mask is to be extracted, it is large enough to accomodate the rectangle boundariesl, depends upon the size of the rectangle
cx = 76.6;
cy = 44.1;
Theta = 30; % Angle of conic section/sector
Theta1 = Theta / 2; % Get the angle divided between two linesegment slopes
ix = 3*r; iy = 3*r; % To get big enough meshgrid/image, three times the radius !! @Sean de: HERE THE PROBLEM IS!!
[X, Y] = meshgrid(-(cx - 1):(ix - cx), -(cy - 1): (iy - cy));
%--------------------------
% [X, Y] = meshgrid((cx-fix(ix/2)):(cx + fix(ix/2)),(cy-fix(iy/2)):(cy + fix(iy/2))) %Your proposed solution
% ----------------------------------------
c_mask = ((X.^2 + Y.^2) <= r^2); % Get a circulat mask
Slope = tand(Theta1); % Create a line segment with the slope Theta1
l1 = Y - Slope * X;
L1 = (l1 < 0.0);
Slope = tand(-Theta1); % Create another line segment with slope -Theta1
l2 = Y - Slope * X;
L2 = (l2 > 0.0);
Sector = immultiply(L1,L2); % Get the sector common between two line segments
c_mask = immultiply(Sector, c_mask); % Get the conic section of the circular mask at the defined x-y
% DISPLAY MASK
figure ,
subplot(311), imshow(L1), title('Line 1')
axis on, hold on
subplot(312), imshow(L2), title('Line 2')
axis on, hold on
subplot(313), imshow(Sector), title('Conic sector')
axis on, hold on
h = imcontour( Sector, 1, 'r');
it seems to be a programming challange for me and I would appreciate any help
Faraz please edit your first question and add all the details, that way Sean and others can help you better.
Thanks Paulo. My first question is all about rotaing an image about given axix, if that solved then I dont need rest of the stuff.

Connectez-vous pour commenter.

Question posée :

le 29 Juin 2011

Community Treasure Hunt

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

Start Hunting!

Translated by