Example #1
0
def getLaneCurve(img):
    imgCopy = img.copy()

    h, w, c = img.shape
    points = utils.valTrackbars()

    imgThres = utils.thresholding(img)
    # imgWarp = utils.warpImg(img, points, w, h)
    imgWarpThres = utils.warpImg(imgThres, points, w, h)
    imgWarpPoints = utils.drawPoints(imgCopy, points)
    imgLane = img.copy()

    # imgCanny = utils.canny(imgWarp)
    basePoint, imgHist = utils.getHistogram(imgWarpThres, display=True)
    imgSliding, curves, lanes, ploty = utils.sliding_window(imgWarpThres,
                                                            draw_windows=True)

    curverad = utils.get_curve(imgLane, curves[0], curves[1])
    lane_curve = np.mean([curverad[0], curverad[1]])
    imgLane = utils.drawLanes(img,
                              curves[0],
                              curves[1],
                              frameWidth,
                              frameHeight,
                              src=points)
    # print(round((lane_curve-84.41)/7.5,2))

    imgStack = utils.stackImages(
        0.6,
        ([img, imgWarpThres, imgWarpPoints], [imgHist, imgSliding, imgLane]))
    cv2.imshow('Stack', imgStack)

    return cv2.resize(imgStack, (2 * frameHeight, frameHeight))
Example #2
0
def main(processing_method):
    webcam = cv2.VideoCapture(0)

    background_img = get_background_image(webcam)

    _, fst_frame = webcam.read()

    while True:
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break

        _, cur_frame = webcam.read()

        mask = processing_method(cur_frame, fst_frame)

        masked_frame = cv2.bitwise_and(cur_frame, cur_frame, mask=mask)
        indexes = np.where(masked_frame == 0)
        masked_frame[indexes] = background_img[indexes]

        stacked_img = stackImages(
            .6, (
                [cur_frame, background_img],
                [mask, masked_frame]
            )
        )

        cv2.imshow("Imagens", stacked_img)

    webcam.release()
    cv2.destroyAllWindows()
Example #3
0
def getLaneCurve(img):

    # imgCopy = img.copy()
    # imgResult = img.copy()
    #### STEP 1
    imgThres = utils.thresholding(img)

    hT, wT, c = img.shape
    points = utils.valTrackbars()
    imgWarp = utils.warpImg(imgThres, points, wT, hT)
    #imgWarpPoints = utils.drawPoints(imgCopy,points)
    mid, imgHist = utils.getHistogram(imgThres,
                                      display=True,
                                      minPer=0.5,
                                      region=4)
    base, imgHist = utils.getHistogram(imgThres, display=True, minPer=0.9)
    cur = base - mid
    turn.append(cur)
    if len(turn) > avgVal:
        turn.pop(0)

    curve = int(sum(turn) / len(turn))
    cv2.putText(img, str(curve), (wT // 2 - 80, 85), cv2.FONT_HERSHEY_COMPLEX,
                2, (255, 0, 255), 3)
    imgStacked = utils.stackImages(0.7, ([img, imgHist, imgThres]))
    cv2.imshow('ImageStack', imgStacked)

    #cv2.imshow("warp", imgWarp)
    # cv2.imshow("thres", imgThres)
    # cv2.imshow("histogram", imgHist)
    return curve
Example #4
0
def getLaneCurve(img, display=2):
    imgCopy = img.copy()
    imgResult = img.copy()

    #-- Step1: Threshold each frame
    imgThres = utils.thresholding(img)

    #-- Step 2: Warp each frame
    hT, wT, c = img.shape
    points = utils.valTrackbars()
    imgWarp = utils.warpImg(imgThres, points, wT, hT)
    imgWarpPoints = utils.drawPoints(imgCopy, points)

    #-- Step 3: Get midpoint and curve average point to find raw curve value
    midPoint, imgHist = utils.getHistogram(imgWarp,
                                           display=True,
                                           minPer=0.5,
                                           region=4)
    curveAveragePoint, imgHist = utils.getHistogram(imgWarp,
                                                    display=True,
                                                    minPer=0.9)
    curveRaw = curveAveragePoint - midPoint

    #-- Step 4: Find curve value
    curveList.append(curveRaw)
    if len(curveList) > avgVal:
        curveList.pop(0)
    curve = int(sum(curveList) / len(curveList))

    #-- Display options:
    #-- if display not 0, make the component necessary to diplay
    #-- if display = 1, show only final result frames with curve value
    #-- if display = 2, show the whole process of image processing by stacking different frames
    if display != 0:
        imgInvWarp = utils.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)
    if display == 2:
        imgStacked = utils.stackImages(0.7,
                                       ([img, imgWarpPoints, imgWarp],
                                        [imgHist, imgLaneColor, imgResult]))
        cv2.imshow('ImageStack', imgStacked)
    elif display == 1:
        cv2.imshow('Resutlt', imgResult)
Example #5
0
def readVideo():
    cap = cv2.VideoCapture(0)
    cap.set(3, wiImg)
    cap.set(4, heImg)
    cap.set(10, 150)
    while True:
        success, img = cap.read()
        if(success):
            img = cv2.resize(img, (wiImg, heImg))
            im_blank = np.zeros_like(img)
            preProcess(img)
            imContours = img.copy()
            getContours(imThres)
            stack =  stackImages(1, ([img]))
            cv2.imshow("stack", stack)
            cv2.waitKey(1)
        else:
            break
Example #6
0
def main(processing_method):
    webcam = cv2.imread('../resources/câmera_1.png', cv2.IMREAD_COLOR)

    background_img = get_background_image(webcam)

    while True:
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break

        _, cur_frame = webcam.read()

        mask = processing_method(cur_frame)

        masked_frame = cv2.bitwise_and(cur_frame, cur_frame, mask=mask)
        indexes = np.where(masked_frame == 0)
        masked_frame[indexes] = background_img[indexes]

        stacked_img = stackImages(
            .6, ([cur_frame, background_img], [mask, masked_frame]))

        cv2.imshow("Imagens", stacked_img)

    webcam.release()
    cv2.destroyAllWindows()
Example #7
0
    # ============== Inverse Perspective of Seccond Biggest rectangle(Grade)==========================
    imgRawGrade = np.zeros_like(imgGradeDisplay)
    cv2.putText(imgRawGrade,str(int(score)) + "%", (50,100), cv2.FONT_ITALIC, 3, (0,0,255), 5)
    #cv2.imshow("grade",imgRawGrade)

    invmatrixG = cv2.getPerspectiveTransform(ptsG2, ptsG1)
    imgInvGradeDisplay = cv2.warpPerspective(imgRawGrade, invmatrixG, (widthImg, heightImg))
    #cv2.imshow("Inv grade",imgInvGradeDisplay)

    imgFinal = cv2.addWeighted(imgFinal,0.75,imgInvWarp,1,0)
    imgFinal = cv2.addWeighted(imgFinal,0.75,imgInvGradeDisplay,1,0)
    #cv2.imshow("Final Img", imgFinal)



lables = [  ["Original", "Gray", "Blur", "Canny"],
            ["Contours", "Biggest Contour", "Warpped", "Threshold"],
            ["Result", "Raw Drawing", "Inverse Warp", "Final"]  
        ]

blank_img = np.zeros_like(img)
imgArray = ([img, imgGray, imgBlur, imgCanny],          
            [imgContours,imgBiggestContours,imgWarpColored,imgThresh],
            [imgResult, imgRawDrawings, imgInvWarp, imgFinal])

imageStacked = utils.stackImages(imgArray, 0.32)


cv2.imshow("Stacked Img", imageStacked)
cv2.waitKey(0)
Example #8
0
def getLaneCurve(img, display=2):

    imgCopy = img.copy()
    imgResult = img.copy()
    #### STEP 1
    imgThres = utils.thresholding(img)

    #### STEP 2
    hT, wT, c = img.shape
    points = utils.valTrackbars()
    imgWarp = utils.warpImg(imgThres, points, wT, hT)
    imgWarpPoints = utils.drawPoints(imgCopy, points)

    #### STEP 3
    middlePoint, imgHist = utils.getHistogram(imgWarp,
                                              display=True,
                                              minPer=0.5,
                                              region=4)
    curveAveragePoint, imgHist = utils.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 = utils.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 = utils.stackImages(0.7,
                                       ([img, imgWarpPoints, imgWarp],
                                        [imgHist, imgLaneColor, imgResult]))
        cv2.imshow('ImageStack', imgStacked)
    elif display == 1:
        cv2.imshow('Resutlt', imgResult)

    #### NORMALIZATION
    curve = curve / 100
    if curve > 1: curve == 1
    if curve < -1: curve == -1

    return curve
Example #9
0
trackbarWinName = 'Trackbars'
cv2.namedWindow(trackbarWinName)
cv2.resizeWindow(trackbarWinName, 640, 240)
cv2.createTrackbar('Hue min', trackbarWinName, 11, 179, empty)
cv2.createTrackbar('Hue max', trackbarWinName, 37, 179, empty)
cv2.createTrackbar('Sat min', trackbarWinName, 20, 255, empty)
cv2.createTrackbar('Sat max', trackbarWinName, 207, 255, empty)
cv2.createTrackbar('Val min', trackbarWinName, 129, 255, empty)
cv2.createTrackbar('Val max', trackbarWinName, 255, 255, empty)

while True:
    hMin = cv2.getTrackbarPos('Hue min', trackbarWinName)
    hMax = cv2.getTrackbarPos('Hue max', trackbarWinName)
    sMin = cv2.getTrackbarPos('Sat min', trackbarWinName)
    sMax = cv2.getTrackbarPos('Sat max', trackbarWinName)
    vMin = cv2.getTrackbarPos('Val min', trackbarWinName)
    vMax = cv2.getTrackbarPos('Val max', trackbarWinName)

    lowerMatrix = np.array([hMin, sMin, vMin])
    upperMatrix = np.array([hMax, sMax, vMax])
    mask = cv2.inRange(imgHsv, lowerMatrix, upperMatrix)
    imgResult = cv2.bitwise_and(img, img, mask=mask)

    # print(hMin, hMax, sMin, sMax, vMin, vMax)

    imgsStack = stackImages(0.6, ([img, imgHsv], [mask, imgResult]))

    cv2.imshow('All imgs', imgsStack)

    cv2.waitKey(1)
Example #10
0

			#	FINAL IMAGE
			picFinal = cv2.addWeighted(picFinal, 1, imgInverseWarp, 1, 0)
			picFinal = cv2.addWeighted(picFinal, 1, imgInvGrade, 1, 0)

		

		imgBlank = np.zeros_like(pic)
		imgArray = ([pic,picGray,picBlur,picCanny],[picContours, picBiggestContours, imgWarpColored, imgThresh],[imgResult, imRawDrawing, imgInverseWarp, picFinal])

	except:
		imgBlank = np.zeros_like(pic)
		imgArray = ([pic,picGray,picBlur,picCanny],[imgBlank, imgBlank, imgBlank, imgBlank],[imgBlank, imgBlank, imgBlank, imgBlank])		
	
	lables = [["Original","Gray","Edges","Canny"],
	              ["Contour","biggest contour","Warpped","Threshold"],
	              ["Result","Raw drawing","Inv Warp","Final"]]

	imgStack = utils.stackImages(imgArray, 0.5, lables)
	cv2.imshow("pakode",imgStack)
	

	#	SAVE THE IMAGE
	if cv2.waitKey(1) and 0xFF == ord('s'):
		cv2.imwrite('FinalResult.jpg', picFinal)
		cv2.waitKey(300)

	if cv2.waitKey(1) & 0xFF == ord('q'):
        break
Example #11
0
    imgRes = imgWrapColor.copy()
    imgRes = utils.showAnswers(imgRes, myIndex, grading, ans, 5, 5)
    imgResraw = np.zeros_like(imgRes)
    imgResraw = utils.showAnswers(imgResraw, myIndex, grading, ans, 5, 5)
    invmatrix = cv2.getPerspectiveTransform(pt2, pt1)
    imgInvWarp = cv2.warpPerspective(imgResraw, invmatrix, (640, 640))

    imgFinal = cv2.addWeighted(imgFinal, 1, imgInvWarp, 1, 0)
    # cv2.imshow("fgfgf",imgFinal)

    imgRawGrade = np.zeros_like(imggWrapColor)
    cv2.putText(imgRawGrade,
                str(int(score)) + "%", (65, 100), cv2.FONT_HERSHEY_COMPLEX, 3,
                (0, 255, 255), 3)
    invmatrixG = cv2.getPerspectiveTransform(ptg2, ptg1)
    imgInvGradeDisplay = cv2.warpPerspective(imgRawGrade, invmatrixG,
                                             (640, 640))
    # cv2.imshow("FDFDF",imgInvGradeDisplay)
    imgFinal = cv2.addWeighted(imgFinal, 1, imgInvGradeDisplay, 1, 0)

imgBlank = np.zeros_like(img)
imgArray = ([img, imggray, imgblur, imgcany],
            [imgContours, imgBiggestContour, imgWrapColor,
             imgThreshx], [imgRes, imgResraw, imgInvWarp, imgFinal])

imgstacked = utils.stackImages(imgArray, 0.5)
imgstacked = cv2.resize(imgstacked, (640, 640))

cv2.imshow("Stacked Images", imgstacked)
cv2.imshow("Final Image", imgFinal)
cv2.waitKey(0)
Example #12
0
def getLaneCurve(img, display=2):
    #sent an image and get the value of curve
    imgCopy = img.copy()
    imgResult = img.copy()
    ######step 1
    imgThresh = utils.thresholding(img)

    ######step 2
    hT, wT, c = img.shape
    #for points we need trackbar
    points = utils.valTrackbars()
    imgWarp = utils.warpImg(imgThresh, points, wT, hT)
    imgWarpPoints = utils.drawPoints(imgCopy, points)

    #############step 3
    middlePoint, imgHist = utils.getHistogram(imgWarp,
                                              display=True,
                                              minPer=0.5,
                                              region=4)
    curveAveragePoint, imgHist = utils.getHistogram(imgWarp,
                                                    display=True,
                                                    minPer=0.9)
    #print(basePoint - midPoint)
    curveRaw = curveAveragePoint - middlePoint

    ##########step 4 (avrg)
    #avrging because want to smooth transition>> noise reduce happens
    curveList.append(curveRaw)
    if len(curveList) > avrgVal:
        curveList.pop(
            0
        )  # maane koyta niye avrg korbo otar beshi batch hoye gele frst er ta baad diye dibe
    curve = int(sum(curveList) / len(curveList))

    #####Step 5  Display
    if display != 0:
        imgInvWarp = utils.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 = utils.stackImages(0.7,
                                       ([img, imgWarpPoints, imgWarp],
                                        [imgHist, imgLaneColor, imgResult]))
        cv2.imshow('ImageStack', imgStacked)

    elif display == 1:
        cv2.imshow('Result', imgResult)

    #normalization
    curve = curve / 100
    if curve > 1: curve = 1
    if curve < -1: curve = -1
    '''
	cv2.imshow('Threshold', imgThresh)
	cv2.imshow('Warp', imgWarp)
	cv2.imshow('Warp Points', imgWarpPoints)
	cv2.imshow('Histogram', imgHist)
	'''
    return curve
Example #13
0
def getLaneCurve(img, display=2):
    # create a copy image to display warp points
    imgCopy = img.copy()
    imgResult = img.copy()
    
    ##1 First find the threshold image
    imgThres = utils.thresholding(img)

    ##2 Now find the warped tracking coordinates
    hT, wT, c = img.shape
    points = utils.valTrackbars()
    imgWarp = utils.warpImg(imgThres, points, wT, hT)
    imgWarpPoints = utils.drawPoints(imgCopy, points)
    
    ##3 We have to find the center of the base which will give us the center line and 
    #   then compare the pixels on both side. By summation of these pixels we are basically
    #   finding the histogram
    middlePoint, imgHist = utils.getHistogram(imgWarp, display=True, minPer=0.5, region=4)
    # optimize curve for finding centre point for path incase of straight path but unsymmetrical histogram 
    curveAveragePoint, imgHist = utils.getHistogram(imgWarp, display=True, minPer=0.9)
    # subtract this value from the center to get the curve value. 
    curveRaw = curveAveragePoint - middlePoint

    ##4 Averaging the curve value for smooth transitioning
    curveList.append(curveRaw)
    if len(curveList)>avgVal:
        curveList.pop(0)
    curve = int(sum(curveList)/len(curveList))
 
    ##5 Display
    if display != 0:
        imgInvWarp = utils.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:
        # stack all the windows together (just for display purposes, no other requirement)
        imgStacked = utils.stackImages(0.7, ([img, imgWarpPoints, imgWarp],
                                             [imgHist, imgLaneColor, imgResult]))
        cv2.imshow('ImageStack', imgStacked)
    elif display == 1:
        cv2.imshow('Result', imgResult)
 
    ### NORMALIZATION
    curve = curve/100
    if curve>1: curve = 1
    if curve<-1: curve = -1
 
    return curve
Example #14
0
cv2.resizeWindow("HSV", 640, 240)
cv2.createTrackbar("Hue min", "HSV", 0, 179, empty)
cv2.createTrackbar("Hue max", "HSV", 179, 179, empty)
cv2.createTrackbar("Sat min", "HSV", 0, 255, empty)
cv2.createTrackbar("Sat max", "HSV", 255, 255, empty)
cv2.createTrackbar("Value min", "HSV", 0, 255, empty)
cv2.createTrackbar("Value max", "HSV", 255, 255, empty)

while True:
    _, img = cap.read()
    imgHSV = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)

    h_min = cv2.getTrackbarPos("Hue min", "HSV")
    h_max = cv2.getTrackbarPos("Hue max", "HSV")
    s_min = cv2.getTrackbarPos("Sat min", "HSV")
    s_max = cv2.getTrackbarPos("Sat max", "HSV")
    v_min = cv2.getTrackbarPos("Value min", "HSV")
    v_max = cv2.getTrackbarPos("Value max", "HSV")

    lower = np.array([h_min, s_min, v_min])
    upper = np.array([h_max, s_max, v_max])
    mask = cv2.inRange(imgHSV, lower, upper)

    result = cv2.bitwise_and(img, img, mask=mask)

    cv2.imshow("Image Detection", utils.stackImages(0.3, [img, mask, result]))
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

cap.release()
cv2.destroyAllWindows()
            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)

    except:
        lane_curve = 00
        pass

    imgFinal = utils.drawLines(imgFinal, lane_curve)

    imgThres = cv2.cvtColor(imgThres, cv2.COLOR_GRAY2BGR)
    imgBlank = np.zeros_like(img)
    imgStacked = utils.stackImages(
        0.7,
        ([img, imgUndis, imgWarpPoints], [imgColor, imgCanny, imgThres
                                          ], [imgWarp, imgSliding, imgFinal]))

    cv2.imshow("PipeLine", imgStacked)
    cv2.imshow("Result", imgFinal)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

cap.release()
cv2.destroyAllWindows()
Example #16
0
def getLaneCurve(img, display):
   img = cv2.resize(img, (1280, 720))  #resize the frames to the same size used for calibration
   imgCopy = img.copy()  # 
   imgResult = img.copy()
   
   #-- step 1: thresholding frames
   imgThres = utils.thresholding(img)  #input original image frames to thresholding function in utils module

   #-- step 2: warping frames
   hT, wT, c = img.shape  #get image width and height (channels not used)
   points = utils.valTrackbars()  #get lane reference points from current trackbar(valTrackbars() is a function we created to place points on four lane edges)
   imgWarp = utils.warpImg(imgThres, points, wT,hT)  #get warped frame by inputting thresholded frame, lane reference points, width and height
   imgWarpPoints = utils.drawPoints(imgCopy, points)  #draw 

   #-- step 3: 
   midPoint, imgHist =  utils.getHistogram(imgWarp, display=True, minPer=0.5, region=4)
   curveAveragePoint, imgHist = utils.getHistogram(imgWarp, display=True, minPer=0.9)
   curveRaw = curveAveragePoint-midPoint

   ### step 4
   curveList.append(curveRaw)
   if len(curveList)>avgVal:
       curveList.pop(0)
   curve = int(sum(curveList)/len(curveList))


   ### final step
   if display != 0:
       imgInvWarp = utils.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 = utils.stackImages(0.7, ([img, imgWarpPoints, imgWarp],
                                            [imgHist, imgLaneColor, imgResult]))
       cv2.imshow('ImageStack', imgStacked)
   elif display == 1:
       cv2.imshow('Resutlt', imgResult)

   #--- Define Tag
   id_to_find  = 0  #id of the aruco marker to be found is 0
   marker_size  = 4 #marker size in centimeters is 4

   #--- Get the camera calibration path
   calib_path  = "/home/pi/mu_code/SeniorDesign/Aruco_pose_est/"

   #-- Load camera matrix and camera distortion
   camera_matrix   = np.load(calib_path+'camera_matrix.npy')
   camera_distortion   = np.load(calib_path+'camera_distortion.npy')

   #--- 180 deg rotation matrix around the x axis
   R_flip  = np.zeros((3,3), dtype=np.float32)
   R_flip[0,0] = 1.0
   R_flip[1,1] =-1.0
   R_flip[2,2] =-1.0

   #--- Define the aruco dictionary
   aruco_dict  = aruco.getPredefinedDictionary(aruco.DICT_4X4_50)
   parameters  = aruco.DetectorParameters_create()

   #-- Convert in gray scale
   gray    = cv2.cvtColor(img2, cv2.COLOR_BGR2GRAY) #remember, OpenCV stores color images in Blue, Green, Red

   #-- Find all the aruco markers in the image
   corners, ids, rejected = aruco.detectMarkers(image=gray, dictionary=aruco_dict, parameters=parameters)  #, cameraMatrix=camera_matrix, distCoeff=camera_distortion)

   #-- If an aruco marker is found check if it is a marker with id 0
   if ids is not None and ids[0] == id_to_find:
       ret = aruco.estimatePoseSingleMarkers(corners, marker_size, camera_matrix, camera_distortion)  #
       rvec, tvec = ret[0][0,0,:], ret[1][0,0,:]
       distance = tvec[2]
       current_velocity = 0  #initial velocity
       distance = round(distance, 2)

       if distance < 1:
         current_velocity_right = 0
         current_velocity_left = 0
       else if curve < 0:
         current_velocity_right = distance * .8
         current_velocity_left = (distance * .8) + curve
       else if curve > 0:
         current_velocity_right = distance * .8 + curve 
         current_velocity_left = distance * .8
       else:
         current_velocity_right = distance * .8
         current_velocity_left = distance * .8  
  
       r.ChangeDutyCycle(current_velocity_right)
       l.ChangeDutyCycle(current_velocity_left)

   else if (curve < 0):
        print('SLOW LEFT WHEEL!!!')
        r.ChangeDutyCycle(15)
        l.ChangeDutyCycle(12)
   else if (curve > 0):
        print('SLOW RIGHT WHEEL!!!')
        r.ChangeDutyCycle(12)
        l.ChangeDutyCycle(15)
   else:
        r.ChangeDutyCycle(20)
        l.ChangeDutyCycle(20)
Example #17
0
        # APPLY ADAPTIVE THRESHOLD
        imgWarpGray = cv2.cvtColor(imgWarpColored, cv2.COLOR_BGR2GRAY)
        imgAdaptiveThre = cv2.adaptiveThreshold(imgWarpGray, 255, 1, 1, 7, 2)
        imgAdaptiveThre = cv2.bitwise_not(imgAdaptiveThre)
        imgAdaptiveThre = cv2.medianBlur(imgAdaptiveThre, 3)

        # Image Array for Display
        imageArray = ([img, imgGray, imgThreshold, imgContours],
                      [imgBigContour, imgWarpColored, imgWarpGray, imgAdaptiveThre])

    else:
        imageArray = ([img, imgGray, imgThreshold, imgContours],
                      [imgBlank, imgBlank, imgBlank, imgBlank])

    # LABELS FOR DISPLAY
    lables = [["Original", "Gray", "Threshold", "Contours"],
              ["Biggest Contour", "Warp Prespective", "Warp Gray", "Adaptive Threshold"]]

    stackedImage = utils.stackImages(imageArray, 0.75, lables)
    cv2.imshow("Result", stackedImage)

    # SAVE IMAGE WHEN 's' key is pressed
    if cv2.waitKey(1) & amp; 0xFF == ord('s'):
        cv2.imwrite("Scanned/myImage" + str(count) + ".jpg", imgWarpColored)
        cv2.rectangle(stackedImage, ((int(stackedImage.shape[1] / 2) - 230), int(stackedImage.shape[0] / 2) + 50),
                      (1100, 350), (0, 255, 0), cv2.FILLED)
        cv2.putText(stackedImage, "Scan Saved", (int(stackedImage.shape[1] / 2) - 200, int(stackedImage.shape[0] / 2)),
                    cv2.FONT_HERSHEY_DUPLEX, 3, (0, 0, 255), 5, cv2.LINE_AA)
        cv2.imshow('Result', stackedImage)
        cv2.waitKey(300)
        count += 1
Example #18
0
        if obj_cor == 3:
            obj_type = "Tri"
        elif obj_cor == 4:
            asp_ratio = w/float(h)
            if 0.95 < asp_ratio < 1.05:
                obj_type = "Sqr"
            else:
                obj_type = "Rec"
        else:
            obj_type = "Cir"

        cv2.rectangle(img_out, (x, y), (x + w, y + h), (0, 255, 0), 2)
        cv2.putText(img_out, obj_type, (x + w // 2 - 10, y + h // 2), cv2.FONT_HERSHEY_COMPLEX, 0.75, (0, 0, 0), 2)


path = 'resources/shapes.png'
img = cv2.imread(path)

img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
img_blur = cv2.GaussianBlur(img_gray, (7, 7), 1)
img_canny = cv2.Canny(img_blur, 50, 50)
img_blank = np.zeros_like(img)

img_contours = img.copy()
getContours(img_canny, img_contours)

img_stack = stackImages(0.75, [[img, img_gray, img_blur], [img_canny, img_contours, img_blank]])

cv2.imshow("Stacked", img_stack)
cv2.waitKey(0)
Example #19
0
cv2.namedWindow("TrackBars")
cv2.resizeWindow("TrackBars", 640, 240)
# 179 is max value for hue in OpenCV
cv2.createTrackbar("Hue Min", "TrackBars", 0, 179, empty)
cv2.createTrackbar("Hue Max", "TrackBars", 19, 179, empty)
cv2.createTrackbar("Sat Min", "TrackBars", 110, 255, empty)
cv2.createTrackbar("Sat Max", "TrackBars", 240, 255, empty)
cv2.createTrackbar("Val Min", "TrackBars", 153, 255, empty)
cv2.createTrackbar("Val Max", "TrackBars", 255, 255, empty)

while True:
    img = cv2.imread(path)
    # convert to HSV space
    imgHSV = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)

    hue_min = cv2.getTrackbarPos("Hue Min", "TrackBars")
    hue_max = cv2.getTrackbarPos("Hue Max", "TrackBars")
    sat_min = cv2.getTrackbarPos("Sat Min", "TrackBars")
    sat_max = cv2.getTrackbarPos("Sat Max", "TrackBars")
    val_min = cv2.getTrackbarPos("Val Min", "TrackBars")
    val_max = cv2.getTrackbarPos("Val Max", "TrackBars")

    lower = np.array([hue_min, sat_min, val_min])
    upper = np.array([hue_max, sat_max, val_max])
    mask = cv2.inRange(imgHSV, lower, upper)
    imgResult = cv2.bitwise_and(img, img, mask=mask)

    img_stack = stackImages(0.6, [[img, imgHSV], [mask, imgResult]])
    cv2.imshow("stack", img_stack)
    cv2.waitKey(1)