logo

大佬您好,我想通过找直角来确定矩形的四个顶点,请问有什么办法来记录每次拍摄时最距离摄像头最近的两个直角的顶点吗?

2022-05-12 21:24
P
rm4a
enable_lens_corr = False # turn on for straighter lines...
import sensor, image, time
sensor.reset()
sensor.set_pixformat(sensor.GRAYSCALE) # grayscale is faster
sensor.set_framesize(sensor.QVGA)
sensor.skip_frames(time = 2000)
clock = time.clock()
right_angle_threshold = (60, 90)
binary_threshold = [(0, 180)]
forget_ratio = 0.8
move_threshold = 5
k=0
x=[0,0,0,0]
y=[0,0,0,0]
def calculate_angle(line1, line2):
    # 利用四边形的角公式, 计算出直线夹角
    angle  = (180 - abs(line1.theta() - line2.theta()))
    if angle > 90:
        angle = 180 - angle
    return angle


def is_right_angle(line1, line2):
    global right_angle_threshold
    # 判断两个直线之间的夹角是否为直角
    angle = calculate_angle(line1, line2)
    if angle >= right_angle_threshold[0] and angle <=  right_angle_threshold[1]:
        # 判断在阈值范围内
        return True
    return False

def find_verticle_lines(lines):
    line_num = len(lines)
    for i in range(line_num -1):
        for j in range(i, line_num):
            if is_right_angle(lines[i], lines[j]):
                return (lines[i], lines[j])
    return (None, None)


def calculate_intersection(line1, line2):
    # 计算两条线的交点
    a1 = line1.y2() - line1.y1()
    b1 = line1.x1() - line1.x2()
    c1 = line1.x2()*line1.y1() - line1.x1()*line1.y2()

    a2 = line2.y2() - line2.y1()
    b2 = line2.x1() - line2.x2()
    c2 = line2.x2() * line2.y1() - line2.x1()*line2.y2()

    if (a1 * b2 - a2 * b1) != 0 and (a2 * b1 - a1 * b2) != 0:
        cross_x = int((b1*c2-b2*c1)/(a1*b2-a2*b1))
        cross_y = int((c1*a2-c2*a1)/(a1*b2-a2*b1))
        return (cross_x, cross_y)
    return (-1, -1)


def draw_cross_point(cross_x, cross_y):
    img.draw_cross(cross_x, cross_y)
    img.draw_circle(cross_x, cross_y, 5)
    img.draw_circle(cross_x, cross_y, 10)

old_cross_x = 0
old_cross_y = 0

while(True):
    clock.tick()
    img = sensor.snapshot()
    #img.binary(binary_threshold)
    lines =  img.find_lines(threshold = 2000, theta_margin = 20, rho_margin = 20, roi=(0, 0, 320,200))
    for line in lines:
        pass
       # img.draw_line(line.line(), color = (255, 0, 0))
    # 如果画面中有两条直线
    if len(lines) >= 2:
        (line1, line2) = find_verticle_lines(lines)
        if (line1 == None or line2 == None):
            # 没有垂直的直线
            continue
        # 画线
        #img.draw_line(line1.line(), color = (255, 0, 0))
        #img.draw_line(line2.line(), color = (255, 0, 0))

        # 计算交点
        (cross_x, cross_y) = calculate_intersection(line1, line2)
        print("cross_x:  %d, cross_y: %d"%(cross_x, cross_y))

        if cross_x != -1 and cross_y != -1:
                if(k<4):
                    x[k]=cross_x
                    y[k]=cross_y
                    k=k+1
                if(k==4):
                    xz=int((x[0]+x[1]+x[2]+x[3])/4)
                    yz=int((y[0]+y[1]+y[2]+y[3])/4)
                    draw_cross_point(xz, yz)
                    k=0
P
rm4a

这个想通过找直角来记录矩形的四个顶点求平均,来找矩形的中心,但是我像通过找最低的两个直角顶点来求平均 ,以此确定整个矩形最低边的中心,但是不知道如何实现,请问大佬能指点一下吗?

2022-05-13 18:27
I
网红小智智 星瞳科技

https://book.openmv.cc/example/09-Feature-Detection/find-rects.html

这个不行吗?

2022-05-13 19:46
P
rm4a

@kidswong999 感觉帧率没有找线快

2022-06-17 14:18
K
hxcb

@kidswong999 这个如果是两个矩形叠加起来的话识别不准,能不能做成先测量一下距离,然后只检测那个距离平面的矩形