In [2]:
import matplotlib.image as mpimg
import matplotlib.pyplot as plt
# Uncomment the next line for use in a Jupyter notebook
# This enables the interactive matplotlib window
%matplotlib notebook
image = mpimg.imread('example_grid1.jpg')
rock_image = mpimg.imread('example_rock2.jpg')
In [3]:
import cv2
import numpy as np
image = mpimg.imread('example_grid1.jpg')
def perspect_transform(img, src, dst):
# Get transform matrix using cv2.getPerspectivTransform()
M = cv2.getPerspectiveTransform(src, dst)
# Warp image using cv2.warpPerspective()
# keep same size as input image
warped = cv2.warpPerspective(img, M, (img.shape[1], img.shape[0]))
# Return the result
return warped
def color_thresh(img, rgb_thresh=(0, 0, 0)):
###### TODO:
# Create an empty array the same size in x and y as the image
# but just a single channel
color_select = np.zeros_like(img[:,:,0])
# Lets get the mask for each color
red = img[:,:,0] > rgb_thresh[0]
green = img[:,:,1] > rgb_thresh[1]
blue = img[:,:,2] > rgb_thresh[2]
# Lets applu the masks and assign 1 where threshold has exceeded
color_select[red] = 1
color_select[green] = 1
color_select[blue] = 1
# Apply the thresholds for RGB and assign 1's
# where threshold was exceeded
# Return the single-channel binary image
return color_select
# Define source and destination points
source = np.float32([[ 119,95 ], [119 , 140], [200 ,95 ], [303 , 140]])
destination = np.float32([[160 ,140 ], [160 ,150 ], [170 , 140], [170 ,150 ]])
warped = perspect_transform(image, source, destination)
#warped = perspect_transform(image)
colorsel = color_thresh(warped, rgb_thresh=(160, 160, 160))
#plt.imshow(warped)
plt.imshow(colorsel, cmap='gray')
plt.show()
ypos, xpos = colorsel.nonzero()
plt.plot(xpos, ypos, '.')
plt.xlim(0, 320)
plt.ylim(0, 160)
plt.show()
In [ ]: