Corner filtering: a matlab example that simply uses skeleton local linear regression

Due to the need for accurate counting, it is necessary to extract the skeleton after the binary graph is corroded, and after removing the bifurcation, call the MATLAB's own function corner to extract the corners. The operations involved are all built-in functions:

  • Extract the skeleton function: J=bwmorph(I,'skel',refine_time); refine_time is the number of refinement. When the value is Inf, the skeleton is refined to only one pixel width. The input and output images I and J are both two Value image, the same below;
  • De-skeleton bifurcation function: J=bwmorph(I,'spur',spur_time); spur is the number of de-forking.
  • Corner detection function: C=corner(I,corner_num); corner_num is the number of corner points to be extracted, the default is 200. The output is a corner_num*2 size matrix, and each row represents a corner point coordinate.

Binary graph, skeleton graph, de-branching graph, corner detection graph are as follows:

The red asterisks in the figure are corner points. The corner point display code is as follows:

    C=corner(Ispur,corner_num);
    figure
    imshow(Ispur)
    hold on
    plot(C(:,1),C(:,2),'r*');    

 

Since the total number of output corner points is exactly the same as the total number of expected corner points input (unless the total number of expected corner points exceeds all corner points), further filtering of the corner points is required: filter out the corner points distributed near the linear skeleton and keep The corner points located at the corners and intersections (forking needs to be cleaned up in advance, otherwise it will affect the results), which is also the core of this article. Algorithm introduction and schematic diagram:

Assuming that the image above is an input binary image, the square represents a pixel, the red square represents a corner pixel, and the white square represents a pixel that constitutes the skeleton. The algorithm selects the target corner as the center and within a certain range (blue in the figure). Color box, the size of 2R*2R) of all the pixels on the skeleton (the white pixels marked as 1, 2,...6 in the figure), the coordinates of these pixels (including the corner pixels) (xi ,yi) Perform linear regression to obtain linear function \hat{y}=\hat{a}x+\dot{\hat{b}}and regression coefficients with sample points, interval estimates, residuals, confidence intervals, coefficients of determination, F statistic observations, p-values, estimations of error variances, and significance levels. According to the obtained linear combination of one or more values, it is judged whether it is greater or less than a trained or empirically preset threshold thre. This algorithm uses the determination coefficient R^2 as an example to set the threshold thre. The determination coefficient of the linear regression of the skeleton pixel points within a certain range of the center is greater than the threshold thre, then the corner point is considered to be located on a straight line or a straight line, otherwise it is considered to be located at a corner or intersection.

PS: Linear regression can be realized by self-written function, or directly call the function regress, the calling method is:

  •  [b,bint,r,rint,stats]=regress(Y,X)

Where Y is the column vector of the dependent variable, and X is the column vector of the added independent variable (that is, the first column is all 1s, and the second column is x). The output b is the regression coefficient, bint is the interval estimation of the regression coefficient, r is the residual error, rint is the confidence interval, and stats is a matrix of four 4 values ​​used to test the regression model. The four values ​​of stats are interpreted as the coefficient of determination, The observed value of the F statistic, the value of p of the test, the estimate of the error variance.

The test image is captured as a part of the image after de-forking:

Set several corner coordinates (147,124) (163,146) (181,142)) (139,111) [The red circle in the figure below is the corner position]

Code:

function JudgeList=LinearJudge(I,C,R)
    [m,n]=size(I);
    JudgeList=zeros(1,length(C));
    
    for c=1:length(C)
        pn=0;
        ci=C(c,2);
        cj=C(c,1);
        PointList=[];
        for i =ci-R:ci+R
            for j=cj-R:cj+R
                if i>=1 && i<=m && j>=1 && j<=n
                    if I(i,j)==255
                        pn=pn+1;
                        PointList(pn,:)=[i,j];
                    end
                end
            end
        end
        [b,bint,r,rint,stats]=regress(PointList(:,2),[ones(pn,1),PointList(:,1)]);
        JudgeList(c)=stats(1);
    end
end

The input is a binary image I (here converted to an image with only 0 and 255, if it is a binary image of 0 and 1, just change 255 to 1), the corner coordinate matrix C and the range radius R. PointList is the coordinates of the skeleton pixel points within the range of each corner point, and the output is the value corresponding to all corner points, and the determination coefficient stats(1) is taken here.

Test example code:

I=rgb2gray(imread('spur_01.jpg'));
I=uint8(im2bw(I)*255);%转为只有0和255的二值图像
C=[[147,124];[163,146];[181,142];[139,111]];
imshow(I)
hold on
plot(C(:,1),C(:,2),'ro')
R=ceil(sum(sum(I==255))/20);
result=LinearJudge(I,C,R);
thre=0.75;
JudgeList=LinearJudge(I,C,R);%调用算法
C2=C(find(JudgeList<=thre),:);
figure,
imshow(I)
hold on
plot(C2(:,1),C2(:,2),'go')

Where R is the length of the skeleton divided by 20 and rounded up. C2 is the corner coordinate that meets the condition (the output is greater than thre), and the corner that meets the condition is displayed as a green circle:

Apply to the entire image: (R=5,thre=0.72)

PS: For testing only, the specific values ​​of the two parameters can be obtained through debugging and other methods. The algorithm output attribute value can choose residuals and so on.

Guess you like

Origin blog.csdn.net/qq_36614557/article/details/106935524