Difference between revisions of "Linetracking with opencv"

From ESE205 Wiki
Jump to: navigation, search
Line 6: Line 6:
== Process ==
== Process ==
If you are using Webcam, you can follow this [https://www.hackster.io/Rjuarez7/line-tracking-with-raspberry-pi-3-python2-and-open-cv-9a9327/ link] to realize line detection. The code is given below:
If you are using Webcam, you can follow this [https://www.hackster.io/Rjuarez7/line-tracking-with-raspberry-pi-3-python2-and-open-cv-9a9327/ link] to realize line detection. The code is given below:  
# -*- coding: utf-8 -*-
# -*- coding: utf-8 -*-
import sys
import sys

Revision as of 13:36, 3 December 2018


This Tutorial will show how to make a line detection and find a center of line for a line following pi car.


You will need a Raspberry pi 3 with python installed and Pi camera (Webcam works too). you also need to install OpenCV onto your Raspberry Pi. We can follow instructions to install OpenCV. If it is the first time to use pi camera, to be able to access the Raspberry pi camera with OpenCV and Python, we recommend to look at this instruction. For interfacing with the Raspberry Pi camera module using Python, the basic idea is to install picamera module with NumPy array support since OpenCV represents images as NumPy arrays when using Python bindings.


If you are using Webcam, you can follow this link to realize line detection. The code is given below:

  1. -*- coding: utf-8 -*-

import sys import time import cv2 import numpy as np import os

Kernel_size=15 low_threshold=40 high_threshold=120

rho=10 threshold=15 theta=np.pi/180 minLineLength=10 maxLineGap=1

  1. Initialize camera

video_capture = cv2.VideoCapture(0)

while True:

   ret, frame = video_capture.read()
   #Convert to Grayscale
   gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
   #Blur image to reduce noise. if Kernel_size is bigger the image will be more blurry
   blurred = cv2.GaussianBlur(gray, (Kernel_size, Kernel_size), 0)
   #Perform canny edge-detection.
   #If a pixel gradient is higher than high_threshold is considered as an edge.
   #if a pixel gradient is lower than low_threshold is is rejected , it is not an edge.
   #Bigger high_threshold values will provoque to find less edges.
   #Canny recommended ratio upper:lower  between 2:1 or 3:1
   edged = cv2.Canny(blurred, low_threshold, high_threshold)
   #Perform hough lines probalistic transform
   lines = cv2.HoughLinesP(edged,rho,theta,threshold,minLineLength,maxLineGap)
   #Draw cicrcles in the center of the picture
   #With this for loops only a dots matrix is painted on the picture
   #for y in range(0,480,20):
           #for x in range(0,640,20):
   #With this for loops a grid is painted on the picture
   for y in range(0,480,40):
           for x in range(0,640,40):
   #Draw lines on input image
   if(lines != None):
       for x1,y1,x2,y2 in lines[0]:
   cv2.imshow("line detect test", frame)
   if cv2.waitKey(1) & 0xFF == ord('q'):
  1. When everything is done, release the capture

video_capture.release() cv2.destroyAllWindows()


Pi Car Discovery Group

Group Link

group project page

Weekly logs