Azzera filtri
Azzera filtri

Best Fit Lines on Image Processing Edges

12 visualizzazioni (ultimi 30 giorni)
Edward Sweeney
Edward Sweeney il 31 Mar 2021
Modificato: Sai Pavan il 15 Feb 2024
I am writing MatLab code to determine the angles of a resultant picture after subtracting image 1 from image. The imgDiff should form a triangle, where in need to determine the two angles formed on either side of the base. Below is what I have already written. Any help would greatly be appreciated. It should be noted that the location of the triangle in imgDiff, will be at different locations on subsequent times I run the code.
%%
clear all;
clc;
%% Testing
img1 = imread('AORBaselineImage.jpg');
img2 = imread('AORBaselineImage-noAOR.jpg');
%% Convert images to black and white
img1BW = rgb2gray (img1);
img2BW = rgb2gray (img2);
%% Display black and white images
figure
imshow(img1BW)
figure
imshow(img2BW)
%% Subtracting Images
imgDiff = abs(img1BW - img2BW);
figure
imshow(imgDiff)
%%
BW = imbinarize(imgDiff);
[B,L] = bwboundaries(BW,'noholes');
imshow(label2rgb(L, @jet, [.5 .5 .5]))
hold on
for k = 1:length(B)
boundary = B{k};
plot(boundary(:,2), boundary(:,1), 'w', 'LineWidth', 2)
end

Risposte (1)

Sai Pavan
Sai Pavan il 15 Feb 2024
Modificato: Sai Pavan il 15 Feb 2024
Hello Edward,
As I understand, you want to determine the two angles formed on either side of the base of the triangle formed by taking the difference between two images.
I observe that you've already performed image subtraction and binarization to get the boundaries. Next, you can use the `bwboundaries` function to get the boundary points of the triangle. Then, you can use the `polyfit` function to fit lines to these points and use `polyval` to evaluate the line equations. Finally, you can use basic trigonometry to calculate the angles. Please note that the workflow mentioned works best under the assumption that the base of the triangle is horizontal and is equilateral or isosceles.
Please refer to the below code snippet that is a continuation of your code that includes fitting lines to the triangle's edges and calculating the angles:
% Existing Code
boundary = max(B, [], 'length'); % Assuming the largest boundary corresponds to the triangle, this will select the boundary with the maximum length
x = boundary(:,2);
y = boundary(:,1);
[p1, S1] = polyfit(x(1:end/3), y(1:end/3), 1); % Fit line to first edge
[p2, S2] = polyfit(x(end/3:2*end/3), y(end/3:2*end/3), 1); % Fit line to second edge
[p3, S3] = polyfit(x(2*end/3:end), y(2*end/3:end), 1); % Fit line to third edge (base)
v1 = [polyval(p1, min(x)), min(x)]; % Lower vertex
v2 = [polyval(p2, max(x)), max(x)]; % Right vertex
v3 = [polyval(p3, (min(x)+max(x))/2), (min(x)+max(x))/2]; % Top vertex (midpoint of the base)
fittedX = linspace(min(x), max(x), 100); % Generate points for plotting the fitted lines
plot(fittedX, polyval(p1, fittedX), 'r-', 'LineWidth', 2);
plot(fittedX, polyval(p2, fittedX), 'g-', 'LineWidth', 2);
plot(fittedX, polyval(p3, fittedX), 'b-', 'LineWidth', 2);
plot(v1(2), v1(1), 'mo', v2(2), v2(1), 'mo', v3(2), v3(1), 'mo'); % Plot the vertices
vec1 = [1, p1(1)]; % Vector for the first edge
vec2 = [1, p2(1)]; % Vector for the second edge
vec3 = [-1, -p3(1)]; % Vector for the base (direction matters for the angle)
vec1 = vec1 / norm(vec1); % Normalize the vectors
vec2 = vec2 / norm(vec2);
vec3 = vec3 / norm(vec3);
angle1 = acos(dot(vec1, -vec3)) * (180/pi); % Angle at vertex 1
angle2 = acos(dot(vec2, vec3)) * (180/pi); % Angle at vertex 2
Please refer to the below documentation to learn more about:
Hope it helps!

Categorie

Scopri di più su Images in Help Center e File Exchange

Prodotti


Release

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by