Advertisement
acclivity

pyFollowWhiteLine

Feb 2nd, 2022 (edited)
916
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.74 KB | None | 0 0
  1. # Script to find a white line, and draw a red blob that follows the line
  2. # Mike Kerry Jan 2022
  3. import cv2
  4. import numpy as np
  5.  
  6. SPOTSIZE = 8
  7. BLOCKSIZE = 5
  8.  
  9. def get_block_avg(y, x, dim):
  10.     block = matrix[y: y + dim, x: x + dim]      # Extract a block of integers from matrix
  11.     return block.sum() // (dim * dim)           # Find the average whiteness of the block
  12.  
  13. def draw_red(y, x):
  14.     # Draw a red spot of the desired size. y, x is the top left corner.
  15.     for dy in range(0, SPOTSIZE):
  16.         for dx in range(0, SPOTSIZE):
  17.             img[y + dy][x + dx] = (0, 0, 255)       # make the image pixels red
  18.  
  19.     # block the return path by setting already visited matrix positions negative
  20.     for dy in range(0, BLOCKSIZE):
  21.         for dx in range(0, BLOCKSIZE):
  22.             matrix[y + dy][x + dx] = -666       # make that point blacker than black!
  23.  
  24. def find_start():
  25.     # Scan down and right looking for a block of pixels that are basically white
  26.     for y in range(0, height - BLOCKSIZE, 3):
  27.         for x in range(0, width - BLOCKSIZE, 3):
  28.             v = get_block_avg(y, x, 5)
  29.             if v > 650:             # 650 was found empirically to be a good value to suggest white
  30.                 return y, x         # Return the coords of the top left of a white block
  31.     return(-1, -1)
  32.  
  33. orig = cv2.imread('pyFollowLineMed.jpg')
  34. img = orig.copy()
  35. dims = img.shape
  36. height, width = dims[0], dims[1]
  37. matrix = img.sum(axis=2, dtype=np.int16)      # Reduce number of axes (?) by summing BGR
  38.  
  39. ypos, xpos = find_start()           # Get the coordinates of the first block of white pixels found
  40.  
  41. # Try and follow the line without retracing
  42. while True:
  43.     cv2.imshow("img", img)
  44.     cv2.waitKey(30)             # pause for effect
  45.     # scan around the current location, in 8 directions, looking for the whitest adjacent block of pixels
  46.     best, besty, bestx = 0, 0, 0
  47.     for incy in range(-BLOCKSIZE, BLOCKSIZE+1, BLOCKSIZE):                # y becomes -5, 0, 5
  48.         for incx in range(-BLOCKSIZE, BLOCKSIZE+1, BLOCKSIZE):            # x becomes -5, 0, 5
  49.             y2 = ypos + incy
  50.             x2 = xpos + incx
  51.             v = get_block_avg(y2, x2, BLOCKSIZE)
  52.             if v > best:
  53.                 best, besty, bestx = v, y2, x2      # save the coords at top left of white block
  54.  
  55.     ypos, xpos = besty, bestx   # top left of the whitest adjacent block of pixels
  56.     img = orig.copy()           # get the original image again (without any red spots)
  57.     draw_red(ypos, xpos)        # draw the new red spot and block the return path
  58.     if best < 670:          # 670 was found empirically to be a good value to end the loop
  59.         break               # No adjacent white block found. We have reached the end of the line
  60.  
  61. cv2.waitKey(0)
  62.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement