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 lane_detect(img): frameWidth = w frameHeight = h intialTracbarVals = (38, 60, 12, 90) count = 0 noOfArrayValues = 11 global arrayCurve, arrayCounter arrayCounter = 0 arrayCurve = np.zeros([noOfArrayValues]) myVals = [] #utlis.initializeTrackbars(intialTracbarVals) # imgWarpPoints = img.copy() imgFinal = img.copy() imgCanny = img.copy() try: # imgUndis = utlis.undistort(img) imgThres, imgCanny, imgColor = utlis.thresholding(img) src = utlis.valTrackbars() imgWarp = utlis.perspective_warp(imgThres, dst_size=(frameWidth, frameHeight), src=src) # imgWarpPoints = utlis.drawPoints(imgWarpPoints, src) imgSliding, curves, lanes, ploty = utlis.sliding_window( imgWarp, draw_windows=False) curverad = utlis.get_curve(imgFinal, curves[0], curves[1]) lane_curve = np.mean([curverad[0], curverad[1]]) imgFinal = utlis.draw_lanes(img, curves[0], curves[1], frameWidth, frameHeight, src=src) #currentCurve = lane_curve // 50 # if int(np.sum(arrayCurve)) == 0:averageCurve = currentCurve # else: # averageCurve = np.sum(arrayCurve) // arrayCurve.shape[0] # if abs(averageCurve-currentCurve) >200: arrayCurve[arrayCounter] = averageCurve # else :arrayCurve[arrayCounter] = currentCurve # arrayCounter +=1 # if arrayCounter >=noOfArrayValues : arrayCounter=0 # cv2.putText(imgFinal, str(int(averageCurve)), (frameWidth//2-70, 70), cv2.FONT_HERSHEY_DUPLEX, 1.75, (0, 0, 255), 2, cv2.LINE_AA) # imgFinal= utlis.drawLines(imgFinal,lane_curve) except: # print("NO Lane Detected") imgFinal = img cv2.putText(imgFinal, "No Lane Detected", (10, 20), cv2.FONT_HERSHEY_DUPLEX, 1, (0, 0, 255), 1, cv2.LINE_AA) return imgFinal
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
myVals=[] utlis.initializeTrackbars(intialTracbarVals) while True: success, img = cap.read() #img = cv2.imread('test3.jpg') if cameraFeed== False:img = cv2.resize(img, (frameWidth, frameHeight), None) imgWarpPoints = img.copy() imgFinal = img.copy() imgCanny = img.copy() imgUndis = utlis.undistort(img) imgThres,imgCanny,imgColor = utlis.thresholding(imgUndis) src = utlis.valTrackbars() imgWarp = utlis.perspective_warp(imgThres, dst_size=(frameWidth, frameHeight), src=src) imgWarpPoints = utlis.drawPoints(imgWarpPoints, src) imgSliding, curves, lanes, ploty = utlis.sliding_window(imgWarp, draw_windows=True) try: curverad =utlis.get_curve(imgFinal, curves[0], curves[1]) lane_curve = np.mean([curverad[0], curverad[1]]) imgFinal = utlis.draw_lanes(img, curves[0], curves[1],frameWidth,frameHeight,src=src) # ## Average currentCurve = lane_curve // 50 if int(np.sum(arrayCurve)) == 0:averageCurve = currentCurve else: averageCurve = np.sum(arrayCurve) // arrayCurve.shape[0] if abs(averageCurve-currentCurve) >200: arrayCurve[arrayCounter] = averageCurve
heightImg = 1600 widthImg = 1200 utlis.initializeTrackbars() count=0 while True: img = cv2.imread(pathImage)#READING IMAGE img = cv2.resize(img, (widthImg, heightImg)) # RESIZE IMAGE imgBlank = np.zeros((heightImg,widthImg, 3), np.uint8) # CREATE A BLANK IMAGE FOR TESTING DEBUGING IF REQUIRED imgGray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) # CONVERT IMAGE TO GRAY SCALE imgBlur = cv2.GaussianBlur(imgGray, (5, 5), 1) # ADD GAUSSIAN BLUR thres=utlis.valTrackbars() # GET TRACK BAR VALUES FOR THRESHOLDS imgThreshold = cv2.Canny(imgBlur,thres[0],thres[1]) # APPLY CANNY BLUR kernel = np.ones((5, 5))#INITIALISING KERNEL imgDial = cv2.dilate(imgThreshold, kernel, iterations=2) # APPLY DILATION imgThreshold = cv2.erode(imgDial, kernel, iterations=1) # APPLY EROSION ## FIND ALL COUNTOURS imgContours = img.copy() # COPY IMAGE FOR DISPLAY PURPOSES imgBigContour = img.copy() # COPY IMAGE FOR DISPLAY PURPOSES contours, hierarchy = cv2.findContours(imgThreshold, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) # FIND ALL CONTOURS cv2.drawContours(imgContours, contours, -1, (0, 255, 0), 10) # DRAW ALL DETECTED CONTOURS # FIND THE BIGGEST COUNTOUR biggest, maxArea = utlis.biggestContour(contours) # FIND THE BIGGEST CONTOUR if biggest.size != 0:
count = 0 while True: # if webCamFeed:success,img = cap.read() # else: # img = cv2.imread('03_oswal.png') img = cv2.imread('03_oswal.png') img = cv2.resize(img, (widthImg, heightImg)) # RESIZE IMAGE imgBlank = np.zeros((heightImg, widthImg, 3), np.uint8) imgGray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) imgBlur = cv2.GaussianBlur(imgGray, (5, 5), 1) thres = utlis.valTrackbars() imgThreshold = cv2.Canny(imgBlur, thres[0], thres[1]) kernel = np.ones((5, 5)) imgDial = cv2.dilate(imgThreshold, kernel, iterations=2) #elimina blancos imgThreshold = cv2.erode(imgDial, kernel, iterations=1) # engruesa la imagen imgContours = img.copy() imgBigContour = img.copy() contours, hierarchy = cv2.findContours(imgThreshold, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) cv2.drawContours(imgContours, contours, -1, (0, 255, 0), 10) biggest, maxArea = utlis.biggestContour(contours)
utlis.initializeTrackbars() count = 0 while True: imgBlank = np.zeros((heightImg, widthImg, 3), np.uint8) if webCam: success, img = cap.read() else: img = cv2.imread(imageLocation) img = cv2.imread(imageLocation) img = cv2.resize(img, (widthImg, heightImg)) imgGray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) #convets the image to grayscale imgBlur = cv2.GaussianBlur( imgGray, (5, 5), 1) #converts the grayscale image to blured image thres = utlis.valTrackbars( ) #using this function we get the trackbars which help us adjust the threshold imgThreshold = cv2.Canny(imgBlur, thres[0], thres[1]) kernel = np.ones((5, 5)) imgDial = cv2.dilate(imgThreshold, kernel, iterations=2) # APPLY DILATION imgThreshold = cv2.erode(imgDial, kernel, iterations=1) # APPLY EROSION ## FIND ALL COUNTOURS imgConts = img.copy() imgBigConts = img.copy() contours, hierarchy = cv2.findContours( imgThreshold, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) # FIND ALL CONTOURS cv2.drawContours(imgConts, contours, -1, (0, 255, 0), 10) # DRAW ALL DETECTED CONTOURS # FIND THE LARGEST COUNTOUR IN THE FRAME