# Getting meshgrid along different angles for data extraction from a matrix/image?

4 views (last 30 days)
waqas on 21 Jul 2020
Commented: waqas on 26 Jul 2020
Hi,
I have three point locations of a matrix which I want to use to get inclined meshgrid about the lines joining these points. I know how to individually perform these but I am unable to figure out how to rotate both of them together using pol2cart function. A similar question where pacman shape is obtained using the same technique. However, in the current implementation, I do not want a pacman shape and the rounded region that we see in the second plot should be at the end of the second line.
How to resolve this?
x0 = 500; y0 = 1;
x1 = 450; y1 = 300;
x2 = 600; y2 = 700;
m = 1000; n=1500;
subplot(2,1,1)
plot([y0,y1,y2],[x0,x1,x2]); axis ij equal; xlim([1,n]);ylim([1,m]);
grid on;
title('This is how the lines should look in the image or matrix')
[yi,xi] = meshgrid(y1-(1:n),x1-(1:m));
ang = atan((y1-y0)/(x1-x0));
ang2 = atan((y2-y1)/(x2-x1));
[the,ro] = cart2pol(yi,xi);
tt = the(:,1:x1);rt = ro(:,1:x1);
[xt1,yt1] = pol2cart(tt+ang,rt);
tt2 = the(:,(x1+1):end);rt2 = ro(:,(x1+1):end);
[xt2,yt2] = pol2cart(tt2+ang2,rt2);
x = [xt1,xt2];
y = [yt1,yt2];
intR = 10; extR = 100;
subplot(2,1,2)
crack2 = x<=intR & x>=-intR & y<=sqrt(intR^2-x.^2);
notcrack= x<=(intR+extR) & x>=-(intR+extR) & y<=sqrt((intR+extR)^2-(x).^2);
region= notcrack & ~crack2;
imshow(region);h = gca; h.Visible = 'on';
title('this is the maximum external region')
##### 2 CommentsShowHide 1 older comment
waqas on 23 Jul 2020
Edited: waqas on 23 Jul 2020
What I actually want to find out is the value of a function similar to:
The values are defined by where you take the crack tip and overall angle of the crack in the image. Value of n varies from -1 to 2. For a straight crack, I can inclined the mesh using pol2cart as was answered in last question I asked (link to open the question). But if the crack is changing directions e.g., in the first figure of current question where the crack tip is now at (600,700), then implementation of pol2cart should change. Thats why I used "inclined meshgrid".
I do not have a formula for mask but the idea is that the mask would consist of some region outside the crack too. Howoever, crack itself will be removed from the mask as the formula I mentioned above is not suppose to be applied on the crack itself.
"Pacman shape reference" was based on last question [link]. I changed the shape as follows:
As you can see that the round region is after the crack tip only.
"Maximum external region" in would be R_ext now and I want to make a mask that would take care of the change of direction of the crack, which is shown in the plot attached with the question.
Hopefully I have explained all the points clearly. Now, I want to select region around two line (in the plot) to be equal to R_ext and then at the end of the crack tip i.e., (600,700), I want to make a rounded shape just like the figure posted above. Eventually I am going to use this mask to get the pixel values of grayscale images that I have and then analyze them.
In my previous question, you mentioned that we can use image processing techniques to get the mask too. Can you share the details?

Sign in to comment.

### Accepted Answer

Matt J on 22 Jul 2020
If you're just trying to generate shaded polygons, it would be simpler just to use impoly, e.g.,
x0 = 1; y0=1;
x1 = 200; y1=500;
x2 = 500; y2=100;
m = 800; n=600;
xy=[x0,y0;x1 y1;x2 y2];
xy=[xy;flipud(xy+[0,100])];
H=impoly(ancestor(imshow(false(m,n)),'axes'),xy);
imshow(H.createMask)
##### 14 CommentsShowHide 13 older comments
waqas on 26 Jul 2020
Thank you the discussion was really helpful. I am posting the complete code with all the corrections in case some needs help. I will accept your answer to close the thread too.
This is how the final mask looks like:
There is still a little inconsistancy right where the lines meet but I can live with that.
x0 = 500; y0 = 1;
x1 = 400; y1 = 500;
x2 = 600; y2 = 1000;
m = 1088; n=2060;
subplot(2,1,1)
plot([y0,y1,y2],[x0,x1,x2]); axis ij equal; xlim([1,n]);ylim([1,m]);
grid on;
title('This is how the crack should look in the image or matrix')
[yi,xi] = meshgrid(y2-(1:n),x2-(1:m));
% ang = atan((y1-y0)/(x1-x0));
% ang2 = atan((y2-y1)/(x2-x1));
ang = atan((x1-x0)/(y1-y0));
ang2 = atan((x2-x1)/(y2-y1));
[the,ro] = cart2pol(yi,xi);
tt = the(:,1:y1);rt = ro(:,1:y1);
[yt1,xt11] = pol2cart(tt-ang,rt);
translation = (x0-x2) + ((y2-y0)*tan(ang));
xt1 = xt11 + translation;
tt2 = the(:,(y1+1):end);rt2 = ro(:,(y1+1):end);
[yt2,xt2] = pol2cart(tt2-ang2,rt2);
x = [xt1,xt2];
y = [yt1,yt2];
intR = 10; extR = 200;
subplot(2,1,2)
crack2 = x<=intR & x>=-intR & y>=-sqrt(intR^2-x.^2);
notcrack= x<=(intR+extR) & x>=-(intR+extR) ;
notcrack2 = y>=-sqrt((intR+extR)^2-(x).^2);
region= notcrack & ~crack2 & notcrack2;
imshow(region);h = gca; h.Visible = 'on';
title('Mask')

Sign in to comment.

### Community Treasure Hunt

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

Start Hunting!

Translated by