Principal direction in a binary image - matlab

As shown in image, there is a binary polygonal image. I want to find the principal direction in the image with respect to X-axis. I have shown the principal direction and X-axis with blue line. This can be done using PCA but my problem is such a small rectangle will have around 1000 pixels and I have to find Principal directions for around 100 polygons (polygon can be of arbitrary shape).
One approach that I have thought is:
Project that rectangle onto a line which is oriented at degrees at an interval (say) 5 degrees. The projection which has the maximum variance is the desired projection axis, and thus that is the desired angle. But this also falls under a greedy approach and thus will take time. Is there a smarter approach?
Also, if anybody could explain the exact procedure to do this using PCA, it would be helpful. I know the steps:
1. Take the covariance matrix.
2. Get the top eigenvector corresponding to largest eigenvalue -> that will be the principal direction.
But I am confused in the following statement which I often read everywhere:
A column vector: [0.5 0.5] is the first principal component and it gives the direction of the maximum variance. So can do I exactly calculate the angle by which I should rotate the data so that it will become parallel to X-axis.

Compute the eigenvector associated with the highest eigen value. Call that v. Normalize v. v = v/norm(v);
Compute angle between that and the horizontal direction: angle=acos(sum(v.*[1,0]))
Rotate by -angle, transformation matrix T = [cos(-angle) -sin(-angle); sin(-angle) cos(-angle)], multiply all points by that matrix. Do that for all polygons.

Related

Calculate angle of matrix with topography for each triangle

I have a matrix with data of a topography, let's say several hills. I want to have information of the angle of each data point to the vertical line. Here are two examples:
If I consider a place near the foot of the hill that is totally flat, I have a degree of 90ยฐ (90ยฐ to the vertical line).
If I am at the steepest point of the hill, I have a lower angle of let's say 50ยฐ.
To compute this I guess I have to to connect all the topography data so that (at least) three near pixels form triangle. After that I have to compute the angle(s) of this triangle.
Can I use a existing algorithm?
If your heightmap is a matrix A then you could approximate the the gradient components of every inner point (without the egdes) by
Xgrad = (A(2:end-1,3:end)-A(2:end-1,1:end-2))/2;
Ygrad = (A(3:end,2:end-1)-A(1:end-2,2:end-1))/2;
The angulars will then be
deg = (pi/2 - atan(sqrt(Xgrad.^2 + Ygrad.^2),1))/pi*180;
Depending on your heightmap, differentiating numerically can produce "fuzzy" results. Maybe you have to do some blur-filtering in order to produce smoother gradients.

how to determine if an image is symmetrical?-MATLAB

I have a 3D color image. I wish to plot its principal axis and its centroid. To do that, I have extracted each of the color channels, found the centroid and then proceeded to find the covariance matrix and eigenvalue. I am unsure as to whether this will help me plot the principal axis.
My primary goal is to plot the principal axis so that I can find whether the image is symmetrical about that axis.
This is the algorithm I would like to implement.
Plot the principal axis of the image. The image is then rotated
(clockwise) to align its coordinate (๐‘ฅ and ๐‘ฆ) axes with principal
inertia axis of the image. The image is then hypothetically folded
around the ๐‘ฅ-axis and the the sum of the absolute grey-level
difference between the corresponding pixels(๐ด ๐‘ฅ) between the
overlapping folds was taken as the amount of asymmetry on the ๐‘ฅ-axis.
The same procedure was performed for the ๐‘ฆ-axis. Two asymmetry
measures were calculated from ๐ด ๐‘ฅ and ๐ด ๐‘ฆ as follows:
A1 = min (๐ด
๐‘ฅ
, ๐ด
๐‘ฆ
)
๐ด
ร— 100%
A2 =
(๐ด
๐‘ฅ
+ ๐ด
๐‘ฆ
)
๐ด
ร— 100%.
The two asymmetry measures noted above (๐ด 1 and ๐ด 2) are computed
for each image channel, in the RGB colour space, resulting in the
total of six-coefficient (6D) colour asymmetry feature.
How do I plot the principal axis? I tried using regionprops function in MATLAB to plot the major axis, minor axis, centroid and orientation but I am unsure if this is the right approach. Should I use PCA?
Can I overlap the image using the fliplr function?
What do they mean same procedure for y-axis? If an image is asymmetrical over one axis then doesn't it mean its asymmetrical over the y-axis too?
I am unable to implement this algorithm. Please guide on how to code this in MATLAB.
https://in.mathworks.com/matlabcentral/answers/266988-how-to-flip-a-segmented-image-along-the-major-axis#answer_208875
This is a partial answer to my query but I am stuck at the initial part. How do I find the major axis and align in with the axes using imtranslate()?

How can i find a two points of triangle from a point and a center of gravity in a 3D

Using Matlab, how can I calculate a two points in Equilateral triangle if there are known one point and the Center of Gravity in a 3D ?
I know there is a infinite solutions but i need just a random one.
Thank you.
Take the vector pointing from the center of gravity to the point.
Create an orthogonal vector (this can be done in a few ways, I usually take the first vector, add 1.0 to each component until it is not parallel, then take the cross product with the original vector).
Rotate your vector 120 degrees about the orthogonal vector. (look up the rotation matrix about an arbitrary vector)
Create your second point by adding that vector to your center of gravity.
Create your third point by rotating it again or in the opposite direction.

Arrange the vertices of a 3D convex polygonal plane in counter clockwise direction in MATLAB

I have a convex polygon in 3D. For simplicity, let it be a square with vertices, (0,0,0),(1,1,0),(1,1,1),(0,0,1).. I need to arrange these vertices in counter clockwise order. I found a solution here. It is suggested to determine the angle at the center of the polygon and sort them. I am not clear how is that going to work. Does anyone have a solution? I need a solution which is robust and even works when the vertices get very close.
A sample MATLAB code would be much appreciated!
This is actually quite a tedious problem so instead of actually doing it I am just going to explain how I would do it. First find the equation of the plane (you only need to use 3 points for this) and then find your rotation matrix. Then find your vectors in your new rotated space. After that is all said and done find which quadrant your point is in and if n > 1 in a particular quadrant then you must find the angle of each point (theta = arctan(y/x)). Then simply sort each quadrant by their angle (arguably you can just do separation by pi instead of quadrants (sort the points into when the y-component (post-rotation) is greater than zero).
Sorry I don't have time to actually test this but give it a go and feel free to post your code and I can help debug it if you like.
Luckily you have a convex polygon, so you can use the angle trick: find a point in the interior (e.g., find the midpoint of two non-adjacent points), and draw vectors to all the vertices. Choose one vector as a base, calculate the angles to the other vectors and order them. You can calculate the angles using the dot product: A ยท B = A B cos ฮธ = |A||B| cos ฮธ.
Below are the steps I followed.
The 3D planar polygon can be rotated to 2D plane using the known formulas. Use the one under the section Rotation matrix from axis and angle.
Then as indicated by #Glenn, an internal points needs to be calculated to find the angles. I take that internal point as the mean of the vertex locations.
Using the x-axis as the reference axis, the angle, on a 0 to 2pi scale, for each vertex can be calculated using atan2 function as explained here.
The non-negative angle measured counterclockwise from vector a to vector b, in the range [0,2pi], if a = [x1,y1] and b = [x2,y2], is given by:
angle = mod(atan2(y2-y1,x2-x1),2*pi);
Finally, sort the angles, [~,XI] = sort(angle);.
It's a long time since I used this, so I might be wrong, but I believe the command convhull does what you need - it returns the convex hull of a set of points (which, since you say your points are a convex set, should be the set of points themselves), arranged in counter-clockwise order.
Note that MathWorks recently delivered a new class DelaunayTri which is intended to superseded the functionality of convhull and other older computational geometry stuff. I believe it's more accurate, especially when the points get very close together. However I haven't tried it.
Hope that helps!
So here's another answer if you want to use convhull. Easily project your polygon into an axes plane by setting one coordinate zero. For example, in (0,0,0),(1,1,0),(1,1,1),(0,0,1) set y=0 to get (0,0),(1,0),(1,1),(0,1). Now your problem is 2D.
You might have to do some work to pick the right coordinate if your polygon's plane is orthogonal to some axis, if it is, pick that axis. The criterion is to make sure that your projected points don't end up on a line.

MATLAB: Return array of values between two co-ordinates in a large matrix (diagonally)

If I explain why, this might make more sense
I have a logical matrix (103x3488) output of a photo of a measuring staff having been run through edge detect (1=edge, 0=noedge). Aim- to calculate the distance in pixels between the graduations on the staff. Problem, staff sags in the middle.
Idea: User inputs co-ordinates (using ginput or something) of each end of staff and the midpoint of the sag, then if the edges between these points can be extracted into arrays I can easily find the locations of the edges.
Any way of extracting an array from a matrix in this manner?
Also open to other ideas, only been using matlab for a month, so most functions are unknown to me.
edit:
Link to image
It shows a small area of the matrix, so in this example 1 and 2 are the points I want to sample between, and I'd want to return the points that occur along the red line.
Cheers
Try this
dat=imread('83zlP.png');
figure(1)
pcolor(double(dat))
shading flat
axis equal
% get the line ends
gi=floor(ginput(2))
x=gi(:,1);
y=gi(:,2);
xl=min(x):max(x); % line pixel x coords
yl=floor(interp1(x,y,xl)); % line pixel y coords
pdat=nan(length(xl),1);
for i=1:length(xl)
pdat(i)=dat(yl(i),xl(i));
end
figure(2)
plot(1:length(xl),pdat)
peaks=find(pdat>40); % threshhold for peak detection
bigpeak=peaks(diff(peaks)>10); % threshold for selecting only edge of peak
hold all
plot(xl(bigpeak),pdat(bigpeak),'x')
meanspacex=mean(diff(xl(bigpeak)));
meanspacey=mean(diff(yl(bigpeak)));
meanspace=sqrt(meanspacex^2+meanspacey^2);
The matrix pdat gives the pixels along the line you have selected. The meanspace is edge spacing in pixel units. The thresholds might need fiddling with, depending on the image.
After seeing the image, I'm not sure where the "sagging" you're referring to is taking place. The image is rotated, but you can fix that using imrotate. The degree to which it needs to be rotated should be easy enough; just input the coordinates A and B and use the inverse tangent to find the angle offset from 0 degrees.
Regarding the points, once it's aligned straight, all you need to do is specify a row in the image matrix (it would be a 1 x 3448 vector) and use find to get non-zero vector indexes. As the rotate function may have interpolated the pixels somewhat, you may get more than one index per "line", but they'll be identifiable as being consecutive numbers, and you can just average them to get an approximate value.