import cv2 import sys sys.path.append('..') from cv.image_init import ImageInit from cv.find_key_point import FindKeyPoint from cv.hough_line_transform import HoughLines image_p = ImageInit(320, 240, convert_type="BINARY", threshold=120, bitwise_not=False) capture = cv2.VideoCapture(0) fkp = FindKeyPoint(True) hl = HoughLines() while True: ret, frame = capture.read() image2 = image_p.processing(frame) _, image3 = fkp.get_key_point(image2, frame) lines = hl.get_lines(image2, frame) print(lines) cv2.imshow('frame', image2) cv2.imshow("fkp", image3) cv2.imshow("hl", frame) if cv2.waitKey(1) == ord('q'): break capture.release() cv2.destroyAllWindows()
sys.path.append('..') from cv.show_images import ShowImage import time from cv.image_init import ImageInit LINE_CAMERA_WIDTH = 320 LINE_CAMERA_HEIGHT = 240 camera = cv2.VideoCapture('/dev/video0') freq = cv2.getTickFrequency() show_image = ShowImage() init = ImageInit(320, 240) ret, frame = camera.read() while True: t1 = time.perf_counter() # 获取一帧 ret, frame = camera.read() show_image.show(frame) image = init.processing(frame) show_image.show(image, window_name="image") t2 = time.perf_counter() frame_rate_calc = 1.0 / (t2 - t1) print(frame_rate_calc) if cv2.waitKey(1) == ord('q'): break camera.release() cv2.destroyAllWindows()
# fl对象用于寻找引导线偏离图像中心的位置,threshold是控制连续白色的的阈值,也就是只有连续多少个白色像素点才认为已经找到引导线 # direction是开始寻找的方向,True是从左边开始寻找,False是右边。当顺时针绕圈时,引导线大概率出现在右边,所以可以选择False。 fl = FollowLine(width=320, height=240, threshold=15, direction=False) # 串口类,此类最好不要直接使用,而是通过CarController来对车子进行控制 serial = CarSerial(SERIAL, receive=True) # 此类并没有实现PID控制,而是简单的使用了比例这个参数。(现在这么简单的地图还无需用到PID) # 如果需要使用PID可以直接调用car目录下的pid类,同时把此类的比例参数设置为1 ctrl = CarController(serial, proportional=0.4) p_offset = 0 while True: ret, frame = camera.read() # 读取每一帧 frame = init.resize(frame) # 把图像缩小,尺寸有ImageInit在初始化时指定 display.show(frame, "original") image = init.processing(frame) # 对帧进行处理 # 偏置就是白色线的中心点距离图片中心点的距离,比如320*240的图像,中心点在160 offset, render_image = fl.get_offset(image, frame) # 第一个参数是需要处理的图像,第二个参数是需要渲染的图像 # 直接把Offset赋值给CarController,对于一般的线没有问题。 # 但是对于急弯,或者线突然不见了,没办法处理,想稳定的巡线,需要对offset进行处理后再给CarController # PID处理offset后再给CarController是一个选择 # 简单的可以做如下的处理,当找不到线时,会出现offset=-1000的情况,我们可以不理它当它是0. if offset == -1000: offset = p_offset * 1.5 else: p_offset = offset ctrl.follow_line(offset)
if not (self.event_function is None): self.event_function(intersection_number=self.__intersection_number) if __name__ == '__main__': LINE_CAMERA = '/dev/video1' LINE_CAMERA_WIDTH = 320 LINE_CAMERA_HEIGHT = 240 camera = cv2.VideoCapture(LINE_CAMERA) # ret = camera.set(3, LINE_CAMERA_WIDTH) # ret = camera.set(4, LINE_CAMERA_HEIGHT) im_p = ImageInit(320, 240) while True: ret, image = camera.read() cv2.imshow("image", image) image2 = im_p.processing(image) # image_processing(image, width=320, height=240, threshold=248, convert_type="BINARY") cv2.imshow("test_one", image2) fi = FindIntersection(150) # data = _find(image2, (160, 230), image) data2 = fi._find(image2, (160, 200), image) # print(data) print(data2) cv2.imshow("_render_image", image) if cv2.waitKey(1) == ord('q'): break camera.release() cv2.destroyAllWindows()