def _final_frame(self, im, fill, lcurv, rcurv, offset, font=cv.FONT_HERSHEY_DUPLEX, scale_font=1, color_font=(255, 0, 0), show=False): fill = prsp.warp(fill, inverse=True) out = im.copy() out = cv.addWeighted(out, 0.7, fill, 0.5, 0) xtxt = 50 lcurv_text = 'Left curvature={0:.01f}m'.format(lcurv) rcurv_text = 'Right curvature={0:.01f}m'.format(rcurv) offset_text = 'Offset={0:.02f}m'.format(offset) out = cv.putText(out, lcurv_text, (xtxt, 30), font, scale_font, color_font) out = cv.putText(out, rcurv_text, (xtxt, 60), font, scale_font, color_font) out = cv.putText(out, offset_text, (xtxt, 90), font, scale_font, color_font) if show == True: show_images(im, out, 'origin', 'lanes', 'Lanes detected') return out
def fill_lanes(im, llane, rlane, fill_color=(0, 255, 255), show=False): y = llane.y lp = np.array([np.transpose(np.vstack([llane.line, y]))]) rp = np.array([np.flipud(np.transpose(np.vstack([rlane.line, y])))]) points = np.hstack([lp, rp]) fill = np.zeros_like(im) cv.fillPoly(fill, np.int32([points]), fill_color) if show == True: cpy = im.copy() cpy = cv.addWeighted(cpy, 1, fill, 0.2, 0) show_images(im, cpy, 'origin', 'filled warp', 'Fill lanes') return fill
def hls_channel(im, channel='s', thresh=(0, 255), show=False): i, title = 2, 'saturation' if channel == 'h': i, title = 0, 'hue' elif channel == 'l': i, title = 1, 'level' chan = cv.cvtColor(im, cv.COLOR_RGB2HLS)[:, :, i] chan[(chan < thresh[0]) | (chan > thresh[1])] = 0 if show == True: title = '{0}, thresh={1}'.format(title, thresh) show_images(im, chan, 'original', title, 'HLS', cmap2='gray') return chan
def rgb_channel(im, channel='s', thresh=(0, 255), show=False): i, title = 0, 'red' if channel == 'g': i, title = 1, 'green' elif channel == 'b': i, title = 2, 'blue' chan = np.copy(im[:, :, i]) chan[(chan < thresh[0]) | (chan > thresh[1])] = 0 if show == True: title = '{0}, thresh={1}'.format(title, thresh) show_images(im, chan, 'original', title, 'RGB', cmap2='gray') return chan
def fitlanes(im, show=False): nonz = np.array(im.nonzero()) (pyl, pxl), (pyr, pxr) = nonzero_points(nonz) fitl = np.polyfit(pyl, pxl, 2) fitr = np.polyfit(pyr, pxr, 2) if show == True: y_axe = np.linspace(0, im.shape[0] - 1, im.shape[0]) line_l = fitl[0] * y_axe**2 + fitl[1] * y_axe + fitl[2] line_r = fitr[0] * y_axe**2 + fitr[1] * y_axe + fitr[2] ps_l = np.array([np.transpose(np.vstack([line_l, y_axe]))]) ps_r = np.array([np.flipud(np.transpose(np.vstack([line_r, y_axe])))]) points = np.hstack([ps_l, ps_r]) draw = np.zeros_like(im).astype(np.uint8) cv.fillPoly(draw, np.int32([points]), (255, 255, 0)) mix = cv.addWeighted(np.uint8(im), 1, draw, 0.1, 0) show_images(im, mix, 'origin', 'mix', 'Fit lines') return fitl, fitr
def abs(im, orient='x', sobel_kernel=3, thresh=(20, 100), show=False): gray = cv.cvtColor(im, cv.COLOR_RGB2GRAY) x, y = (1, 0) if orient == "x" else (0, 1) sobel = cv.Sobel(gray, cv.CV_64F, x, y, ksize=sobel_kernel) abs = np.absolute(sobel) scaled = np.uint8(255 * abs / np.max(abs)) tmin, tmax = thresh binary = np.zeros_like(scaled) binary[(scaled >= tmin) & (scaled <= tmax)] = 1 if show == True: show_images(gray, binary, 'origin', 'sobel', 'sobel thresh', cmap1='gray', cmap2='gray') return binary
def magnitude(im, sobel_kernel=3, thresh=(30, 100), show=False): gray = cv.cvtColor(im, cv.COLOR_RGB2GRAY) sobelx = cv.Sobel(gray, cv.CV_64F, dx=1, dy=0, ksize=sobel_kernel) sobely = cv.Sobel(gray, cv.CV_64F, dx=0, dy=1, ksize=sobel_kernel) abs = np.sqrt(sobelx**2 + sobely**2) scaled = np.uint8(255 * abs / np.max(abs)) binary = np.zeros_like(scaled) tmin, tmax = thresh binary[(scaled >= tmin) & (scaled <= tmax)] = 1 if show == True: show_images(gray, binary, 'origin', 'magnitude', 'Magnitude Thresholding', cmap1='gray', cmap2='gray') return binary
def apply(im, show=False): s = ColorThresholder.hls_channel(im, channel='s', thresh=(100, 255)) l = ColorThresholder.hls_channel(im, channel='l', thresh=(200, 255)) g = ColorThresholder.rgb_channel(im, channel='g', thresh=(200, 255)) m = SobelMask.magnitude(im, thresh=(15, 255)) #m = np.zeros(s.shape) since = im.shape[0] - (im.shape[0] // 2) m[since:, :] = 0 #l = np.zeros(s.shape) binary = np.zeros(s.shape) binary[(s > 0) | (l > 0) | (g > 0) | (m > 0)] = 1 if show == True: combined = (g == m).astype(np.uint8) binary_color = np.dstack([s, l, combined]) binary_color[binary_color == 1] = 100 show_images(im, binary_color, 'original', 'binary colored', 'Masking image') show_images(im, binary, 'original', 'binary b&w', 'Masking image') return binary
def direction(im, sobel_kernel=3, thresh=(.7, 1.3), show=False): gray = cv.cvtColor(im, cv.COLOR_RGB2GRAY) sobx = cv.Sobel(gray, cv.CV_64F, 1, 0, ksize=sobel_kernel) soby = cv.Sobel(gray, cv.CV_64F, 0, 1, ksize=sobel_kernel) abs_sobx = np.absolute(sobx) abs_soby = np.absolute(soby) arctan = np.arctan2(abs_soby, abs_sobx) tmin, tmax = thresh binary = np.zeros_like(arctan) binary[(arctan >= tmin) & (arctan <= tmax)] = 1 if show == True: show_images(gray, binary, 'origin', 'direction', 'Direction sobel', cmap1='gray', cmap2='gray') return binary
def _find_lane_points(self, im, num_strips=10, radius=80, max_radius=100, peak_diff=60, show=False): strip_height = im.shape[0] // num_strips heights = [None] * num_strips strips = [None] * num_strips for i in range(num_strips): s, e = i * strip_height, (i + 1) * strip_height heights[-i - 1] = (s, e) strips[-i - 1] = im[s:e, :] ly, lx = [[]] * num_strips, [[]] * num_strips ry, rx = [[]] * num_strips, [[]] * num_strips nonzeros = np.array(im.nonzero()) peaks = [None] * num_strips if show == True: cpy = im.copy() lpeak, rpeak = 0, 0 for i in range(num_strips): if i == 0: lpeak, rpeak = self._find_peaks(im, mode='argmax') lrad, rrad = max_radius, max_radius else: lpeak_prev, rpeak_prev = peaks[i - 1] lpeak, rpeak = self._find_peaks(strips[i], mode='argmax') if np.abs(lpeak_prev - lpeak) > peak_diff: lpeak = lpeak_prev lrad = max_radius else: lrad = radius if np.abs(rpeak_prev - rpeak) > peak_diff: rpeak = rpeak_prev rrad = max_radius else: rrad = radius lbox = ((heights[i][0], lpeak - lrad), (heights[i][1], lpeak + lrad)) rbox = ((heights[i][0], rpeak - rrad), (heights[i][1], rpeak + rrad)) peaks[i] = (lpeak, rpeak) if show == True: top, bot = lbox[0], lbox[1] cpy = cv.rectangle(cpy, (top[1], top[0]), (bot[1], bot[0]), (i + 1, 0, 0), 2) top, bot = rbox[0], rbox[1] cpy = cv.rectangle(cpy, (top[1], top[0]), (bot[1], bot[0]), (i + 1, 0, 0), 2) (ly[i], lx[i]), (ry[i], rx[i]) = self._nonzero_points(nonzeros, left=lbox, right=rbox) left = (np.concatenate(ly), np.concatenate(lx)) right = (np.concatenate(ry), np.concatenate(rx)) if show == True: filtered = np.zeros(im.shape[:2]) filtered[left[0], left[1]] = 1 filtered[right[0], right[1]] = 1 show_images(filtered, cpy, 'filtered', 'regions', 'Found points', cmap1='gray') return left, right