Answer the question
In order to leave comments, you need to log in
How to determine the slope of a square with a known side from its Bounding Box?
Hello,
there are squares in the image with a known side length, I need to know the slope about the x-axis. The MATLAB regionprops function returns the axis-aligned Bounding Boxes. As I reasoned:
Let the width of the boundingbox w, the side of the square c, the angle of inclination alpha. Then
Use the Weerstrass substitution Simplify
the equation Solve the
quadratic equation with respect to tan(alpha/2) , coefficients
For a non-zero slope, the discriminant is positive
The logic seems to be correct, the calculations too, but the answer is wrong. Tell me, please, what I made a mistake, and advise a way to accurately determine the slope of the actual square inscribed in the square.
Here is an example in MATLAB
img = false(25,25);
img(5:16,5:16) = true;
rot_img = imrotate(img, 30, 'crop');
props = regionprops(bwlabel(rot_img),'BoundingBox');
bbox = cat(1,props.BoundingBox);
w = bbox(3);
h = 12;
a = -1*(1+w/h); b = 2; c = 1 - w/h;
D = b^2 - 4*a*c;
alpha = 2*atand((-b + sqrt(D))/(2*a));
%alpha = 25.5288
p = bwperim(rot_img);
theta=0:0.1:179.9;
[R,xp] = radon(p,theta); %Преобразование Радона
a=imregionalmax(R,true(3,3)); %Локальные двумерные максимумы преобразования Радона
[r,c]=find(a); idx=sub2ind(size(a),r,c); maxvals=R(idx);
[val,midx]=sort(maxvals,'descend');
mean(rem(theta(c(midx(1:4))),90)) %Выбираем 4 самых больших локальных максимума
%29.85
Answer the question
In order to leave comments, you need to log in
w/c=sin(a)+cos(a)
(w/c)^2=1+sin(2a)
sin(2a)=(w/c)^2-1
a=0.5*arcsin((w/c)^2-1)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question