How to check if four points form a rectangle

后端 未结 4 1816
隐瞒了意图╮
隐瞒了意图╮ 2021-02-14 15:52

I am working on a shape recognition app. At this moment a set of points (x,y) is determined by corner detector (red points, img. 2.). Four of these points (in red frames, img. 2

4条回答
  •  自闭症患者
    2021-02-14 16:31

    You are aware that by visually inspecting the point cloud you can already distinguish a multitude of rectangles? In other words, you will likely find many rectangles if you don't do a sort of pre-selection routine...

    Anyway, aside from the method already given by @mathematician1975, you can also just check if the sides are (more or less) parallel.

    Let's call @mathematician1975's method method 1 and parallel-check method 2. Then:

    %# method 1: 
    n1 = |u1-u2|    %#  3 sub., 3 mult, 2 add. per distance
    n2 = |u3-u2|    %#  total of 6 distances to compute.
    n3 = |u4-u3|    %#  then max 5+4+3+2+1 = 15 comp. to find unique distances
    n4 = |u1-u4|    
    n5 = |u4-u2|    %#  Total:
    n6 = |u3-u1|    %#  12 sub., 18 mult., 12 add, 15 comp
    
    
    
    %# method 2:
    w1 = u1-u2       %#  3 subtractions per vector
    w2 = u3-u2       %#  total of 4 vectors to compute 
    w3 = u3-u2
    w4 = u1-u4                
                            %#  12 sub.
    abs(w1-w3) == [0 0 0]   %#  3 sub., 3 comp., 1 sign.
    abs(w2-w4) == [0 0 0]   %#  3 sub., 3 comp., 1 sign.
    
                            %# Total: 18 sub., 6 comp. 2 sign.
    

    Note that these are both worst-case; with a bit of bookkeeping you can drastically reduce the cost of both.

    Note also that method 2 needs to know beforehand that the vertices are already in the right order. If this is not the case, it'll increase the cost by a factor of 4, which is more that method 1..

    May I ask how you are computing the distances?

提交回复
热议问题