16 Straight line identification and fitting angle bisector (matlab program)

1. Brief description

      

  • Line detection is an important content of digital image processing, and it has very important applications in road recognition, building recognition, medical image analysis and other fields. Through the edge detection of the obtained image, and then use the Hough transform to detect the edge detection line. This method is simple and less affected by the gap and noise in the straight line. The simulation experiment of this method is carried out in Matlab environment, and the straight-line target in the image is detected.

2. Code

 

function chengxu()
%% Step 1
close all
I=imread('1.jpg'); % read image
I=rgb2gray(I); % convert color image to grayscale image
I=edge(double(I)) ; % detect the edge of the image
figure
imshow(I) % display the result of the edge detection

%% step 2

[m,n]=size(I); % Calculate the size of the image

M=3; %Define the number of blocks divided in the X direction
N=3; %Define the number of blocks divided in the Y direction
mm=floor(m/M); %The length of the sub-block row
nn=floor(n/N); %sub The length of the block column
count=1; % counter
figure
for i=1:M
    for j=1:N
        A=I((i-1)*mm+1:i*mm,(j-1)*nn+1 :j*nn); %Segment the original image to get a
        subplot(M,N,count)      
        imshow(A) %Display a subplot
        zuoshangjiao=[(i-1)*mm+1 (j-1)* nn+1]; %The coordinates of the upper left corner of the sub-block
        [x,y,k,b]=zikuai(A,zuoshangjiao);%Get the slope k and intercept b of the straight line fitted by the white pixels in the sub-block ( Call the zikuai function)
        X{count}=x; % save the x coordinates of all white pixels in the sub-block
        Y{count}=y; % save the y coordinates of all white pixels in the sub-block
        K(count)=k; % save the sub-block The slope k of the line fitted in the block
        B(count)=b; % save the intercept b of the line fitted in the sub-block
        count=count+1; %counter plus 1, calculate the next sub-block
    end
end

%% step 3

KK=K(~isnan(K)); % remove NaN in K (sub-block with less than 10 white pixels) BB=
B(~isnan(B)); % remove NaN in B (less white pixels subblocks over 10)

mean_K=mean(KK); % Find the average of all slopes
mean_B=mean(BB); % Find the average of all intercepts

figure
subplot(2,1,1)
plot(KK,'-o')
title('The straight line k value obtained by fitting each sub-block')
subplot(2,1,2)
plot(BB,'-o')
title('The straight line b value obtained by fitting each sub-block')

count1=1;
count2=1;
for i=1:length(K)
    if ~isnan(K(i))
        if K(i)>mean_K % is greater than the average slope of the sub-blocks, the white pixels of these sub-blocks Set the position to cell type arrays X1 and Y1 (store x and y respectively)
            X1{count1}=X{i};
            Y1{count1}=Y{i};
            count1=count1+1;
        else % less than the average value of the slope Block, collect the white pixel positions of these sub-blocks into cell arrays X2 and Y2 (store x and y respectively)
            X2{count2}=X{i};
            Y2{count2}=Y{i};
            count2=count2+ 1;
        end
    end
end

XX1=[];
YY1=[];
XX2=[];
YY2=[];

for i=1:length(X1) % is greater than the average slope of the sub-blocks, the white pixel positions of these sub-blocks are collected into a double array, which is convenient for calculation XX1=[XX1;X1{i}]; YY1
    =
    [ YY1;Y1{i}];
end

for i=1:length(X2) % is less than the average value of the slope of the sub-blocks, the white pixel positions of these sub-blocks are collected into a double array, which is convenient for calculation XX2=[XX2;X2{i}]; YY2
    =
    [ YY2;Y2{i}];
end

%% Straight line 1 and straight line 2 are extracted from the image respectively as XX1, YY1 and XX2, YY2
%% The slope k1 and intercept b1 of straight line 1 are obtained by fitting discrete points
A1=[XX1,ones(length(XX1) ,1)];
kb1=A1\YY1;
k1=kb1(1);
b1=kb1(2);
            
%% Slope k2 and intercept b2 of line 2 obtained by fitting discrete points
A2=[XX2,ones(length( XX2),1)];
kb2=A2\YY2;
k2=kb2(1);
b2=kb2(2);

xx0=[1 m];
yy0=[1 n];

yy1=k1*xx0+b1; % get two points on line 1 to draw line 1
yy2=k2*xx0+b2; % get two points on line 3 to draw line 2

%% Draw the fitted straight line and the separated discrete points
%% Straight line 1
figure
axis([1,m,1,n]) %Set the display range
hold on
scatter(XX1,YY1,'LineWidth',5) % Draw the white pixel position corresponding to line 1
plot(xx0,yy1,'r','LineWidth',3) % Draw the fitted line 1

%% Line 2
hold on
scatter(XX2,YY2,'k','LineWidth',5) % draw the white pixel position corresponding to line 2 plot
(xx0,yy2,'y','LineWidth',3) % draw the intended line 2

%% step 4

%% Find the intersection of two straight lines
X0=(b2-b1)/(k1-k2);
Y0=k1*X0+b1;

alpha=atan(k1); % angle between line 1 and x
beta=atan(k2); % angle between line 2 and x

K01=tan((alpha+beta)/2); %Slope of angle bisector 1
K02=tan(-pi/2+(alpha+beta)/2); %Slope of angle bisector 2

B01=Y0-K01*X0; % intercept of angle bisector 1
B02=Y0-K02*X0; % intercept of angle bisector 2

%% draw angle bisector
xx0=[1 m];
yy0=[1 n];

YY1=K01*xx0+B01; % angle bisector 1
YY2=K02*xx0+B02; % angle bisector 2
plot(xx0,YY1); % draw angle bisector 1
plot(xx0,YY2); % draw angle bisector line 2

view([90 90])
end

function [x,y,k,b]=zikuai(feikuai,zuoshangjiao)
%% Find the straight line y=kx+b fitted by each sub-block data, return k and b, and the coordinate
x0 of the white pixel in the sub-block =zuoshangjiao(1); %Get the x-coordinate of the upper left corner of the sub-block
y0=zuoshangjiao(2); %Get the y-coordinate of the upper-left corner of the sub-block

[m,n]=size(feikuai); %Calculate the size of the sub-block
N=1; %N counts from 1
x=[]; %Define the vector x
y=[]; % Define the vector y to save the y coordinates of the white pixels of the sub-block
for i=1:m % Scan the pixel values ​​of each point of the sub-block in turn
    for j=1:n
        if feikuai(i,j)==1 % If it is a white pixel, save The coordinates of the pixel
            x(N)=i; % save the relative coordinates (x direction)
            y(N)=j; % save the relative coordinates (y direction)
            N=N+1; % add 1 to the count and judge the
        end of the next pixel
    end
end

if length(x)<10 % Sub-blocks with less than 10 white pixels (including sub-blocks without white points), discarded (the number of points is too small, it may be noise) k=NaN; % assign kbxy to NaN (Matlab
    system variable, indicating Not a Number), to show the difference from other sub-blocks
    b=NaN;
    x=NaN;
    y=NaN;
    return %return
else %The number of white pixels in the sub-block is greater than or equal to 10, and the coordinates of the white pixels Perform fitting to obtain the slope k and intercept of the straight line b
    x=x+x0; % get the absolute coordinates of the white pixels in the sub-block relative to the original image x
    y=y+y0; % get the relative coordinates of the white pixels in the sub-block In the absolute coordinates of the original image y
    x=x';
    A=[x,ones(length(x),1)]; %Definition matrix A (straight line fitting is the least square solution of the solution beyond the linear equations, the unknown is k and b) namely A*[k;b]=y';
    y=y';
    kb=A\y; % get the least squares solution, corresponding to k and b     
    k=kb(1); % get k
    b= kb(2); % get b
end
end

 

3. Running results

eb24f3be2a754b99932763c61396ffd2.png

7b725569944249f5a4378b36b0b14211.png 

 26d17b1d03a3478693dcba0ff551c700.png

 2259f67a4b3a479693f83088622cb040.png

 588491549e7b4a3981a1817d81d50040.png

 

 

Guess you like

Origin blog.csdn.net/m0_57943157/article/details/131565061