forked from jygan/Advanced-Lane-Detection
-
Notifications
You must be signed in to change notification settings - Fork 0
/
perspective_regionofint_main.py
37 lines (33 loc) · 1.5 KB
/
perspective_regionofint_main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import numpy as np
import cv2
#perspective transform on undistorted images
def perspective_transform(img):
imshape = img.shape
#print (imshape)
vertices = np.array([[(.55*imshape[1], 0.63*imshape[0]), (imshape[1],imshape[0]),
(0,imshape[0]),(.45*imshape[1], 0.63*imshape[0])]], dtype=np.float32)
#print (vertices)
src= np.float32(vertices)
dst = np.float32([[0.75*img.shape[1],0],[0.75*img.shape[1],img.shape[0]],
[0.25*img.shape[1],img.shape[0]],[0.25*img.shape[1],0]])
#print (dst)
M = cv2.getPerspectiveTransform(src, dst)
Minv = cv2.getPerspectiveTransform(dst, src)
img_size = (imshape[1], imshape[0])
perspective_img = cv2.warpPerspective(img, M, img_size, flags = cv2.INTER_LINEAR)
return perspective_img, Minv
#region of interest
def region_of_interest(img, vertices):
#defining a blank mask to start with
mask = np.zeros_like(img, dtype=np.uint8)
#defining a 3 channel or 1 channel color to fill the mask with depending on the input image
if len(img.shape) > 2:
channel_count = img.shape[2] # i.e. 3 or 4 depending on your image
ignore_mask_color = (255,) * channel_count
else:
ignore_mask_color = 255
#filling pixels inside the polygon defined by "vertices" with the fill color
cv2.fillPoly(mask, vertices, ignore_mask_color)
#returning the image only where mask pixels are nonzero
masked_image = cv2.bitwise_and(img, mask)
return masked_image