canny算子计算矩形边缘疑问
-
各位大佬,我用canny算子计算矩形边缘,得到的很清晰的矩形边缘轮廓,然后我进行了识别矩形和识别顶点的操作,但是出现了识别不稳定的情况,也就是一会能识别到一会识别不到,但是如果不使用边缘检测,只用灰度阈值找矩形边缘的时候就能连续不间断的识别顶点,请问我应该怎么做才能很好的识别矩形边缘并且连续的识别矩形四个顶点呢?
import pyb, sensor, image, math, time from pyb import LED import pyb sensor.reset() sensor.set_framesize(sensor.QVGA) sensor.set_pixformat(sensor.GRAYSCALE) sensor.skip_frames(time = 2000) clock = time.clock() red = LED(1) # 定义一个LED1 红灯 green = LED(2) # 定义一个LED2 绿灯 blue = LED(3) # 定义一个LED3 蓝灯 white = LED(4) # 定义一个LED4 照明灯 Center = [0.1,0.1,0.1] X =230 flag = 0 while(True): flag = 0 clock.tick() img = sensor.snapshot() edges = img.find_edges(image.EDGE_CANNY, threshold=(100,140)) for r in edges.find_rects(threshold = 70000): edges.draw_rectangle(r.rect(), color=(255,255,255)) print("Four corners:",end = "") print(r.rect()) four_corner = r.rect() if (four_corner[2]>60 and four_corner[2]<110 and four_corner[3]>60 and four_corner[3]<110): #white.on() Center[0] = 0 Center[1] = four_corner[0]+four_corner[2]/2 Center[2] = 240-(four_corner[1]+four_corner[3]/2) #原点从左上角转换到左下角 print(type(Center[1]),end = "") print(Center[1]) if Center[1]>200: Center[0] = 200 Center[1] = four_corner[0]+four_corner[2]/2-200 CenterPoint=bytearray([20, int(Center[0]), int(Center[1]), int(Center[2]), 10]) print(Center) uart.write(CenterPoint) flag = flag+1 else : CenterPoint=bytearray([20, int(Center[0]), int(Center[1]), int(Center[2]), 10]) print(Center) uart.write(CenterPoint) flag = flag+1 for p in r.corners(): img.draw_circle(p[0],p[1],6,color=(255,255,255)) #print(r) print("FPS %f" % clock.fps()) if flag==0: print(CenterPoint)
-
用find_rects找到四个顶点就行了,不用找边。