简体   繁体   中英

OpenCV Python Line following Code Low FPS

I tried various ways but i am still getting apx 2-3 fps.

import cv2

import numpy as np

 import cv2 import numpy as np ##cap = cv2.imread('C:\\\\Users\\\\efeongan\\\\Desktop\\\\PYTHOn_OPENCV\\\\linetest.mp4') cap = cv2.VideoCapture('C:\\\\Users\\\\efeongan\\\\Desktop\\\\PYTHOn_OPENCV\\\\linetest.mp4') while True: _, frame = cap.read() gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) lowbg = np.array([0,0,0]) highbg = np.array([50, 50, 50]) kernel = np.ones([10, 10], np.uint8) mask = cv2.inRange(frame, lowbg, highbg) res = cv2.bitwise_and(gray, gray, mask = mask) dilation = cv2.dilate(res, kernel, iterations = 1) lines = cv2.HoughLinesP(dilation,rho=0.02,theta=np.pi/500, threshold=100,lines=np.array([]), minLineLength= 0) a,b,c = lines.shape if lines[1][0][0] > 240: diff = lines[1][0][0] - 240 print(diff) if lines[1][0][0] < 240: diff = 220 - lines[1][0][0] print(diff) for i in range(a): cv2.line(dilation, (lines[i][0][0], lines[i][0][1]), (lines[i][0][2], lines[i][0][3]), (255, 255, 255), 3, cv2.LINE_AA) cv2.line(frame, (240, 800), (240 + diff, 800), (255,0,0),10) cv2.putText(frame,str(diff),(240 + diff,800), cv2.FONT_HERSHEY_SIMPLEX, 1, 255) print("x", lines[1][0][0], cnts) cv2.imshow('raw', frame) k = cv2.waitKey(1) if k == 27: brak cv2.desrtroyAllWindows() 

Do try using a lower frame resolution. You can experiment with smaller resolutions and see how the accuracy decreases with decreasing resolution, and find a trade-off.

Another possibility is to process only a selected region of each frame. Suppose you find the line to follow in one region of the image. You can assume that the location of the line does not change much in the next frame, and process only the region around this line.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM