Perform angle recognition on the objects in the following images

1 view (last 30 days)
clc
clear
A = imread('shipai.jpg');
A = rgb2gray(A);
BW = A<128;
% find angle of dominant line
[H,T,R] = hough(BW);
P = houghpeaks(H,5,'threshold',ceil(0.3*max(H(:))));
x = T(P(:,2)); y = R(P(:,1));
plot(x,y,'s','color','white');
lines = houghlines(BW,T,R,P,'FillGap',5,'MinLength',7);
imshow(A), hold on
max_len = 0;
for k = 1:length(lines)
xy = [lines(k).point1; lines(k).point2];
plot(xy(:,1),xy(:,2),'LineWidth',2,'Color','green');
% Plot beginnings and ends of lines
plot(xy(1,1),xy(1,2),'x','LineWidth',2,'Color','yellow');
plot(xy(2,1),xy(2,2),'x','LineWidth',2,'Color','red');
% Determine the endpoints of the longest line segment
len = norm(lines(k).point1 - lines(k).point2);
if ( len > max_len)
max_len = len;
xy_long = xy;
end
end
n = length(lines);
for i=1:n-1
k=(lines(i).theta-lines(i+1).theta);
if abs(k>=15)
jiaodu=k
return
end
end
That's the code I used originally. It has a good effect on the following image recognition
Then you can get that the angle of two of the lines is 90
But for the following pictures, we can't recognize straight lines very well
I can't recognize the boundary straight line very well. The main reason is that my camera is not very good and the fabric is a little reflective. Is there any better way to deal with it? Thank you
  4 Comments
Image Analyst
Image Analyst on 17 Mar 2022
I've spent about a third or half my working career over the past 30 years working on fabric image analysis. There are many papers published on it. See this link:

Sign in to comment.

Accepted Answer

Simon Chan
Simon Chan on 17 Mar 2022
Edited: Simon Chan on 17 Mar 2022
Do some filtering before searching for lines.
Need to increase the number of peaks to 20 for function houghpeaks otherwise it is not possible to detect the diagonal line.
clear; clc;
rawdata = imread('fabric.png');
A = rgb2gray(rawdata);
se = strel('square',3);
BW1 = imbothat(A,se); % Filtering
BW2 = bwareaopen(BW1>2,5); % Remove small objects
% find angle of dominant line
[H,T,R] = hough(BW2);
P = houghpeaks(H,20,'threshold',ceil(0.3*max(H(:)))); % Increase number of peaks = 20
x = T(P(:,2)); y = R(P(:,1));
plot(x,y,'s','color','white');
lines = houghlines(BW2,T,R,P,'FillGap',5,'MinLength',7);
imshow(A), hold on
max_len = 0;
for k = 1:length(lines)
xy = [lines(k).point1; lines(k).point2];
plot(xy(:,1),xy(:,2),'LineWidth',2,'Color','green');
% Plot beginnings and ends of lines
plot(xy(1,1),xy(1,2),'x','LineWidth',2,'Color','yellow');
plot(xy(2,1),xy(2,2),'x','LineWidth',2,'Color','red');
% Determine the endpoints of the longest line segment
len = norm(lines(k).point1 - lines(k).point2);
if ( len > max_len)
max_len = len;
xy_long = xy;
end
end
n = length(lines);
for i=1:n-1
k=(lines(i).theta-lines(i+1).theta);
if abs(k>=15)
jiaodu=k
return
end
end
  4 Comments
Simon Chan
Simon Chan on 17 Mar 2022
You may extend the line manually from the result in variable 'lines'.
tao wang
tao wang on 17 Mar 2022
Thank you. I used the parameter equation of two points to calculate the intersection with the picture, but it's more troublesome if the denominator is equal to 0. Thank you. I'll try again

Sign in to comment.

More Answers (1)

Matt J
Matt J on 17 Mar 2022
Edited: Matt J on 17 Mar 2022
Perhaps as follows,
load Image
BW = edge(im2gray(A));
for i=1:5
E=bwmorph(BW,'endpoints');
BW=BW&~E;
end
BW=bwareaopen(BW,6);
imshow(BW)
T=regionprops('table',BW,'Orientation');
angle=mean(T.Orientation)
angle = -24.0316
histogram(T.Orientation)

Community Treasure Hunt

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

Start Hunting!