- Obtain the limits to be set to the axis from the height and width of the image.
- Use 'image' function to show the image on the axis while setting appropraite axis limits as 'XData' and 'YData' properties.
- Use 'reverse' propety of the axis to obtain the image correctly aligned.
- Use 'ginput' function to register user clicks on the image and obtain corresponding points.
- Then using the 'atan2' function obtain the angle between the selected points from the Center(0,0).
How to create axes in the center of an image and calculate angles between the points in an image?
13 views (last 30 days)
Show older comments
Below you can see an image where i would like to draw an axis at the center(0,0) and calculate angle between the points from the center of an image. Can anyone plese help me solve this issue or any idea how can i approach?
0 Comments
Answers (2)
Rahul
on 6 Dec 2024 at 9:30
In order to achieve the desired result of adjusting the axis of the image to obtain Center(0,0) as the center of the image and obtain the angle of user selected points from the Center(0,0), consider using the following steps:
Here is the example:
% Assuming 'img' to be the variable containing image data
[height, width, ~] = size(img);
image('XData', [0, width] - (width / 2), 'YData', [0, height] - (height / 2),'CData',img);
set(gca, 'YDir', 'reverse');
[x, y] = ginput(2); % Here I have set the limit to obtain 2 user defined input points
% Calculating and normalizing the angle difference
angle_difference = mod(atan2(y(2), x(2)) - atan2(y(1), x(1)),2*pi);
The following MATLAB Answers can also be referred:
How to adjust axis limits if 'imagesc' is to be used: https://www.mathworks.com/matlabcentral/answers/290561-put-axes-in-center-of-image?s_tid=answers_rc1-2_p2_MLT
Functions to use to obtain value of selected point:
How to find angle between lines:
The following MathWorks documentations can be referred to know more:
Thanks.
0 Comments
DGM
on 6 Dec 2024 at 13:55
Edited: DGM
on 6 Dec 2024 at 13:58
There's no need to use manual transcription of a plot. Use the points that have already been found.
Here is a hypothetical recreation of the original setup, but with the angle calculation and display.
% a grayscale image
inpict = imread('circ.png');
imshow(inpict)
% OP started with edge detection
alledges = edge(inpict);
% we only need to pay attention to all but the largest two edges
ROI = alledges & ~bwareafilt(alledges,2);
% isolate the center object from the others
centerpart = bwareafilt(ROI,1);
outerdots = ROI & ~centerpart;
% get the centroids
S = regionprops(centerpart,'centroid');
C0 = vertcat(S.Centroid);
S = regionprops(outerdots,'centroid');
C = vertcat(S.Centroid);
% calculate angles
% bear in mind that image y-coordinates are flipped
theta = -atan2d(C(:,2)-C0(2),C(:,1)-C0(1));
% visualize everything
figure
imshow(alledges); hold on
plot(C0(1),C0(2),'yx','markersize',20,'linewidth',3)
plot(C(:,1),C(:,2),'g*','markersize',20,'linewidth',3)
% throw down a bunch of labels
% i have to make the text huge, otherwise it's invisible on the forum
ht = gobjects(numel(theta),1);
for k = 1:numel(theta)
str = sprintf('%0.1f°',theta(k));
ht(k) = text(C(k,1),C(k,2)-35,str,'color','g','fontsize',18, ...
'horizontalalignment','center');
end
% zoom in, otherwise nothing shows up on the forum
xlim([990 1890])
ylim([96 1000])
0 Comments
See Also
Categories
Find more on Read, Write, and Modify Image in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!