Opencv detection of rails

# Import all the relavant libraries
import cv2
import numpy as np
# Function for the slider
def nothing(x):
    pass
# Setting the resolution for the camera device. (When a video file is used, the cv2.resize function is utillized.)
frameWidth = 640
frameHeight = 480
cap = cv2.VideoCapture("Video_Simple.mp4") # File must be in the same directory
cap.set(3, frameWidth)
cap.set(4, frameHeight)

# Function for displaying multiple screens in one window.
def stackImages(imgArray,scale,lables=[]):
    sizeW= imgArray[0][0].shape[1]
    sizeH = imgArray[0][0].shape[0]
    rows = len(imgArray)
    cols = len(imgArray[0])
    rowsAvailable = isinstance(imgArray[0], list)
    width = imgArray[0][0].shape[1]
    height = imgArray[0][0].shape[0]
    if rowsAvailable:
        for x in range ( 0, rows):
            for y in range(0, cols):
                imgArray[x][y] = cv2.resize(imgArray[x][y], (sizeW, sizeH), None, scale, scale)
                if len(imgArray[x][y].shape) == 2: imgArray[x][y]= cv2.cvtColor( imgArray[x][y], cv2.COLOR_GRAY2BGR)
        imageBlank = np.zeros((height, width, 3), np.uint8)
        hor = [imageBlank]*rows
        hor_con = [imageBlank]*rows
        for x in range(0, rows):
            hor[x] = np.hstack(imgArray[x])
            hor_con[x] = np.concatenate(imgArray[x])
        ver = np.vstack(hor)
        ver_con = np.concatenate(hor)
    else:
        for x in range(0, rows):
            imgArray[x] = cv2.resize(imgArray[x], (sizeW, sizeH), None, scale, scale)
            if len(imgArray[x].shape) == 2: imgArray[x] = cv2.cvtColor(imgArray[x], cv2.COLOR_GRAY2BGR)
        hor= np.hstack(imgArray)
        hor_con= np.concatenate(imgArray)
        ver = hor
    if len(lables) != 0:
        eachImgWidth= int(ver.shape[1] / cols)
        eachImgHeight = int(ver.shape[0] / rows)
        print(eachImgHeight)
        for d in range(0, rows):
            for c in range (0,cols):
                cv2.rectangle(ver,(c*eachImgWidth,eachImgHeight*d),(c*eachImgWidth+len(lables[d][c])*13+27,30+eachImgHeight*d),(255,255,255),cv2.FILLED)
                cv2.putText(ver,lables[d][c],(eachImgWidth*c+10,eachImgHeight*d+20),cv2.FONT_HERSHEY_DUPLEX,0.7,(255,0,255),2)
    return ver

def getContours(img,imgContour):
    contours, hierarchy = cv2.findContours(img, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
    for cnt in contours:
        area = cv2.contourArea(cnt)
        areaMin = cv2.getTrackbarPos("Area", "Sliders")
        if area > areaMin:
            cv2.drawContours(imgContour, cnt, -1, (255, 0, 255), 3)
            peri = cv2.arcLength(cnt, True)
            approx = cv2.approxPolyDP(cnt, 0.02 * peri, True)
            print(len(approx))
            if approx.any() < 4:
                x , y , w, h = cv2.boundingRect(approx)
                cv2.rectangle(imgContour, (x , y ), (x + w , y + h ), (0, 0, 255), 5)

                cv2.putText(imgContour, "Points: " + str(len(approx)), (x + w + 20, y + 20), cv2.FONT_HERSHEY_DUPLEX, .5,
                        (0, 255, 0), 1)
                cv2.putText(imgContour, "Area: " + str(int(area)), (x + w + 20, y + 45), cv2.FONT_HERSHEY_DUPLEX, 0.5,
                        (0, 255, 0), 1)




 # Create sliders for adjustment 
cv2.namedWindow('Sliders')
 # Setting windowsize for the Sliders
cv2.resizeWindow('Sliders', 840, 240)
 # Creating the sliders itself
cv2.createTrackbar('Blur', 'Sliders', 0, 100, nothing)
cv2.createTrackbar('Max','Sliders',255,255,nothing)
cv2.createTrackbar('Min','Sliders',87,255,nothing)
cv2.createTrackbar('Area', 'Sliders', 1947, 10000, nothing)
cv2.createTrackbar('Min Threshold', 'Sliders', 153, 255, nothing)
cv2.createTrackbar('Max Threshold', 'Sliders', 255, 255, nothing)



#cv2.createTrackbar('Maximum','Max',0,255,100)

# While loop for the main program when the video is playing.
while (cap.isOpened()):
    success, img0 = cap.read()

    img = cv2.resize(img0, (640, 480))
    #img = cv2.bitwise_not(img01)

 
    imgContour = img.copy()
    
    A = cv2.getTrackbarPos('Min','Sliders') #Creating variables for the value's of the sliders. (Must be in while loop to reassess value)
    B = cv2.getTrackbarPos('Max','Sliders')
    C = cv2.getTrackbarPos('Blur','Sliders')
    D = cv2.getTrackbarPos('Min Threshold', 'Sliders')
    E = cv2.getTrackbarPos('Max Threshold', 'Sliders')
    
    imgGray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY) # Turns picture gray
    #retval, imgGray1 = cv2.threshold(imgGray,D,E,cv2.THRESH_BINARY+cv2.THRESH_OTSU)
    
   
    #imgBlur = cv2.GaussianBlur(img,(3,3),C) # Adds blurr to picture
    
    

    imgBl = cv2.Canny(imgGray,A,B) # Transforms picture to black and white outline
    
  
    
    
    imgBlDil  = cv2.dilate(imgBl, (27,27), iterations = 7 ) 
    
    
    
   
    getContours(imgBlDil,imgContour)
    StackedImages = stackImages(([img,imgGray],
                                 [imgBlDil,imgContour]),(0.1))

    if success:
        cv2.imshow("Staked Images", StackedImages)
    else:
       print('Video ended')
       cv2.destroyAllWindows()
       break


    
# When the q key is pressed, the progam stops and the window closes.    
    if cv2.waitKey(1) & 0xFF == ord('q'):
        cv2.destroyAllWindows()
        break

Zo moet je code copy pasten:
```
Code
```

Code

https://www.pyimagesearch.com/2018/07/30/opencv-object-tracking/ Zou dit kunnen helpen?

En standaard een deel wegcroppen?
image