how can I plot a symmetry axis over an image in Matlab?
8 views (last 30 days)
Show older comments
how can I plot a symmetry axis over an image in Matlab using least square method?
2 Comments
John D'Errico
on 1 Apr 2023
Huh?
You have an image. I get that.
Now, apparently there is some line that passes through the image, on either side of which, the image is apparently symmetric? Symmetric in what way? I can think of several ways an image may be symmetric around a line. And the least squares problem would be different depending on how symmetry is interpreted.
May the line be at any angle? Any orientation? Or just vertical or horizontal?
And I'm not even positive this is the question you are asking.
Walter Roberson
on 1 Apr 2023
Edited: Walter Roberson
on 1 Apr 2023
Answers (1)
Amith
on 27 Apr 2023
Hi,
As per my understanding you wanted to plot a symmetry axis over an image in Matlab using least square method.
To plot a symmetry axis over an image using the least square method, you can follow these general steps:
1. Load your image using the `imread` function and convert it to grayscale if it's a color image.
2. Choose two points on opposite sides of the axis you want to find. These points should be approximately equidistant from the axis.
3. Compute the equation of the line passing through these two points using the least square method. The equation should be in the form `y = ax + b`.
4. Plot the image using the `imshow` function and then plot the symmetry axis line using the `plot` function.
Here's some example code that demonstrates this process:
% Load the image
img = imread('myimage.jpg');
grayImg = rgb2gray(img);
% Choose two points on opposite sides of the axis
point1 = [100, 200]; % [x, y] coordinates of point 1
point2 = [300, 100]; % [x, y] coordinates of point 2
% Compute the equation of the line passing through the two points using least square method
x = [point1(1), point2(1)];
y = [point1(2), point2(2)];
A = [x', ones(size(x'))];
coeffs = A \ y';
a = coeffs(1);
b = coeffs(2);
% Plot the image and the symmetry axis line
figure;
imshow(grayImg);
hold on;
xCoords = linspace(1, size(grayImg, 2), 100);
yCoords = a*xCoords + b;
plot(xCoords, yCoords, 'LineWidth', 2, 'Color', 'r');
In this example, we first load an image and convert it to grayscale. We then choose two points on opposite sides of the axis and compute the equation of the line passing through them using the least square method. Finally, we plot the image using `imshow` and overlay the symmetry axis line on top of it using `plot`.
Note - that this method assumes that the image is symmetric with respect to the axis you want to find, and that the two chosen points are approximately equidistant from the axis. If these assumptions do not hold, the method may not produce accurate results.
0 Comments
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!