def getLaneCurve(img,display=2): imgCopy = img.copy() imgResult = img.copy() #### STEP 1 imgThres = utlis.thresholding(img) #### STEP 2 hT, wT, c = img.shape points = utlis.valTrackbars() imgWarp = utlis.warpImg(imgThres,points,wT,hT) imgWarpPoints = utlis.drawPoints(imgCopy,points) #### STEP 3 middlePoint,imgHist = utlis.getHistogram(imgWarp,display=True,minPer=0.5,region=4) curveAveragePoint, imgHist = utlis.getHistogram(imgWarp, display=True, minPer=0.9) curveRaw = curveAveragePoint - middlePoint #### SETP 4 curveList.append(curveRaw) if len(curveList)>avgVal: curveList.pop(0) curve = int(sum(curveList)/len(curveList)) #### STEP 5 if display != 0: imgInvWarp = utlis.warpImg(imgWarp, points, wT, hT, inv=True) imgInvWarp = cv2.cvtColor(imgInvWarp, cv2.COLOR_GRAY2BGR) imgInvWarp[0:hT // 3, 0:wT] = 0, 0, 0 imgLaneColor = np.zeros_like(img) imgLaneColor[:] = 0, 255, 0 imgLaneColor = cv2.bitwise_and(imgInvWarp, imgLaneColor) imgResult = cv2.addWeighted(imgResult, 1, imgLaneColor, 1, 0) midY = 450 cv2.putText(imgResult, str(curve), (wT // 2 - 80, 85), cv2.FONT_HERSHEY_COMPLEX, 2, (255, 0, 255), 3) cv2.line(imgResult, (wT // 2, midY), (wT // 2 + (curve * 3), midY), (255, 0, 255), 5) cv2.line(imgResult, ((wT // 2 + (curve * 3)), midY - 25), (wT // 2 + (curve * 3), midY + 25), (0, 255, 0), 5) for x in range(-30, 30): w = wT // 20 cv2.line(imgResult, (w * x + int(curve // 50), midY - 10), (w * x + int(curve // 50), midY + 10), (0, 0, 255), 2) #fps = cv2.getTickFrequency() / (cv2.getTickCount() - timer); #cv2.putText(imgResult, 'FPS ' + str(int(fps)), (20, 40), cv2.FONT_HERSHEY_SIMPLEX, 1, (230, 50, 50), 3); if display == 2: imgStacked = utlis.stackImages(0.7, ([img, imgWarpPoints, imgWarp], [imgHist, imgLaneColor, imgResult])) cv2.imshow('ImageStack', imgStacked) cv2.imshow('Warp Points', imgWarpPoints) elif display == 1: cv2.imshow('Resutlt', imgResult) #### NORMALIZATION curve = curve/100 if curve>1: curve ==1 if curve<-1:curve == -1 return curve
def getLaneCurve(img): imgCopy = img.copy() #Step1 imgThres = utlis.thresholding(img) #Step2 h,w,c = img.shape points = utlis.valTrackbars() imgWarp = utlis.warpImg(img,points,w,h) imgWarpPoints = utlis.drawPoints(imgCopy,points) cv2.imshow("Thres", imgThres) cv2.imshow("Warp", imgWarp) cv2.imshow("Warp Points", imgWarpPoints) return None
cap.set(10, 160) cap.set(3, 640) cap.set(4, 480) scale = 3 wP = 210 * scale #width of a4 paper hP = 297 * scale #Height of a4 paper while True: if webcam: success, img = cap.read() else: img = cv2.imread(path) imgCont, conts = utlis.getContours(img, minArea=5000, filter=4) if len(conts) != 0: biggest = conts[0][2] imgWarp = utlis.warpImg(img, biggest, wP, hP) imgCont2, conts2 = utlis.getContours(imgWarp, minArea=2000, filter=4, cThr=[50, 50], draw=False) if len(conts) != 0: for obj in conts2: cv2.polylines(imgCont2, [obj[2]], True, (0, 255, 0), 2) nPoints = utlis.reorder(obj[2]) nW = round( utlis.findDis(nPoints[0][0] // scale, nPoints[1][0] // scale), 1) nH = round( utlis.findDis(nPoints[0][0] // scale,
wp = 210 * scale hp = 297 * scale while True: if webcam: success, img = cap.read() else: img = cv2.imread(path) imgContours, finalContours = utlis.getContours(img, minArea=50000, filter=4) if len(finalContours) != 0: biggest = finalContours[0][2] #print(biggest) imgWarp = utlis.warpImg(img, biggest, wp, hp) imgContours2, finalContours2 = utlis.getContours(imgWarp, minArea=2000, filter=4, cThr=[50, 50], draw=False) if len(finalContours2) != 0: for obj in finalContours2: cv2.polylines(imgContours2, [obj[2]], True, (0, 255, 0), 2) nPoints = utlis.reorder(obj[2]) nW = round((utlis.findDis(nPoints[0][0] // scale, nPoints[1][0] // scale) / 10), 1) nH = round((utlis.findDis(nPoints[0][0] // scale, nPoints[2][0] // scale) / 10), 1) cv2.arrowedLine(imgContours2,
def getLaneCurve(img, display=2): imgCopy = img.copy() imgResult = img.copy() #### mask the image and get the white A4 paper imgThres = utlis.thresholding(img) #### Wrap the image to get Bird's-eye view hT, wT, c = img.shape points = [[128, 147], [352, 147], [49, 240], [431, 240]] imgWarp = utlis.warpImg(imgThres, points, wT, hT) # draw the points if display != 0: imgWarpPoints = utlis.drawPoints(imgCopy, points) #### get the middle point to calculate the curve middlePoint, imgHist = utlis.getHistogram(imgWarp, display=True, minPer=0.8, region=4) curveAveragePoint, imgHist = utlis.getHistogram(imgWarp, display=True, minPer=0.9) curveRaw = curveAveragePoint - middlePoint # average the last'10' curve curveList.append(curveRaw) if len(curveList) > avgVal: curveList.pop(0) curve = int(sum(curveList) / len(curveList)) # displays if display != 0: imgInvWarp = utlis.warpImg(imgWarp, points, wT, hT, inv=True) imgInvWarp = cv2.cvtColor(imgInvWarp, cv2.COLOR_GRAY2BGR) imgInvWarp[0:hT // 3, 0:wT] = 0, 0, 0 imgLaneColor = np.zeros_like(img) imgLaneColor[:] = 0, 255, 0 imgLaneColor = cv2.bitwise_and(imgInvWarp, imgLaneColor) imgResult = cv2.addWeighted(imgResult, 1, imgLaneColor, 1, 0) midY = 450 cv2.putText(imgResult, str(curve), (wT // 2 - 80, 85), cv2.FONT_HERSHEY_COMPLEX, 2, (255, 0, 255), 3) cv2.line(imgResult, (wT // 2, midY), (wT // 2 + (curve * 3), midY), (255, 0, 255), 5) cv2.line(imgResult, ((wT // 2 + (curve * 3)), midY - 25), (wT // 2 + (curve * 3), midY + 25), (0, 255, 0), 5) for x in range(-30, 30): w = wT // 20 cv2.line(imgResult, (w * x + int(curve // 50), midY - 10), (w * x + int(curve // 50), midY + 10), (0, 0, 255), 2) # fps = cv2.getTickFrequency() / (cv2.getTickCount() - timer) # cv2.putText(imgResult, 'FPS ' + str(int(fps)), (20, 40), cv2.FONT_HERSHEY_SIMPLEX, 1, (230, 50, 50), 3) if display == 2: imgStacked = utlis.stackImages(0.7, ([img, imgWarpPoints, imgWarp], [imgHist, imgLaneColor, imgResult])) cv2.imshow('ImageStack', imgStacked) cv2.waitKey(1) elif display == 1: cv2.imshow('Resutlt', imgResult) cv2.waitKey(1) # NORMALIZATION curve = curve / 100 if curve > 1: curve == 1 if curve < -1: curve == -1 return curve