def plot_value_function(V, title="Value Function"): """ Plots the value function as a surface plot. """ min_x = min(k[0] for k in V.keys()) max_x = max(k[0] for k in V.keys()) min_y = min(k[1] for k in V.keys()) max_y = max(k[1] for k in V.keys()) x_range = np.arange(min_x, max_x + 1) y_range = np.arange(min_y, max_y + 1) X, Y = np.meshgrid(x_range, y_range) # Find value for all (x, y) coordinates Z_noace = np.apply_along_axis(lambda _: V[(_[0], _[1], False)], 2, np.dstack([X, Y])) Z_ace = np.apply_along_axis(lambda _: V[(_[0], _[1], True)], 2, np.dstack([X, Y])) def plot_surface(X, Y, Z, title): fig = plt.figure(figsize=(20, 10)) ax = fig.add_subplot(111, projection='3d') surf = ax.plot_surface(X, Y, Z, rstride=1, cstride=1, cmap=matplotlib.cm.coolwarm, vmin=-1.0, vmax=1.0) ax.set_xlabel('Player Sum') ax.set_ylabel('Dealer Showing') ax.set_zlabel('Value') ax.set_title(title) ax.view_init(ax.elev, -120) fig.colorbar(surf) plt.show() plot_surface(X, Y, Z_noace, "{} (No Usable Ace)".format(title)) plot_surface(X, Y, Z_ace, "{} (Usable Ace)".format(title))
def _array_from_bitmap(bitmap): """Convert a FreeImage bitmap pointer to a numpy array """ dtype, shape = FI_TYPES.get_type_and_shape(bitmap) if type(dtype) == str and dtype == "bit": bitmap8 = _FI.FreeImage_ConvertToGreyscale(bitmap) try: return _array_from_bitmap(bitmap8).astype(np.bool) finally: _FI.FreeImage_Unload(bitmap8) array = _wrap_bitmap_bits_in_array(bitmap, shape, dtype) # swizzle the color components and flip the scanlines to go from # FreeImage's BGR[A] and upside-down internal memory format to something # more normal def n(arr): return arr[..., ::-1].T if len(shape) == 3 and _FI.FreeImage_IsLittleEndian() and dtype.type == np.uint8: b = n(array[0]) g = n(array[1]) r = n(array[2]) if shape[0] == 3: return np.dstack((r, g, b)) elif shape[0] == 4: a = n(array[3]) return np.dstack((r, g, b, a)) else: raise ValueError("mahotas.freeimage: cannot handle images of" " this shape (%s)" % shape) # We need to copy because array does *not* own its memory # after bitmap is freed. return n(array).copy()
def load_data(dirname="cifar-10-batches-py", one_hot=False): tarpath = maybe_download("cifar-10-python.tar.gz", "http://www.cs.toronto.edu/~kriz/", dirname) X_train = [] Y_train = [] for i in range(1, 6): fpath = os.path.join(dirname, 'data_batch_' + str(i)) data, labels = load_batch(fpath) if i == 1: X_train = data Y_train = labels else: X_train = np.concatenate([X_train, data], axis=0) Y_train = np.concatenate([Y_train, labels], axis=0) fpath = os.path.join(dirname, 'test_batch') X_test, Y_test = load_batch(fpath) X_train = np.dstack((X_train[:, :1024], X_train[:, 1024:2048], X_train[:, 2048:])) / 255. X_train = np.reshape(X_train, [-1, 32, 32, 3]) X_test = np.dstack((X_test[:, :1024], X_test[:, 1024:2048], X_test[:, 2048:])) / 255. X_test = np.reshape(X_test, [-1, 32, 32, 3]) if one_hot: Y_train = to_categorical(Y_train, 10) Y_test = to_categorical(Y_test, 10) return (X_train, Y_train), (X_test, Y_test)
def compute_density(lon, lat, xx, yy, use_hsa): min_lon = np.min(xx) max_lon = np.max(xx) min_lat = np.min(yy) max_lat = np.max(yy) selected = filter_array(lon, lat, min_lon, min_lat, max_lon, max_lat) lon = lon[selected] lat = lat[selected] samples = np.dstack([lon, lat]) assert samples.shape[0] == 1 samples = samples.reshape(samples.shape[1:]) support = np.squeeze(np.dstack([xx, yy])) bwlist = np.array([cpu_ref.approx_bandwidth(support[:, k]) for k in range(support.shape[1])]) pdf = np.zeros(support.shape[0], dtype=np.float64) if samples.size: print(samples.shape, samples.dtype) start_time = timer() if use_hsa: print("HSA".center(80, '-')) hsa_imp.hsa_multi_kde(support, samples, bwlist, pdf) else: print("CPU".center(80, '-')) cpu_ref.multi_kde_seq(support, samples, bwlist, pdf) end_time = timer() print("duration", "{0:0.2f} seconds".format(end_time - start_time)) return pdf, samples.size
def interp(pic,flow): ys=np.arange(pic.shape[0]*pic.shape[1])/pic.shape[1] ud=(flow[:,:,0].reshape(-1)+ys)%pic.shape[0] xs=np.arange(pic.shape[0]*pic.shape[1])%pic.shape[1] lr=(flow[:,:,1].reshape(-1)+xs)%pic.shape[1] u=np.int32(np.floor(ud)) d=np.int32(np.ceil(ud))%pic.shape[0] udiffs=ud-u udiffs=np.dstack((udiffs,udiffs,udiffs)) l=np.int32(np.floor(lr)) r=np.int32(np.ceil(lr))%pic.shape[1] ldiffs=lr-l ldiffs=np.dstack((ldiffs,ldiffs,ldiffs)) ul=pic[u,l,:] ur=pic[u,r,:] dl=pic[d,l,:] dr=pic[d,r,:] udl=ul*(1-udiffs)+dl*udiffs udr=ur*(1-udiffs)+dr*udiffs ans=np.zeros(pic.shape) ans[ys,xs,:]=udl*(1-ldiffs)+udr*ldiffs return ans
def find_zero_crossings(laplacian_of_img): result = np.zeros((laplacian_of_img.shape[0], laplacian_of_img.shape[1]), dtype=np.int) # Array indicating if values are positive or negative image_array_signs = np.sign(laplacian_of_img) # Difference along xaxis xdiff = np.diff(image_array_signs, axis=1) xzero_crossings = np.where(xdiff) # Output of where gives two arrays...combine the result to obtain [x,y] coordinate pairs xzero_crossings = np.dstack((xzero_crossings[0], xzero_crossings[1]))[0] #difference along yaxis ydiff = np.diff(image_array_signs, axis=0) yzero_crossings = np.where(ydiff) # Output of where gives two arrays...combine the result to obtain [x,y] coordinate pairs yzero_crossings = np.dstack((yzero_crossings[0], yzero_crossings[1]))[0] xzero_crossings_rows = xzero_crossings.view([('', xzero_crossings.dtype)] * xzero_crossings.shape[1]) yzero_crossings_rows = yzero_crossings.view([('', yzero_crossings.dtype)] * yzero_crossings.shape[1]) # Obtain the tuples of xzero_crossings which are not found in yzero_crossings diff = np.setdiff1d(xzero_crossings_rows, yzero_crossings_rows).view(xzero_crossings_rows.dtype).reshape(-1, xzero_crossings_rows.shape[1]) # The format of diff cannot be used in append due to different "shape" of yzero_crossings and diff. diff_formatted = [] for index in range(0, len(diff)): diff_formatted.append(diff[index][0]) diff_a, diff_b = zip(*diff_formatted) difference_result = np.dstack((diff_a, diff_b))[0] # Append the zero crossings inside yzero_crossings with the remaining x,y coordinates zero_crossings = np.append(yzero_crossings, difference_result, axis=0) for tuple in zero_crossings: result[tuple[0], tuple[1]] = 120 return result
def visualize_depth_image(data): data[data == 0.0] = np.nan maxdepth = np.nanmax(data) mindepth = np.nanmin(data) data = data.copy() data -= mindepth data /= (maxdepth - mindepth) gray = np.zeros(list(data.shape) + [3], dtype=data.dtype) data = (1.0 - data) gray[..., :3] = np.dstack((data, data, data)) # use a greenish color to visualize missing depth gray[np.isnan(data), :] = (97, 160, 123) gray[np.isnan(data), :] /= 255 gray = exposure.equalize_hist(gray) # set alpha channel gray = np.dstack((gray, np.ones(data.shape[:2]))) gray[np.isnan(data), -1] = 0.5 return gray * 255
def testComponentSeparation(self): A = generate_covsig([[10,5,2],[5,10,2],[2,2,10]], 500) B = generate_covsig([[10,2,2],[2,10,5],[2,5,10]], 500) X = np.dstack([A,B]) W, V = csp(X,[1,2]) C1a = np.cov(X[:,:,0].dot(W).T) C2a = np.cov(X[:,:,1].dot(W).T) Y = np.dstack([B,A]) W, V = csp(Y,[1,2]) C1b = np.cov(Y[:,:,0].dot(W).T) C2b = np.cov(Y[:,:,1].dot(W).T) # check symmetric case self.assertTrue(np.allclose(C1a.diagonal(), C2a.diagonal()[::-1])) self.assertTrue(np.allclose(C1b.diagonal(), C2b.diagonal()[::-1])) # swapping class labels (or in this case, trials) should not change the result self.assertTrue(np.allclose(C1a, C1b)) self.assertTrue(np.allclose(C2a, C2b)) # variance of first component should be greatest for class 1 self.assertTrue(C1a[0,0] > C2a[0,0]) # variance of last component should be greatest for class 1 self.assertTrue(C1a[2,2] < C2a[2,2]) # variance of central component should be equal for both classes self.assertTrue(np.allclose(C1a[1,1], C2a[1,1]))
def getParallelPatches(self, parallels, **kwargs): """Get parallel lines in matplotlib format. Parallel lines in conics are straight, appropriate matplotlib.patches will be returned. Args: meridians: list of rectascensions **kwargs: matplotlib.collection.LineCollection parameters Returns: matplotlib.LineCollection """ # remove duplicates parallels_ = np.unique(parallels % 360) # the outer boundaries need to be duplicated because the same # parallel appear on the left and the right side of the map if self.ra_0 < 180: outer = self.ra_0 - 180 else: outer = self.ra_0 + 180 parallels_ = np.array(list(parallels_) + [outer]) from matplotlib.collections import LineCollection top = self.__call__(parallels_, 90) bottom = self.__call__(parallels_, -90) x_ = np.dstack((top[0], bottom[0]))[0] y_ = np.dstack((top[1], bottom[1]))[0] return LineCollection(np.dstack((x_, y_)), color='k', **kwargs)
def testFlipWhenProbIsOne(self): numpy_image = np.dstack([[[5., 6.], [9., 0.]], [[4., 3.], [3., 5.]]]) dim0_flipped = np.dstack([[[9., 0.], [5., 6.]], [[3., 5.], [4., 3.]]]) dim1_flipped = np.dstack([[[6., 5.], [0., 9.]], [[3., 4.], [5., 3.]]]) dim2_flipped = np.dstack([[[4., 3.], [3., 5.]], [[5., 6.], [9., 0.]]]) image = tf.convert_to_tensor(numpy_image) with self.test_session(): actual, is_flipped = preprocess_utils.flip_dim([image], prob=1, dim=0) self.assertAllEqual(dim0_flipped, actual.eval()) self.assertAllEqual(True, is_flipped.eval()) actual, is_flipped = preprocess_utils.flip_dim([image], prob=1, dim=1) self.assertAllEqual(dim1_flipped, actual.eval()) self.assertAllEqual(True, is_flipped.eval()) actual, is_flipped = preprocess_utils.flip_dim([image], prob=1, dim=2) self.assertAllEqual(dim2_flipped, actual.eval()) self.assertAllEqual(True, is_flipped.eval())
def _array_from_bitmap(bitmap): """Convert a FreeImage bitmap pointer to a numpy array. """ dtype, shape = FI_TYPES.get_type_and_shape(bitmap) array = _wrap_bitmap_bits_in_array(bitmap, shape, dtype) # swizzle the color components and flip the scanlines to go from # FreeImage's BGR[A] and upside-down internal memory format to something # more normal def n(arr): return arr[..., ::-1].T if len(shape) == 3 and _FI.FreeImage_IsLittleEndian() and \ dtype.type == numpy.uint8: b = n(array[0]) g = n(array[1]) r = n(array[2]) if shape[0] == 3: return numpy.dstack((r, g, b)) elif shape[0] == 4: a = n(array[3]) return numpy.dstack((r, g, b, a)) else: raise ValueError('Cannot handle images of shape %s' % shape) # We need to copy because array does *not* own its memory # after bitmap is freed. return n(array).copy()
def upsample(arr, n): z = numpy.zeros(len(arr)) # upsample with values for i in range(int(int(n-1)/2)): #TODO arr = numpy.dstack((z,arr)) for i in range(int(int( n )/2)):#TODO arr = numpy.dstack((arr,z)) return arr.reshape((1,-1))[0]
def testReturnPaddedImageWithNonZeroPadValue(self): for dtype in [np.int32, np.int64, np.float32, np.float64]: image = np.dstack([[[5, 6], [9, 0]], [[4, 3], [3, 5]]]).astype(dtype) expected_image = np.dstack([[[255, 255, 255, 255, 255], [255, 255, 255, 255, 255], [255, 5, 6, 255, 255], [255, 9, 0, 255, 255], [255, 255, 255, 255, 255]], [[255, 255, 255, 255, 255], [255, 255, 255, 255, 255], [255, 4, 3, 255, 255], [255, 3, 5, 255, 255], [255, 255, 255, 255, 255]]]).astype(dtype) with self.session() as sess: padded_image = preprocess_utils.pad_to_bounding_box( image, 2, 1, 5, 5, 255) padded_image = sess.run(padded_image) self.assertAllClose(padded_image, expected_image) # Add batch size = 1 to image. padded_image = preprocess_utils.pad_to_bounding_box( np.expand_dims(image, 0), 2, 1, 5, 5, 255) padded_image = sess.run(padded_image) self.assertAllClose(padded_image, np.expand_dims(expected_image, 0))
def loadWeather(m): h5f = h5py.File('c:\\tmp\\data.h5','r') #randlist = np.random.randint(0,h5f['weather_data'].shape[0],2920) randlist = np.arange(161686,164606) randlist_usort = list(np.sort(np.array(list(set(randlist))))) weather_points = h5f['weather_points'][:] [coord] = np.dstack(m(weather_points['lon'],weather_points['lat'])) weather_data = h5f['weather_data'][randlist_usort] h5f.close() minx, maxx, miny, maxy = (27686.0,848650.0,56061.0,645608.0) grid_x, grid_y = np.mgrid[minx:maxx:10000, miny:maxy:10000] [vluPoints]=np.dstack(([grid_x.reshape(grid_x.shape[0]*grid_x.shape[1],1)],[grid_y.reshape(grid_y.shape[0]*grid_y.shape[1],1)])) tree = cKDTree(coord) d, inds = tree.query(vluPoints, k = 10) _,indsNN = tree.query(vluPoints, k = 1) w = 1.0 / d**2 su = np.sum(w, axis=1) values_idw = np.empty((weather_data.shape[0],grid_x.shape[0]*grid_x.shape[1]), dtype=[('wsp', np.float32),('wdir', np.float32),('hs', np.float32),('light', np.uint32)]) for d in range(0,values_idw.shape[0]): values_idw[d,:]['wsp'] = weather_data[d,:]['wsp'][indsNN] / 10. values_idw[d,:]['wdir'] = np.deg2rad(weather_data[d,:]['wdir'][indsNN]) values_idw[d,:]['hs'] = (np.sum(w * (weather_data[d,:]['hs'][inds]), axis=1) / su)/10. values_idw[d,:]['light'] = weather_data[d,:]['light'][indsNN] values_idw.shape =((values_idw.shape[0],grid_x.shape[0],grid_x.shape[1])) return values_idw
def find_mask(image): black_range1 = np.array([0,0,0]) im_mask = (cv2.inRange(image, black_range1, black_range1)).astype('bool') im_mask_inv = (1-im_mask).astype('bool') im_mask_inv = np.dstack((im_mask_inv, im_mask_inv, im_mask_inv)) im_mask= np.dstack((im_mask, im_mask, im_mask)) return im_mask_inv, im_mask
def grad_sobel(raster): c0m,c0d = get_channel_gradient_sobel(raster[:,:,0]) c1m,c1d = get_channel_gradient_sobel(raster[:,:,1]) c2m,c2d = get_channel_gradient_sobel(raster[:,:,2]) grad_mag = np.dstack([c0m,c1m,c2m]) grad_dir = np.dstack([c0d,c1d,c2d]) return grad_mag, grad_dir
def compute_maximum_ts_map(ts_map_results): """ Compute maximum TS map across a list of given `TSMapResult` objects. Parameters ---------- ts_map_results : list List of `TSMapResult` objects. Returns ------- TS : `TSMapResult` `TSMapResult` object. """ # Get data ts = np.dstack([result.ts for result in ts_map_results]) niter = np.dstack([result.niter for result in ts_map_results]) amplitude = np.dstack([result.amplitude for result in ts_map_results]) scales = [result.scale for result in ts_map_results] # Set up max arrays ts_max = np.max(ts, axis=2) scale_max = np.zeros(ts.shape[:-1]) niter_max = np.zeros(ts.shape[:-1]) amplitude_max = np.zeros(ts.shape[:-1]) for i, scale in enumerate(scales): index = np.where(ts[:, :, i] == ts_max) scale_max[index] = scale niter_max[index] = niter[:, :, i][index] amplitude_max[index] = amplitude[:, :, i][index] return TSMapResult(ts=ts_max, niter=niter_max, amplitude=amplitude_max, morphology=ts_map_results[0].morphology, scale=scale_max)
def read_MSR_depth_ims(depth_file, resize='VGA'): ''' Extracts depth images and masks from the MSR Daily Activites dataset ---Parameters--- depth_file : filename for set of depth images (.bin file) ''' file_ = open(depth_file, 'rb') ''' Get header info ''' frames = np.fromstring(file_.read(4), dtype=np.int32)[0] cols = np.fromstring(file_.read(4), dtype=np.int32)[0] rows = np.fromstring(file_.read(4), dtype=np.int32)[0] ''' Get depth/mask image data ''' data = file_.read() ''' Depth images and mask images are stored together per row. Thus we need to extract each row of size n_cols+n_rows ''' dt = np.dtype([('depth', np.int32, cols), ('mask', np.uint8, cols)]) ''' raw -> usable images ''' frame_data = np.fromstring(data, dtype=dt) depthIms = frame_data['depth'].astype(np.uint16).reshape([frames, rows, cols]) maskIms = frame_data['mask'].astype(np.uint16).reshape([frames, rows, cols]) if resize == 'VGA': # embed() depthIms = np.dstack([cv2.resize(depthIms[d,:,:], (640,480)) for d in xrange(len(depthIms))]) maskIms = np.dstack([cv2.resize(maskIms[d,:,:], (640,480)) for d in xrange(len(maskIms))]) return depthIms, maskIms
def long_short_rules_1(ohlc_df): """ :param ohlc_df: :return: """ ichimoku_components = components(ohlc_df) mid_prices = (ohlc_df['high'] + ohlc_df['low']) / 2 kumo_top = ichimoku_components[['senkou-span-a', 'senkou-span-b']].max(axis=1) kumo_bottom = ichimoku_components[['senkou-span-a', 'senkou-span-b']].min(axis=1) prices_above_kumo = (ohlc_df['close'].astype(numpy.float64) - kumo_top) >= 0 tenkan_above_kijun = ichimoku_components['tenkan-sen'] >= ichimoku_components['kijun-sen'] chikou_above_lagged_price = (mid_prices.astype(numpy.float64) - ichimoku_components['chikou']).shift(26) >= 0 kumo_ahead_bullish = (ichimoku_components['senkou-span-a'] - ichimoku_components['senkou-span-b']).shift(-26) >= 0 bullish = prices_above_kumo & tenkan_above_kijun & chikou_above_lagged_price & kumo_ahead_bullish prices_below_kumo = (ohlc_df['close'].astype(numpy.float64) - kumo_bottom) < 0 tenkan_below_kijun = ichimoku_components['tenkan-sen'] < ichimoku_components['kijun-sen'] chikou_below_lagged_price = (mid_prices.astype(numpy.float64) - ichimoku_components['chikou']).shift(26) < 0 kumo_ahead_bearish = (ichimoku_components['senkou-span-a'] - ichimoku_components['senkou-span-b']).shift(-26) < 0 bearish = prices_below_kumo & tenkan_below_kijun & chikou_below_lagged_price & kumo_ahead_bearish longs = bullish.map(lambda value: (0, 1)[value]) shorts = bearish.map(lambda value: (0, -1)[value]) long_diffs = longs.diff() short_diffs = shorts.diff() long_diffs_clean = long_diffs[long_diffs != 0].dropna() short_diffs_clean = short_diffs[short_diffs != 0].dropna() long_trades = numpy.dstack([long_diffs_clean[::2].index.values, long_diffs_clean[1::2].index.values]) short_trades = numpy.dstack([short_diffs_clean[::2].index.values, short_diffs_clean[1::2].index.values]) return long_trades[0], short_trades[0]
def test_optimize(): lena = scipy.misc.lena() blurred1 = scipy.ndimage.gaussian_filter(lena, 3.0) blurred2 = scipy.ndimage.gaussian_filter(blurred1, 1.5) fp = af.FocusPoint(0, 0, 10, 10) stack = np.dstack((blurred2, np.dstack((blurred1, lena)))) assert af.optimize(stack, fp, af.cost_sobel) == 2
def sigs2rays(self, sigslist): """ from signatures list to 2D rays Parameters ---------- sigslist : list Returns ------- rays : dict """ rays = {} for sig in sigslist: s = Signature(sig) Yi = s.sig2ray(self.L, self.pTx[:2], self.pRx[:2]) if Yi is not None: #pdb.set_trace() Yi = np.fliplr(Yi) nint = len(sig[0, :]) if str(nint) in rays.keys(): Yi3d = np.vstack((Yi[:, 1:-1], np.zeros((1, nint)))) Yi3d = Yi3d.reshape(3, nint, 1) rays[str(nint)]['pt'] = np.dstack(( rays[str(nint)]['pt'], Yi3d)) rays[str(nint)]['sig'] = np.dstack(( rays[str(nint)]['sig'], sig.reshape(2, nint, 1))) else: rays[str(nint)] = {'pt': np.zeros((3, nint, 1)), 'sig': np.zeros((2, nint, 1))} rays[str(nint)]['pt'][0:2, :, 0] = Yi[:, 1:-1] rays[str(nint)]['sig'][:, :, 0] = sig return rays
def objects_walls_algorithm(cmd, world, k1=4.2, k2=4.4): x, y = np.mgrid[0:world.xdim:.1, 0:world.ydim:.1] # Calculate naive distribution naive_dist = naive_algorithm(cmd, world) naive_vals = naive_dist.pdf(np.dstack((x, y))) # Find Distance to closest object ref_dists = {ref : np.sqrt((x - ref.center[0])**2 + (y - ref.center[1])**2) for ref in world.references} min_ref_dists = np.min(np.dstack(ref_dists[ref] for ref in ref_dists), axis=2) # Difference between distance to closest object and object reference in command ref_distance_diff = ref_dists[cmd.reference] - min_ref_dists ref_distance_vals = expon.pdf(ref_distance_diff, scale=k1) # Find distance to nearest wall min_wall_dists = np.min(np.dstack((x, y, world.xdim - x, world.ydim - y)), axis=2) # Difference between distance to closest wall and object reference in command wall_distance_diff = ref_dists[cmd.reference] - min_wall_dists wall_distance_diff[wall_distance_diff < 0] = 0 wall_distance_vals = expon.pdf(wall_distance_diff, scale=k2) mean_prob = naive_vals*ref_distance_vals*wall_distance_vals loc = np.where(mean_prob == mean_prob.max()) mean = 0.1*np.array([loc[0][0], loc[1][0]]) mv_dist = multivariate_normal(mean, naive_dist.cov) return mv_dist
def load_from_filestore(self,directory,tablename="Untitled",limit=None,dtype=numpy.float32): """ Not intended to be used often - loads individual files from a directory and stores them in a named table in the datastore - unfinished """ hh=None for dirname, dirnames, filenames in os.walk(directory): num=0 # print path to all subdirectories first. for subdirname in dirnames: print os.path.join(dirname, subdirname) # import data and append to an image stack. imgs=[] names=[] for filename in filenames: print os.path.join(dirname, filename) try: imgs.append(numpy.loadtxt(os.path.join(dirname, filename),dtype=dtype)) names.append(os.path.join(dirname, filename)) num+=1 if num>=limit: return numpy.dstack(imgs) #del(imgs[0]) except: print 'failed' if hh==None: hh=self.get_handle("/"+tablename,imgs[0]) try: hh.append(imgs[-1],num,0) except: pass return numpy.dstack(imgs)
def raw_deinterleaver(input_queue, output_queue, plot_queue): ''' Process for deinterleaving raw FFT data and plotting. ''' signal.signal(signal.SIGINT, signal.SIG_IGN) # Ignore keyboard interrupt signal, parent process will handle. time.sleep(1) while 1: LCP = [] RCP = [] interleavedWindow = np.array(input_queue.get()) if interleavedWindow == None: break index = 0 even1 = (interleavedWindow[0::8] + interleavedWindow[1::8]*1j) odd1 = (interleavedWindow[2::8] + interleavedWindow[3::8]*1j) LCP = np.reshape(np.dstack((even1, odd1)), (1,-1)) even2 = (interleavedWindow[4::8] + interleavedWindow[5::8]*1j) odd2 = (interleavedWindow[6::8] + interleavedWindow[7::8]*1j) RCP = np.reshape(np.dstack((even2, odd2)), (1, -1)) #need to figure out here how to write out to the plotting function. output_queue.put((LCP,RCP)) plot_queue.put((LCP,RCP)) print 'raw_deinterleaver found poison pill' output_queue.put(None)
def findedges(inputtiles, parsenames): tiles = sutils.tile_parser(inputtiles, parsenames) xmin, xmax, ymin, ymax = sutils.get_range(tiles) zoom = sutils.get_zoom(tiles) # zoom = inputtiles[0, -1] # make an array of shape (xrange + 3, yrange + 3) burn = sutils.burnXYZs(tiles, xmin, xmax, ymin, ymax) # Create the indixes for rolling idxs = sutils.get_idx() # Using the indices to roll + stack the array, find the minimum along the rolled / stacked axis xys_edge = (np.min(np.dstack(( np.roll(np.roll(burn, i[0], 0), i[1], 1) for i in idxs )), axis=2) - burn) # Set missed non-tiles to False xys_edge[burn == False] = False # Recreate the tile xyzs, and add the min vals xys_edge = np.dstack(np.where(xys_edge))[0] xys_edge[:, 0] += xmin - 1 xys_edge[:, 1] += ymin - 1 # Return the edge array return np.append(xys_edge, np.zeros((xys_edge.shape[0], 1), dtype=np.uint8) + zoom, axis=1)
def soc(self, count): """Get a SoC bus trace""" self.write_reg(0x231, 0) self.write_reg(0x231, 1) time.sleep(0.1) # Single Data Rate (SDR) signals sdr0 = self.read_regs(0x2000, count) sdr1 = sdr0 sdr = numpy.dstack((sdr1, sdr0))[0].reshape(len(sdr0)+len(sdr1)) print sdr0 print sdr1 print sdr # Registered copies of SDR signals reg0 = self.read_regs(0x2000, count) reg1 = reg0 reg = numpy.dstack((reg1, reg0))[0].reshape(len(reg0)+len(reg1)) print reg0 print reg1 print reg # Double Data Rate DDR signals ddr0 = self.read_regs(0x3000, count) ddr1 = self.read_regs(0x3800, count) ddr = numpy.dstack((ddr1, ddr0))[0].reshape(len(ddr0)+len(ddr1)) print ddr0 print ddr1 print ddr return sdr, reg, ddr
def applyFilter5(self): # Run the functions img = np.copy(self.curRoadRGB).astype(np.uint8) gradx = self.abs_sobel_thresh(img, orient='x', thresh=(25, 100)) grady = self.abs_sobel_thresh(img, orient='y', thresh=(50, 150)) magch = self.mag_thresh(img, sobel_kernel=9, mag_thresh=(30, 150)) dirch = self.dir_threshold(img, sobel_kernel=15, thresh=(0.5, 1.3)) sch = self.hls_s(img, thresh=(20, 80)) hch = self.hls_h(img, thresh=(130, 175)) # create the Red filter rEdgeDetect = img[:, :, 0] / 4 rEdgeDetect = 255 - rEdgeDetect rEdgeDetect[(rEdgeDetect > 220)] = 0 # Output "masked_lines" is a single channel mask shadow = np.zeros_like(dirch).astype(np.uint8) shadow[(sch > 0) & (hch > 0)] = 128 # build the combination combined = np.zeros_like(dirch).astype(np.uint8) combined[(rEdgeDetect > 192) & (rEdgeDetect < 205) & (sch > 0)] = 35 self.curRoadEdge = combined # build diag screen if in debug mode if self.debug: # create diagnostic screen 1-3 # creating a blank color channel for combining ignore_color = np.copy(gradx) * 0 self.diag1 = np.dstack((rEdgeDetect, gradx, grady)) self.diag2 = np.dstack((ignore_color, magch, dirch)) self.diag3 = np.dstack((sch, shadow, hch)) self.diag4 = np.dstack((combined, combined, combined)) * 4
def _zigzag(xmin, xmax, ymin, ymax, nx, ny): # Create the vertices. x_range = numpy.linspace(xmin, xmax, nx) y_range = numpy.linspace(ymin, ymax, ny) nodes = numpy.dstack(numpy.meshgrid(x_range, y_range, numpy.array([0.0]))).reshape( -1, 3 ) # Create the elements (cells). # a = [i + j*nx] a = numpy.add.outer(numpy.array(range(nx - 1)), nx * numpy.array(range(ny - 1))) # [i + j*nx, i+1 + j*nx, i+1 + (j+1)*nx] elems0 = numpy.dstack([a, a + 1, a + nx + 1]) # [i+1 + j*nx, i+1 + (j+1)*nx, i + (j+1)*nx] for "every other" element elems0[0::2, 1::2, 0] += 1 elems0[1::2, 0::2, 0] += 1 elems0[0::2, 1::2, 1] += nx elems0[1::2, 0::2, 1] += nx elems0[0::2, 1::2, 2] -= 1 elems0[1::2, 0::2, 2] -= 1 # [i + j*nx, i+1 + (j+1)*nx, i + (j+1)*nx] elems1 = numpy.dstack([a, a + 1 + nx, a + nx]) # [i + j*nx, i+1 + j*nx, i + (j+1)*nx] for "every other" element elems1[0::2, 1::2, 1] -= nx elems1[1::2, 0::2, 1] -= nx elems = numpy.vstack([elems0.reshape(-1, 3), elems1.reshape(-1, 3)]) return nodes, elems
def convert(self, components): if self.components == components: return self hasAlpha = self.components in (2,4) needAlpha = components in (2,4) if hasAlpha: alpha = self._data[...,-1] color = self._data[...,:-1] else: alpha = None color = self._data isMono = self.components in (1,2) toMono = components in (1,2) if isMono and not toMono: color = np.dstack((color, color, color)) elif toMono and not isMono: color = np.sum(color.astype(np.uint16), axis=-1) / 3 color = color.astype(np.uint8)[...,None] if needAlpha and alpha is None: alpha = np.zeros_like(color[...,:1]) + 255 if needAlpha: data = np.dstack((color, alpha)) else: data = color return type(self)(data = data)
def mfoldX(I, L, m, maxk): # I is the trainset # L is the Training Labels # m is the number of folds # maxk is the largest value of k we wish to test # first thing to acomplish is to randomly divide the data into m parts indices = np.random.permutation(I.shape[0]) # Creates a randomized index vector jump = round(len(L) / m) # Calculates the number of rows to jump for each fold # The following code cuts up our indices vector into m parts # I intended it to handle cases were m % I != 0 but it doesn't so rows(I) needs to be divisible by m I_index = indices[:jump] L_index = indices[:jump] for n in range(1, m - 1): # Iterats through the folds # stacks fold into a third diminsion I_index = np.dstack((I_index, indices[n * jump:(n + 1) * jump])) # a random index for the images L_index= np.dstack((L_index, indices[n * jump:(n + 1) * jump])) # a random index for the labels I_index = np.dstack((I_index, indices[(m-1) * jump:])) L_index = np.dstack((L_index, indices[(m-1) * jump:])) # Yea I'm pretty sure that wasn't necessary. I could have just used jump and the indices # but I'm not changing it now # # now data should be all nice and divided up we need to do something else error = np.zeros(maxk) # Creates a array to store our error rates for n in range(0, m): # Loop through each fold mask = np.ones(m,dtype=bool) mask[n]=0 notn = np.arange(0,m)[mask] # Creates a series of number except for the m we are currently on # Creates a Ipt variable that has all Ipt = I[I_index[:,:,notn].reshape(((m-1)*I_index.shape[1]))] Lpt = L[I_index[:,:,notn].reshape(((m-1)*I_index.shape[1]))] label,near = KNN(Ipt,Lpt ,I[I_index[:,:,n].reshape(I_index.shape[1])],10) for k in range(10): error[k] = error[k] + sum((label[k] != L[L_index[:,:,n]])[0]) error = error / (len(L)) return error
def get_fits_by_sliding_windows(birdeye_binary, line_lt, line_rt, n_windows=9, verbose=False): """ Get polynomial coefficients for lane-lines detected in an binary image. :param birdeye_binary: input bird's eye view binary image :param line_lt: left lane-line previously detected :param line_rt: left lane-line previously detected :param n_windows: number of sliding windows used to search for the lines :param verbose: if True, display intermediate output :return: updated lane lines and output image """ height, width = birdeye_binary.shape # Assuming you have created a warped binary image called "binary_warped" # Take a histogram of the bottom half of the image histogram = np.sum(birdeye_binary[height // 2:-30, :], axis=0) # Create an output image to draw on and visualize the result out_img = np.dstack((birdeye_binary, birdeye_binary, birdeye_binary)) * 255 # Find the peak of the left and right halves of the histogram # These will be the starting point for the left and right lines midpoint = len(histogram) // 2 leftx_base = np.argmax(histogram[:midpoint]) rightx_base = np.argmax(histogram[midpoint:]) + midpoint # Set height of windows window_height = np.int(height / n_windows) # Identify the x and y positions of all nonzero pixels in the image nonzero = birdeye_binary.nonzero() nonzero_y = np.array(nonzero[0]) nonzero_x = np.array(nonzero[1]) # Current positions to be updated for each window leftx_current = leftx_base rightx_current = rightx_base margin = 100 # width of the windows +/- margin minpix = 50 # minimum number of pixels found to recenter window # Create empty lists to receive left and right lane pixel indices left_lane_inds = [] right_lane_inds = [] # Step through the windows one by one for window in range(n_windows): # Identify window boundaries in x and y (and right and left) win_y_low = height - (window + 1) * window_height win_y_high = height - window * window_height win_xleft_low = leftx_current - margin win_xleft_high = leftx_current + margin win_xright_low = rightx_current - margin win_xright_high = rightx_current + margin # Draw the windows on the visualization image cv2.rectangle(out_img, (win_xleft_low, win_y_low), (win_xleft_high, win_y_high), (0, 255, 0), 2) cv2.rectangle(out_img, (win_xright_low, win_y_low), (win_xright_high, win_y_high), (0, 255, 0), 2) # Identify the nonzero pixels in x and y within the window good_left_inds = ((nonzero_y >= win_y_low) & (nonzero_y < win_y_high) & (nonzero_x >= win_xleft_low) & (nonzero_x < win_xleft_high)).nonzero()[0] good_right_inds = ((nonzero_y >= win_y_low) & (nonzero_y < win_y_high) & (nonzero_x >= win_xright_low) & (nonzero_x < win_xright_high)).nonzero()[0] # Append these indices to the lists left_lane_inds.append(good_left_inds) right_lane_inds.append(good_right_inds) # If you found > minpix pixels, recenter next window on their mean position if len(good_left_inds) > minpix: leftx_current = np.int(np.mean(nonzero_x[good_left_inds])) if len(good_right_inds) > minpix: rightx_current = np.int(np.mean(nonzero_x[good_right_inds])) # Concatenate the arrays of indices left_lane_inds = np.concatenate(left_lane_inds) right_lane_inds = np.concatenate(right_lane_inds) # Extract left and right line pixel positions line_lt.all_x, line_lt.all_y = nonzero_x[left_lane_inds], nonzero_y[ left_lane_inds] line_rt.all_x, line_rt.all_y = nonzero_x[right_lane_inds], nonzero_y[ right_lane_inds] detected = True if not list(line_lt.all_x) or not list(line_lt.all_y): left_fit_pixel = line_lt.last_fit_pixel left_fit_meter = line_lt.last_fit_meter detected = False else: left_fit_pixel = np.polyfit(line_lt.all_y, line_lt.all_x, 2) left_fit_meter = np.polyfit(line_lt.all_y * ym_per_pix, line_lt.all_x * xm_per_pix, 2) if not list(line_rt.all_x) or not list(line_rt.all_y): right_fit_pixel = line_rt.last_fit_pixel right_fit_meter = line_rt.last_fit_meter detected = False else: right_fit_pixel = np.polyfit(line_rt.all_y, line_rt.all_x, 2) right_fit_meter = np.polyfit(line_rt.all_y * ym_per_pix, line_rt.all_x * xm_per_pix, 2) line_lt.update_line(left_fit_pixel, left_fit_meter, detected=detected) line_rt.update_line(right_fit_pixel, right_fit_meter, detected=detected) # Generate x and y values for plotting ploty = np.linspace(0, height - 1, height) left_fitx = left_fit_pixel[0] * ploty**2 + left_fit_pixel[ 1] * ploty + left_fit_pixel[2] right_fitx = right_fit_pixel[0] * ploty**2 + right_fit_pixel[ 1] * ploty + right_fit_pixel[2] out_img[nonzero_y[left_lane_inds], nonzero_x[left_lane_inds]] = [255, 0, 0] out_img[nonzero_y[right_lane_inds], nonzero_x[right_lane_inds]] = [0, 0, 255] if verbose: f, ax = plt.subplots(1, 2) f.set_facecolor('white') ax[0].imshow(birdeye_binary, cmap='gray') ax[1].imshow(out_img) ax[1].plot(left_fitx, ploty, color='yellow') ax[1].plot(right_fitx, ploty, color='yellow') ax[1].set_xlim(0, 1280) ax[1].set_ylim(720, 0) plt.show() return line_lt, line_rt, out_img
def fit(binary_warped): histogram = np.sum(binary_warped[binary_warped.shape[0] // 2:, :], axis=0) out_img = np.dstack((binary_warped, binary_warped, binary_warped)) midpoint = np.int(histogram.shape[0] // 2) leftx_base = np.argmax(histogram[:midpoint]) rightx_base = np.argmax(histogram[midpoint:]) + midpoint nwindows = 9 margin = 100 minpix = 50 window_height = np.int(binary_warped.shape[0] // nwindows) nonzero = binary_warped.nonzero() nonzeroy = np.array(nonzero[0]) nonzerox = np.array(nonzero[1]) leftx_current = leftx_base rightx_current = rightx_base left_lane_inds = [] right_lane_inds = [] for window in range(nwindows): # Identify window boundaries in x and y (and right and left) win_y_low = binary_warped.shape[0] - (window + 1) * window_height win_y_high = binary_warped.shape[0] - window * window_height win_xleft_low = leftx_current - margin win_xleft_high = leftx_current + margin win_xright_low = rightx_current - margin win_xright_high = rightx_current + margin cv2.rectangle(out_img, (win_xleft_low, win_y_low), (win_xleft_high, win_y_high), (0, 255, 0), 2) cv2.rectangle(out_img, (win_xright_low, win_y_low), (win_xright_high, win_y_high), (0, 255, 0), 2) good_left_inds = ((nonzeroy >= win_y_low) & (nonzeroy < win_y_high) & (nonzerox >= win_xleft_low) & (nonzerox < win_xleft_high)).nonzero()[0] good_right_inds = ((nonzeroy >= win_y_low) & (nonzeroy < win_y_high) & (nonzerox >= win_xright_low) & (nonzerox < win_xright_high)).nonzero()[0] left_lane_inds.append(good_left_inds) right_lane_inds.append(good_right_inds) if len(good_left_inds) > minpix: leftx_current = np.int(np.mean(nonzerox[good_left_inds])) if len(good_right_inds) > minpix: rightx_current = np.int(np.mean(nonzerox[good_right_inds])) left_lane_inds = np.concatenate(left_lane_inds) right_lane_inds = np.concatenate(right_lane_inds) leftx = nonzerox[left_lane_inds] lefty = nonzeroy[left_lane_inds] rightx = nonzerox[right_lane_inds] righty = nonzeroy[right_lane_inds] left_fit = np.polyfit(lefty, leftx, 2) right_fit = np.polyfit(righty, rightx, 2) ploty = np.linspace(0, binary_warped.shape[0] - 1, binary_warped.shape[0]) left_fitx = left_fit[0] * ploty**2 + left_fit[1] * ploty + left_fit[2] right_fitx = right_fit[0] * ploty**2 + right_fit[1] * ploty + right_fit[2] out_img[lefty, leftx] = [255, 0, 0] out_img[righty, rightx] = [0, 0, 255] return [histogram, out_img, left_fit, right_fit]
def combine_flats(flat_list,MEDIAN_COMBINE=False,**kwargs): ''' Stacks images in flat_list, returns a master flat and header ''' # unpack outFile = kwargs.get('OUTFILE') clobber = kwargs.get('CLOBBER') # read data flat_comb_image = np.array([]) median_image_stack = np.array([]) nImages = 0 expTime = 0 nFlatLimit = 15 # calculate the stack batch size if nFlatLimit < len(flat_list): batchSize = len(flat_list) // 2 + 1 while batchSize > len(flat_list): batchSize = batchSize // 2 + 1 else: batchSize = len(flat_list) # loop over the flat files for file in flat_list: hdu = fits.open(file) br, inst = instruments.blue_or_red(file) data = hdu[0].data header = hdu[0].header nImages += 1 expTime += header.get('EXPTIME') # scale to expTime data /= expTime if len(flat_comb_image) == 0: flat_comb_image = np.copy(data) else: # if median combining, stack the data if MEDIAN_COMBINE: # stack images in the z direction flat_comb_image = np.dstack((flat_comb_image,data)) # if flat_comb_image is getting too big, or if we're at the end of flat_list, # then squash it along the z axis with a median if ((flat_comb_image.shape[2] == batchSize) or (flat_comb_image.shape[2] == len(flat_list)-1)): # stack the intermediate squashed frames if len(median_image_stack) == 0: median_image_stack = np.median(flat_comb_image,axis=2) else: median_image_stack = np.dstack((median_image_stack, np.median(flat_comb_image,axis=2))) # reset the stack flat_comb_image = np.array([]) # otherwise just sum them else: flat_comb_image += np.copy(data) # if median combining, squash the stack of median if MEDIAN_COMBINE: # if there are multiple median images in the stack, median them if len(median_image_stack.shape) > 2: flat_comb_image = np.median(median_image_stack,axis=2) # otherwise just return the single frame else: flat_comb_image = np.copy(median_image_stack) header.add_history('combine_flats: median combined {} files'.format(nImages)) else: header.add_history('combine_flats: summed {} files'.format(nImages)) # counts / sec * expTime flat_comb_image *= expTime if outFile: # clear space if os.path.isfile(outFile) and clobber: os.remove(outFile) # write correct flat data hdu = fits.PrimaryHDU(flat_comb_image,header) hdu.writeto(outFile,output_verify='ignore') return (flat_comb_image,header,inst)
cc_seg_FPs = measure.regionprops(labelled) FP = len(cc_seg_FPs) PPV = TP/(TP + FP) print("PPV value for image %d is: %.3f" %(i + 1, PPV)) all_PPV.append(PPV) """ Save as 3D stack """ if len(output_stack) == 0: output_stack = seg_train #output_stack_masked = seg_train_masked input_im_stack = input_save else: output_stack = np.dstack([output_stack, seg_train]) #output_stack_masked = np.dstack([output_stack_masked, seg_train_masked]) input_im_stack = np.dstack([input_im_stack, input_save]) """ Pre-processing """ # 1) get more data (and good data) # 2) overlay seg masks and binarize to get better segmentations??? """ Post-processing """ """ (1) removes all things that do not appear in > 5 slices!!!""" all_seg, all_blebs, all_eliminated = slice_thresh(output_stack, slice_size=5) filename_split = filename_split.split('_z')[0]
def drawMatches(img1, kp1, img2, kp2, matches): """ My own implementation of cv2.drawMatches as OpenCV 2.4.9 does not have this function available but it's supported in OpenCV 3.0.0 This function takes in two images with their associated keypoints, as well as a list of DMatch data structure (matches) that contains which keypoints matched in which images. An image will be produced where a montage is shown with the first image followed by the second image beside it. Keypoints are delineated with circles, while lines are connected between matching keypoints. img1,img2 - Grayscale images kp1,kp2 - Detected list of keypoints through any of the OpenCV keypoint detection algorithms matches - A list of matches of corresponding keypoints through any OpenCV keypoint matching algorithm """ # Create a new output image that concatenates the two images together # (a.k.a) a montage rows1 = img1.shape[0] cols1 = img1.shape[1] rows2 = img2.shape[0] cols2 = img2.shape[1] out = np.zeros((max([rows1,rows2]),cols1+cols2,3), dtype='uint8') # Place the first image to the left out[:rows1,:cols1,:] = np.dstack([img1, img1, img1]) # Place the next image to the right of it out[:rows2,cols1:cols1+cols2,:] = np.dstack([img2, img2, img2]) # For each pair of points we have between both images # draw circles, then connect a line between them for mat in matches: # Get the matching keypoints for each of the images img1_idx = mat.queryIdx img2_idx = mat.trainIdx # x - columns # y - rows (x1,y1) = kp1[img1_idx].pt (x2,y2) = kp2[img2_idx].pt # Draw a small circle at both co-ordinates # radius 4 # colour blue # thickness = 1 cv2.circle(out, (int(x1),int(y1)), 4, (255, 0, 0), 1) cv2.circle(out, (int(x2)+cols1,int(y2)), 4, (255, 0, 0), 1) # Draw a line in between the two points # thickness = 1 # colour blue cv2.line(out, (int(x1),int(y1)), (int(x2)+cols1,int(y2)), (255, 0, 0), 1) return out
""" assign a RGB color to a complex number z according to its escaping time i """ v = np.log2(i + 1 - np.log2(np.log2(abs(z)))) / 5 if v < 1: return v**4, v**2.5, v else: v = max(0, 2 - v) return v, v**1.5, v**3 def mandel(c): z = 0 for i in range(max_iterations): if abs(z) > radius: return color(z, i) z = z**2 + c return 0, 0, 0 y, x = np.ogrid[-1.16:1.17:figsize[1] * dpi * 1j, -2.1:0.8:figsize[0] * dpi * 1j] z = x + y * 1j R, G, B = np.array(np.frompyfunc(mandel, 1, 3)(z)).astype(np.float) RGB = np.dstack((R, G, B)) fig = plt.figure(figsize=figsize, dpi=dpi) ax = fig.add_axes([0, 0, 1, 1], aspect=1) ax.axis("off") ax.imshow(RGB) fig.savefig("Mandelbrot_Smooth_Coloring.png")
def get_fits_by_previous_fits(birdeye_binary, line_lt, line_rt, verbose=False): """ Get polynomial coefficients for lane-lines detected in an binary image. This function starts from previously detected lane-lines to speed-up the search of lane-lines in the current frame. :param birdeye_binary: input bird's eye view binary image :param line_lt: left lane-line previously detected :param line_rt: left lane-line previously detected :param verbose: if True, display intermediate output :return: updated lane lines and output image """ height, width = birdeye_binary.shape left_fit_pixel = line_lt.last_fit_pixel right_fit_pixel = line_rt.last_fit_pixel nonzero = birdeye_binary.nonzero() nonzero_y = np.array(nonzero[0]) nonzero_x = np.array(nonzero[1]) margin = 100 left_lane_inds = ((nonzero_x > (left_fit_pixel[0] * (nonzero_y**2) + left_fit_pixel[1] * nonzero_y + left_fit_pixel[2] - margin)) & (nonzero_x < (left_fit_pixel[0] * (nonzero_y**2) + left_fit_pixel[1] * nonzero_y + left_fit_pixel[2] + margin))) right_lane_inds = ((nonzero_x > (right_fit_pixel[0] * (nonzero_y**2) + right_fit_pixel[1] * nonzero_y + right_fit_pixel[2] - margin)) & (nonzero_x < (right_fit_pixel[0] * (nonzero_y**2) + right_fit_pixel[1] * nonzero_y + right_fit_pixel[2] + margin))) # Extract left and right line pixel positions line_lt.all_x, line_lt.all_y = nonzero_x[left_lane_inds], nonzero_y[ left_lane_inds] line_rt.all_x, line_rt.all_y = nonzero_x[right_lane_inds], nonzero_y[ right_lane_inds] detected = True if not list(line_lt.all_x) or not list(line_lt.all_y): left_fit_pixel = line_lt.last_fit_pixel left_fit_meter = line_lt.last_fit_meter detected = False else: left_fit_pixel = np.polyfit(line_lt.all_y, line_lt.all_x, 2) left_fit_meter = np.polyfit(line_lt.all_y * ym_per_pix, line_lt.all_x * xm_per_pix, 2) if not list(line_rt.all_x) or not list(line_rt.all_y): right_fit_pixel = line_rt.last_fit_pixel right_fit_meter = line_rt.last_fit_meter detected = False else: right_fit_pixel = np.polyfit(line_rt.all_y, line_rt.all_x, 2) right_fit_meter = np.polyfit(line_rt.all_y * ym_per_pix, line_rt.all_x * xm_per_pix, 2) line_lt.update_line(left_fit_pixel, left_fit_meter, detected=detected) line_rt.update_line(right_fit_pixel, right_fit_meter, detected=detected) # Generate x and y values for plotting ploty = np.linspace(0, height - 1, height) left_fitx = left_fit_pixel[0] * ploty**2 + left_fit_pixel[ 1] * ploty + left_fit_pixel[2] right_fitx = right_fit_pixel[0] * ploty**2 + right_fit_pixel[ 1] * ploty + right_fit_pixel[2] # Create an image to draw on and an image to show the selection window img_fit = np.dstack((birdeye_binary, birdeye_binary, birdeye_binary)) * 255 window_img = np.zeros_like(img_fit) # Color in left and right line pixels img_fit[nonzero_y[left_lane_inds], nonzero_x[left_lane_inds]] = [255, 0, 0] img_fit[nonzero_y[right_lane_inds], nonzero_x[right_lane_inds]] = [0, 0, 255] # Generate a polygon to illustrate the search window area # And recast the x and y points into usable format for cv2.fillPoly() left_line_window1 = np.array( [np.transpose(np.vstack([left_fitx - margin, ploty]))]) left_line_window2 = np.array( [np.flipud(np.transpose(np.vstack([left_fitx + margin, ploty])))]) left_line_pts = np.hstack((left_line_window1, left_line_window2)) right_line_window1 = np.array( [np.transpose(np.vstack([right_fitx - margin, ploty]))]) right_line_window2 = np.array( [np.flipud(np.transpose(np.vstack([right_fitx + margin, ploty])))]) right_line_pts = np.hstack((right_line_window1, right_line_window2)) # Draw the lane onto the warped blank image cv2.fillPoly(window_img, np.int_([left_line_pts]), (0, 255, 0)) cv2.fillPoly(window_img, np.int_([right_line_pts]), (0, 255, 0)) result = cv2.addWeighted(img_fit, 1, window_img, 0.3, 0) if verbose: plt.imshow(result) plt.plot(left_fitx, ploty, color='yellow') plt.plot(right_fitx, ploty, color='yellow') plt.xlim(0, 1280) plt.ylim(720, 0) plt.show() return line_lt, line_rt, img_fit
c, cv2.isContourConvex(c), cv2.contourArea(c), )) contour_info = sorted(contour_info, key=lambda c: c[2], reverse=True) max_contour = contour_info[0] # -- Create empty mask, draw filled polygon on it corresponding to largest contour ---- # Mask is black, polygon is white mask = np.zeros(edges.shape) cv2.fillConvexPoly(mask, max_contour[0], (255)) # -- Smooth mask, then blur it -------------------------------------------------------- mask = cv2.dilate(mask, None, iterations=MASK_DILATE_ITER) mask = cv2.erode(mask, None, iterations=MASK_ERODE_ITER) mask = cv2.GaussianBlur(mask, (BLUR, BLUR), 0) mask_stack = np.dstack([mask] * 3) # Create 3-channel alpha mask # -- Blend masked img into MASK_COLOR background -------------------------------------- mask_stack = mask_stack.astype('float32') / 255.0 # Use float matrices, img = img.astype('float32') / 255.0 # for easy blending masked = (mask_stack * img) + ((1 - mask_stack) * MASK_COLOR) # Blend masked = (masked * 255).astype('uint8') # Convert back to 8-bit cv2.imshow('img', masked) # Display cv2.waitKey() if cv2.waitKey(1) == ord('q'): break
def denoising_img_avg(save_as='denoise_img.tif', path='auto', file_type='.tif', robust=True, noise_floor=False): """ Noise reduction by image averaging. Images should be aligned. By noise we refer here to random fluctuations in brighness produced by the CCD and CMOS sensors. Parameters ---------- save_as : string the name of the generated image, the format to use is determined from the filename extension path : string the path to the folder containing the images to average. If 'auto', the default, the function will ask you for the folder location through a file selection dialog. file_type : string the image format to read robust : bool, default True if True the averaging method use the median. If false the mean. noise_floor : bool, default False if True, the noise floor is calculated. Returns ------- a numpy array with denoised image (and the std values of each pixel if noise_floor=True). The latter is useful is you want to locate pixels that yield large errors. Examples -------- >>> denoising_img_avg(path='C:/Users/name/Documents/my_images/') >>> denoising_img_avg(save_as='new_image.tif', path='C:/Users/name/Documents/my_images/') # For estimate and visualize the noise floor of the image do >>> denoise_img, std_px_vals = denoising_img_avg(noise_floor=True) >>> fig, ax = plt.subplots() >>> im = ax.imshow(std_px_vals, cmap='cividis') >>> fig.colorbar(im, ax=ax) """ if path == 'auto': path = get_path() # open and stack all images print(' ') print('Stacking images...') count = 0 for filename in os.listdir(path): if filename.endswith(file_type): img = np.array(Image.open(path + filename).convert('L')) if count == 0: img_stack = img else: img_stack = np.dstack((img_stack, img)) count += 1 # denoise by averaging print(' ') print('Denoising image...') if robust is True: denoise_img = np.median(img_stack, axis=2) else: denoise_img = np.mean(img_stack, axis=2) # convert from float to integer denoise_img = np.rint(denoise_img) # Estimate the noise floor if proceed if noise_floor is True: # TODO: ask whether the the noise floor is for a ROI crop = input("Do you want to define a region of interest (ROI) to estimate the noise floor? (type 'y' or 'n'): ") if crop == 'y': x, y, xp, yp = input("Define the coordinates (x, y, x', y') (e.g. (1014, 192, 4692, 4644): ") px_std_vals = np.std(img_stack[x - 1:y, xp - 1:yp, :], axis=2) print(' ') print('Estimating the noise floor...') px_std_vals = np.std(img_stack, axis=2) mean_std = np.mean(px_std_vals) print(' ') print('Noise floor:') print('Mean of SD values =', round(mean_std, 2)) print('max, min =', round(np.max(px_std_vals), 2), ',', round(np.min(px_std_vals))) # plot the image using matplotlib fig, ax = plt.subplots() im = ax.imshow(denoise_img, cmap='bone') fig.colorbar(im, ax=ax) fig.tight_layout() # save the denoised image in the same path img = Image.fromarray(denoise_img) img.save(path + save_as) if noise_floor is True: return denoise_img, px_std_vals else: return denoise_img
def main(): parser = create_parser() args = parser.parse_args() # Pre-process features assert ( os.path.isfile(args.input_audio) ), f"The given path is not a file!. Please check your input again. Given input: {args.input_audio}" print("Processing features of input audio: {}".format(args.input_audio)) Z, tfrL0, tfrLF, tfrLQ, t, cenf, f = feature_extraction(args.input_audio) # Load pre-trained model minfo = ModelInfo() model = minfo.load_model(args.model_path) minfo.onset_th = minfo.onset_th if args.onset_th is None else args.onset_th print(minfo) # Post-process feature according to the configuration of model if minfo.feature_type == "HCFP": assert (len(minfo.input_channels) == (HarmonicNum * 2 + 2)) spec = [] ceps = [] for i in range(HarmonicNum + 1): spec.append(fetch_harmonic(tfrL0, cenf, i)) ceps.append(fetch_harmonic(tfrLQ, cenf, i)) spec = np.transpose(np.array(spec), axes=(2, 1, 0)) ceps = np.transpose(np.array(ceps), axes=(2, 1, 0)) feature = np.dstack((spec, ceps)) else: assert (len(minfo.input_channels) <= 4) feature = np.array([Z, tfrL0, tfrLF, tfrLQ]) feature = np.transpose(feature, axes=(2, 1, 0)) print("Predicting...") pred = predict_v1(feature[:, :, minfo.input_channels], model, minfo.timesteps, batch_size=4) mode_mapping = { "frame": "true_frame", "frame_onset": "note", "multi_instrument_frame": "true_frame", "multi_instrument_note": "note" } midi = MultiPostProcess(pred, mode=mode_mapping[minfo.label_type], onset_th=minfo.onset_th, dura_th=minfo.dura_th, frm_th=minfo.frm_th, inst_th=minfo.inst_th, t_unit=0.02) if args.to_midi is not None: midi.write(args.to_midi) print("Midi written as {}".format(args.to_midi))
img1 = skcol.rgb2gray(skio.imread(pathImgs[0])) img2 = skcol.rgb2gray(skio.imread(pathImgs[1])) CC, dxy, maxVal, pq = ph.phaseCorr(img1, img2) img2_shift = np.roll(img2, int(np.floor(+dxy[0])), 1) img2_shift = np.roll(img2_shift, int(np.floor(+dxy[1])), 0) # frm2_nrm_shift=np.roll(frm2_nrm_shift, int(math.floor(-dxy[1])), 0) print ('-') plt.figure(figsize=(16,6)) plt.subplot(1, 5, 1) plt.imshow(img1) plt.title('img#1') plt.subplot(1, 5, 2) plt.imshow(img2) plt.title('img#2') plt.subplot(1, 5, 3) plt.imshow(np.dstack((img1, img2, img1))) plt.title('img#1 vs img#2') plt.subplot(1, 5, 4) plt.imshow(img2_shift) plt.title('img#2_shift') plt.subplot(1, 5, 5) plt.imshow(np.dstack((img1, img2_shift, img1))) plt.title('img#1 vs img#2_shift') plt.show() print ('-')
def panorama(idx, cam_prefix, imu_ts, ukf_euler): # camera projection params Wfov = np.pi / 3 Hfov = np.pi / 4 frame = None # load cam data cam = io.loadmat(cam_prefix + str(idx) + ".mat") cam_vals = np.array(cam['cam']) # m*n*3*k cam_ts = np.array(cam['ts']).T # k*1 imu_idx = 0 # init video writer outfile = 'result/panorama' + str(idx) + '.avi' fourcc = cv2.VideoWriter_fourcc('D', 'I', 'V', 'X') video = cv2.VideoWriter(filename=outfile, fourcc=fourcc, fps=20.0, frameSize=(1920, 960)) for i in range(cam_ts.shape[0]): RGB = cam_vals[:, :, :, i] height, width, channel = RGB.shape cam_t = cam_ts[i] # generate spherical coordinate radius = 1 azimuth = -(np.arange(width) / (width - 1) - 0.5) * Wfov altitude = -(np.arange(height) / (height - 1) - 0.5) * Hfov # negate because pixel origin is at top left azimuth, altitude = np.meshgrid(azimuth, altitude) # transform to cartesian coordinates on unit sphere X = radius * np.cos(altitude) * np.cos(azimuth) Y = radius * np.cos(altitude) * np.sin(azimuth) Z = radius * np.sin(altitude) C = np.dstack((X, Y, Z)) # (m,n,3) # rotate by the nearest timestamp if imu_idx > len(imu_ts) - 1: # check boundary break while imu_ts[imu_idx] < cam_t and imu_idx < len(imu_ts) - 1: imu_idx += 1 if imu_ts[imu_idx] + imu_ts[imu_idx - 1] > 2 * cam_t: # choose closest imu_idx -= 1 euler = ukf_euler[imu_idx] imu_idx += 1 # prevent replication R = taitbryan.euler2mat(euler[0], euler[1], euler[2]) C = np.einsum('pr,mnr->mnp', R, C) # transform cartesian back to spherical X = C[:, :, 0] Y = C[:, :, 1] Z = C[:, :, 2] azimuth = -np.arctan2(Y, X) altitude = -np.arctan2( Z, np.sqrt(X**2 + Y**2)) # altitude range(-pi/2,pi/2) # negate back # radius doesn't change # project sphere to a plane, convert plane into pixel by scaling Px = ((azimuth + np.pi) / Wfov * width).astype(np.uint) Py = ((altitude + np.pi / 2) / Hfov * height).astype(np.uint) # Py = np.flipud(Py.reshape((height, width))).reshape((1,-1)) # paint your pixels on to image if frame is None: # init panorama after knowing image size and camera params frame = np.zeros( (int(np.pi / Hfov * height), int(2 * np.pi / Wfov * width), 3), dtype=np.uint8) frame[Py, Px, :] = RGB # display animation cv2.imshow('Stitching Panorama', frame) cv2.waitKey(10) # write frame to video file video.write(frame) video.release() cv2.destroyAllWindows()
# model from the remaining seasons lag1model = lagged1.loc[lagged1['year'] != 20152016] # predict from the 20142015 season (lag = 2) lag2predictfrom = lagged2.loc[lagged1['year'] == 20152016] # the rows of interest are in the same position as those in lagged1 # model from the remaining seasons lag2model = lagged2.loc[lagged1['year'] != 20152016] lag3predictfrom = lagged3.loc[lagged1['year'] == 20152016] lag3model = lagged3.loc[lagged1['year'] != 20152016] # This array contains all data needed test and train the model modelarrrayfrom = np.transpose(np.dstack((np.array(lag1model), np.array(lag2model), np.array(lag3model))), (0,2,1)) # This array is the one that will be predicted from: predictarrayfrom = np.transpose(np.dstack((np.array(lag1predictfrom), np.array(lag2predictfrom), np.array(lag3predictfrom))), (0,2,1)) #%% Let's harness things from here on. Define a function that separates the # data into training and testing sets; trains the model; predicts; evaluates # prediction quality def modelrun(modelfrom, predictfrom, nrons, epchs, bsize): """
def _mesh_from_dtm(dtm, name='Terrain'): """ Creates a Blender *mesh* from a DTM Parameters ---------- dtm : DTM name : str, optional The name that will be assigned to the new mesh, defaults to 'Terrain' (and, if an object named 'Terrain' already exists, Blender will automatically extend the name of the new object to something like 'Terrain.001') Returns ---------- mesh : bpy_types.Mesh Notes ---------- * We are switching coordinate systems from the NumPy to Blender. Numpy: Blender: + ----> (0, j) ^ (0, y) | | | | v (i, 0) + ----> (x, 0) """ # Create an empty mesh mesh = bpy.data.meshes.new(name) # Get the xy-coordinates from the DTM, see docstring notes y, x = np.indices(dtm.data.shape).astype('float64') x *= dtm.mesh_scale y *= -1 * dtm.mesh_scale # Create an array of 3D vertices vertices = np.dstack([x, y, dtm.data]).reshape((-1, 3)) # Drop vertices with NaN values (used in the DTM to represent # areas with no data) vertices = vertices[~np.isnan(vertices).any(axis=1)] # Calculate the faces of the mesh triangulation = Triangulate(dtm.data) faces = triangulation.face_list() # Fill the mesh mesh.from_pydata(vertices, [], faces) mesh.update() # Create a new UV layer mesh.uv_textures.new("HiRISE Generated UV Map") # We'll use a bmesh to populate the UV map with values bm = bmesh.new() bm.from_mesh(mesh) bm.faces.ensure_lookup_table() uv_layer = bm.loops.layers.uv[0] # Iterate over each face in the bmesh num_faces = len(bm.faces) w = dtm.data.shape[1] h = dtm.data.shape[0] for face_index in range(num_faces): # Iterate over each loop in the face for loop in bm.faces[face_index].loops: # Get this loop's vertex coordinates vert_coords = loop.vert.co.xy # And calculate it's uv coordinate. We do this by dividing the # vertice's x and y coordinates by: # # d + 1, dimensions of DTM (in "posts") # mesh_scale, meters/DTM "post" # # This has the effect of mapping the vertex to its # corresponding "post" index in the DTM, and then mapping # that value to the range [0, 1). u = vert_coords.x / ((w + 1) * dtm.mesh_scale) v = 1 + vert_coords.y / ((h + 1) * dtm.mesh_scale) loop[uv_layer].uv = (u, v) bm.to_mesh(mesh) return mesh
def lane_line_polynomials(img, last_known_polynomials): # Identify the x and y positions of all nonzero pixels in the image nonzero = img.nonzero() nonzeroy = np.array(nonzero[0]) nonzerox = np.array(nonzero[1]) # Set the width of the windows +/- margin margin = 20 #50 # Set minimum number of pixels found to recenter window minpix = 100 # Create empty lists to receive left and right lane pixel indices left_lane_inds = [] right_lane_inds = [] if last_known_polynomials is None: # Take a histogram of the bottom half of the image histogram = np.sum(img[img.shape[0]/2:,:], axis=0) # Choose the number of sliding windows nwindows = 25 #9 # Set height of windows window_height = np.int(img.shape[0]/nwindows) # Find the peak of the left and right halves of the histogram # These will be the starting point for the left and right lines midpoint = np.int(histogram.shape[0]/2) leftx_base = np.argmax(histogram[:midpoint]) rightx_base = np.argmax(histogram[midpoint:]) + midpoint # Current positions to be updated for each window leftx_current = leftx_base rightx_current = rightx_base # Step through the windows one by one for window in range(nwindows): # Identify window boundaries in x and y (and right and left) win_y_low = img.shape[0] - (window+1)*window_height win_y_high = img.shape[0] - window*window_height win_xleft_low = leftx_current - margin win_xleft_high = leftx_current + margin win_xright_low = rightx_current - margin win_xright_high = rightx_current + margin # Identify the nonzero pixels in x and y within the window good_left_inds = ((nonzeroy >= win_y_low) & (nonzeroy < win_y_high) & (nonzerox >= win_xleft_low) & (nonzerox < win_xleft_high)).nonzero()[0] good_right_inds = ((nonzeroy >= win_y_low) & (nonzeroy < win_y_high) & (nonzerox >= win_xright_low) & (nonzerox < win_xright_high)).nonzero()[0] # Append these indices to the lists left_lane_inds.append(good_left_inds) right_lane_inds.append(good_right_inds) # If you found > minpix pixels, recenter next window on their mean position if len(good_left_inds) > minpix: leftx_current = np.int(np.mean(nonzerox[good_left_inds])) if len(good_right_inds) > minpix: rightx_current = np.int(np.mean(nonzerox[good_right_inds])) # Concatenate the arrays of indices left_lane_inds = np.concatenate(left_lane_inds) right_lane_inds = np.concatenate(right_lane_inds) else: smaller_margin = int(margin / 1) left_fit = last_known_polynomials[0] right_fit = last_known_polynomials[1] left_lane_inds = ((nonzerox > (left_fit[0]*(nonzeroy**2) + left_fit[1]*nonzeroy + left_fit[2] - smaller_margin)) & (nonzerox < (left_fit[0]*(nonzeroy**2) + left_fit[1]*nonzeroy + left_fit[2] + smaller_margin))) right_lane_inds = ((nonzerox > (right_fit[0]*(nonzeroy**2) + right_fit[1]*nonzeroy + right_fit[2] - smaller_margin)) & (nonzerox < (right_fit[0]*(nonzeroy**2) + right_fit[1]*nonzeroy + right_fit[2] + smaller_margin))) # Extract left and right line pixel positions leftx = nonzerox[left_lane_inds] lefty = nonzeroy[left_lane_inds] rightx = nonzerox[right_lane_inds] righty = nonzeroy[right_lane_inds] # Fit a second order polynomial to each left_fit = np.polyfit(lefty, leftx, 2) right_fit = np.polyfit(righty, rightx, 2) # Calculate curvature ploty = np.linspace(0, img.shape[0]-1, img.shape[0]) y_eval = np.max(ploty) ym_per_pix = 30/720 # meters per pixel in y dimension xm_per_pix = 3.7/700 # meters per pixel in x dimension # Fit new polynomials to x,y in world space left_fit_cr = np.polyfit(lefty*ym_per_pix, leftx*xm_per_pix, 2) right_fit_cr = np.polyfit(righty*ym_per_pix, rightx*xm_per_pix, 2) # Calculate the radii of curvature left_curverad = ((1 + (2*left_fit_cr[0]*y_eval*ym_per_pix + left_fit_cr[1])**2)**1.5) / np.absolute(2*left_fit_cr[0]) right_curverad = ((1 + (2*right_fit_cr[0]*y_eval*ym_per_pix + right_fit_cr[1])**2)**1.5) / np.absolute(2*right_fit_cr[0]) # Calculate the points for the left and right sides of the fit left_fitx = left_fit[0]*ploty**2 + left_fit[1]*ploty + left_fit[2] right_fitx = right_fit[0]*ploty**2 + right_fit[1]*ploty + right_fit[2] left_line_window = np.array([np.transpose(np.vstack([left_fitx, ploty]))]) right_line_window = np.array([np.flipud(np.transpose(np.vstack([right_fitx, ploty])))]) # Verify if we have to correct one of the lanes, based on curvature ratio curverad_ratio = left_curverad / right_curverad left_base = left_line_window[0][left_line_window[0].shape[0]-1-40][0] right_base = right_line_window[0][40][0] base_delta = (right_base - left_base) replace_left = False replace_right = False if curverad_ratio <= 1/3: # Left side is 4x as curved as right side, let's discard the left side replace_left = True elif curverad_ratio >= 3: # Right side is 4x as curved as right side, let's discard the right side replace_right = True elif (left_fit[0] < 0 and right_fit[0] > 0) or (left_fit[0] > 0 and right_fit[0] < 0): # Lines diverge, take the one with the smallest curvature replace_left = left_curverad > right_curverad replace_right = not(replace_left) if replace_left: left_line_window = ((np.array([np.flipud(right_line_window[0])]) - [base_delta, 0]) * [1, 0]) + (left_line_window * [0, 1]) left_curverad = right_curverad elif replace_right: right_line_window = ((np.array([np.flipud(left_line_window[0])]) + [base_delta, 0]) * [1, 0]) + (right_line_window * [0, 1]) right_curverad = left_curverad # Create an output image to draw on and visualize the result out_img = np.dstack((img, img, img))*0 # Paint the polynomial and the lane data that took us there lane_pts = np.hstack((left_line_window, right_line_window)) cv2.fillPoly(out_img, np.int_([lane_pts]), (0, 230, 0)) out_img[nonzeroy[left_lane_inds], nonzerox[left_lane_inds]] = [200, 0, 0] out_img[nonzeroy[right_lane_inds], nonzerox[right_lane_inds]] = [0, 0, 200] # Crop the top, where most of the noise happens imshape = out_img.shape top = np.array([[(imshape[1]/4, 0), (imshape[1]/4*3, 0), (imshape[1]/4*3, imshape[0]/10), (imshape[1]/4, imshape[0]/10)]], dtype=np.int32) cv2.fillPoly(out_img, top, [0, 0, 0]) # Calculate offset offset = ((imshape[1] / 2) - ((left_base + right_base) / 2)) * xm_per_pix # Return results return out_img, np.array([left_fit, right_fit]), left_curverad, right_curverad, offset
# Apilamiento vertical print('Apilamiento vertical con concatenate =\n', np.concatenate((a, b), axis=0)) # Si axis=0, el apilamiento es vertical # In[8]: # APILAMIENTO EN PROFUNDIDAD # En el apilamiento en profundidad, se crean bloques utilizando # parejas de datos tomados de las dos matrices # Matrices origen print('a =\n', a, '\n') print('b =\n', b, '\n') # Apilamiento en profundidad print('Apilamiento en profundidad =\n', np.dstack((a, b))) # In[9]: # APILAMIENTO POR COLUMNAS # El apilamiento por columnas es similar a hstack() # Se apilan las columnas, de izquierda a derecha, y tomándolas # de los bloques definidos en la matriz # Matrices origen print('a =\n', a, '\n') print('b =\n', b, '\n') # Apilamiento vertical print('Apilamiento por columnas =\n', np.column_stack((a, b))) # In[10]:
def showAnns(self, anns): """ Display the specified annotations. :param anns (array of object): annotations to display :return: None """ if len(anns) == 0: return 0 if 'segmentation' in anns[0] or 'keypoints' in anns[0]: datasetType = 'instances' elif 'caption' in anns[0]: datasetType = 'captions' else: raise Exception('datasetType not supported') if datasetType == 'instances': ax = plt.gca() ax.set_autoscale_on(False) polygons = [] color = [] for ann in anns: c = (np.random.random((1, 3)) * 0.6 + 0.4).tolist()[0] if 'segmentation' in ann: if type(ann['segmentation']) == list: # polygon for seg in ann['segmentation']: poly = np.array(seg).reshape( (int(len(seg) / 2), 2)) polygons.append(Polygon(poly)) color.append(c) else: # mask t = self.imgs[ann['image_id']] if type(ann['segmentation']['counts']) == list: rle = maskUtils.frPyObjects([ann['segmentation']], t['height'], t['width']) else: rle = [ann['segmentation']] m = maskUtils.decode(rle) img = np.ones((m.shape[0], m.shape[1], 3)) if ann['iscrowd'] == 1: color_mask = np.array([2.0, 166.0, 101.0]) / 255 if ann['iscrowd'] == 0: color_mask = np.random.random((1, 3)).tolist()[0] for i in range(3): img[:, :, i] = color_mask[i] ax.imshow(np.dstack((img, m * 0.5))) if 'keypoints' in ann and type(ann['keypoints']) == list: # turn skeleton into zero-based index sks = np.array( self.loadCats(ann['category_id'])[0]['skeleton']) - 1 kp = np.array(ann['keypoints']) x = kp[0::3] y = kp[1::3] v = kp[2::3] for sk in sks: if np.all(v[sk] > 0): plt.plot(x[sk], y[sk], linewidth=3, color=c) plt.plot(x[v > 0], y[v > 0], 'o', markersize=8, markerfacecolor=c, markeredgecolor='k', markeredgewidth=2) plt.plot(x[v > 1], y[v > 1], 'o', markersize=8, markerfacecolor=c, markeredgecolor=c, markeredgewidth=2) p = PatchCollection(polygons, facecolor=color, linewidths=0, alpha=0.4) ax.add_collection(p) p = PatchCollection(polygons, facecolor='none', edgecolors=color, linewidths=2) ax.add_collection(p) elif datasetType == 'captions': for ann in anns: print(ann['caption'])
def double_thresholded_binary(img, s_thresh=(170, 255), sx_thresh=(20, 100)): binary = thresholded_binary(img) binary_3d = np.dstack((binary, binary, binary))*255 return thresholded_binary(binary_3d, sobel_kernel=5)
def process_image(img): new_img = np.copy(img) img_bin, Minv = pipeline(new_img) # if both left and right lines were detected last frame, use polyfit_using_prev_fit, otherwise use sliding window if not l_line.detected or not r_line.detected: l_fit, r_fit, l_lane_inds, r_lane_inds, _ = sliding_window_polyfit( img_bin) else: l_fit, r_fit, l_lane_inds, r_lane_inds = polyfit_using_prev_fit( img_bin, l_line.best_fit, r_line.best_fit) # invalidate both fits if the difference in their x-intercepts isn't around 350 px (+/- 100 px) if l_fit is not None and r_fit is not None: # calculate x-intercept (bottom of image, x=image_height) for fits h = img.shape[0] l_fit_x_int = l_fit[0] * h**2 + l_fit[1] * h + l_fit[2] r_fit_x_int = r_fit[0] * h**2 + r_fit[1] * h + r_fit[2] x_int_diff = abs(r_fit_x_int - l_fit_x_int) if abs(350 - x_int_diff) > 100: l_fit = None r_fit = None l_line.add_fit(l_fit, l_lane_inds) r_line.add_fit(r_fit, r_lane_inds) # draw the current best fit if it exists if l_line.best_fit is not None and r_line.best_fit is not None: img_out1 = draw_lane(new_img, img_bin, l_line.best_fit, r_line.best_fit, Minv) rad_l, rad_r, d_center = calc_curv_rad_and_center_dist( img_bin, l_line.best_fit, r_line.best_fit, l_lane_inds, r_lane_inds) img_out = draw_data(img_out1, (rad_l + rad_r) / 2, d_center) else: img_out = new_img diagnostic_output = False if diagnostic_output: # put together multi-view output diag_img = np.zeros((720, 1280, 3), dtype=np.uint8) # original output (top left) diag_img[0:360, 0:640, :] = cv2.resize(img_out, (640, 360)) # binary overhead view (top right) img_bin = np.dstack((img_bin * 255, img_bin * 255, img_bin * 255)) resized_img_bin = cv2.resize(img_bin, (640, 360)) diag_img[0:360, 640:1280, :] = resized_img_bin # overhead with all fits added (bottom right) img_bin_fit = np.copy(img_bin) for i, fit in enumerate(l_line.current_fit): img_bin_fit = plot_fit_onto_img(img_bin_fit, fit, (20 * i + 100, 0, 20 * i + 100)) for i, fit in enumerate(r_line.current_fit): img_bin_fit = plot_fit_onto_img(img_bin_fit, fit, (0, 20 * i + 100, 20 * i + 100)) img_bin_fit = plot_fit_onto_img(img_bin_fit, l_line.best_fit, (255, 255, 0)) img_bin_fit = plot_fit_onto_img(img_bin_fit, r_line.best_fit, (255, 255, 0)) diag_img[360:720, 640:1280, :] = cv2.resize(img_bin_fit, (640, 360)) # diagnostic data (bottom left) color_ok = (200, 255, 155) color_bad = (255, 155, 155) font = cv2.FONT_HERSHEY_DUPLEX if l_fit is not None: text = 'This fit L: ' + ' {:0.6f}'.format(l_fit[0]) + \ ' {:0.6f}'.format(l_fit[1]) + \ ' {:0.6f}'.format(l_fit[2]) else: text = 'This fit L: None' cv2.putText(diag_img, text, (40, 380), font, .5, color_ok, 1, cv2.LINE_AA) if r_fit is not None: text = 'This fit R: ' + ' {:0.6f}'.format(r_fit[0]) + \ ' {:0.6f}'.format(r_fit[1]) + \ ' {:0.6f}'.format(r_fit[2]) else: text = 'This fit R: None' cv2.putText(diag_img, text, (40, 400), font, .5, color_ok, 1, cv2.LINE_AA) text = 'Best fit L: ' + ' {:0.6f}'.format(l_line.best_fit[0]) + \ ' {:0.6f}'.format(l_line.best_fit[1]) + \ ' {:0.6f}'.format(l_line.best_fit[2]) cv2.putText(diag_img, text, (40, 440), font, .5, color_ok, 1, cv2.LINE_AA) text = 'Best fit R: ' + ' {:0.6f}'.format(r_line.best_fit[0]) + \ ' {:0.6f}'.format(r_line.best_fit[1]) + \ ' {:0.6f}'.format(r_line.best_fit[2]) cv2.putText(diag_img, text, (40, 460), font, .5, color_ok, 1, cv2.LINE_AA) text = 'Diffs L: ' + ' {:0.6f}'.format(l_line.diffs[0]) + \ ' {:0.6f}'.format(l_line.diffs[1]) + \ ' {:0.6f}'.format(l_line.diffs[2]) if l_line.diffs[0] > 0.001 or \ l_line.diffs[1] > 1.0 or \ l_line.diffs[2] > 100.: diffs_color = color_bad else: diffs_color = color_ok cv2.putText(diag_img, text, (40, 500), font, .5, diffs_color, 1, cv2.LINE_AA) text = 'Diffs R: ' + ' {:0.6f}'.format(r_line.diffs[0]) + \ ' {:0.6f}'.format(r_line.diffs[1]) + \ ' {:0.6f}'.format(r_line.diffs[2]) if r_line.diffs[0] > 0.001 or \ r_line.diffs[1] > 1.0 or \ r_line.diffs[2] > 100.: diffs_color = color_bad else: diffs_color = color_ok cv2.putText(diag_img, text, (40, 520), font, .5, diffs_color, 1, cv2.LINE_AA) text = 'Good fit count L:' + str(len(l_line.current_fit)) cv2.putText(diag_img, text, (40, 560), font, .5, color_ok, 1, cv2.LINE_AA) text = 'Good fit count R:' + str(len(r_line.current_fit)) cv2.putText(diag_img, text, (40, 580), font, .5, color_ok, 1, cv2.LINE_AA) img_out = diag_img write_name = './output_images/Step2_ProcessImage_' + '.jpg' cv2.imwrite(write_name, img_out) return img_out
def find_lane(binary_warped): flag_same_lines = False leftx_base, midpoint, rightx_base = find_base(binary_warped) # print(leftx_base, midpoint, rightx_base) # при 0 значении left base out_img = np.dstack((binary_warped, binary_warped, binary_warped)) * 255 # cv2.imshow("out_img",out_img) nwindows = 9 window_height = np.int(binary_warped.shape[0] / nwindows) # 10 частей по 20 px. высота окна = 20 # print(window_height) nonzero = binary_warped.nonzero() # Индексы не нулевых элементов python # print(nonzero) # cv2.waitKey(0) nonzeroy = np.array(nonzero[0]) # индексы по x nonzerox = np.array(nonzero[1]) # индексы по y leftx_current = leftx_base rightx_current = rightx_base # margin = 100 # minpix = 50 margin = 100 minpix = 50 left_lane_inds = [] right_lane_inds = [] # Will start the search from scratch for first frame and then will use margin window # If the sanity fails then will search from scratch if (left_lane.detected == False) or (right_lane.detected == False): # Если левой или правой линии не найдено то: for window in range(nwindows): # Identify window boundaries in x and y (and right and left) win_y_low = binary_warped.shape[0] - (window + 1) * window_height win_y_high = binary_warped.shape[0] - window * window_height # print(win_y_low,win_y_high) win_xleft_low = leftx_current - margin win_xleft_high = leftx_current + margin win_xright_low = rightx_current - margin win_xright_high = rightx_current + margin # print(win_xleft_low) # Identify the nonzero pixels in x and y within the window good_left_inds = ((nonzeroy >= win_y_low) & (nonzeroy < win_y_high) & (nonzerox >= win_xleft_low) & (nonzerox < win_xleft_high)).nonzero()[0] good_right_inds = ((nonzeroy >= win_y_low) & (nonzeroy < win_y_high) & (nonzerox >= win_xright_low) & (nonzerox < win_xright_high)).nonzero()[0] # Append these indices to the lists left_lane_inds.append(good_left_inds) # print("__________________") # print(left_lane_inds) # print("__________________") right_lane_inds.append(good_right_inds) if len(good_left_inds) > minpix: leftx_current = np.int(np.mean(nonzerox[good_left_inds])) # print(leftx_current) if len(good_right_inds) > minpix: rightx_current = np.int(np.mean(nonzerox[good_right_inds])) # Concatenate the arrays of indices left_lane_inds = np.concatenate(left_lane_inds) right_lane_inds = np.concatenate(right_lane_inds) left_lane.detected = True right_lane.detected = True else: left_lane_inds = ( (nonzerox > (left_lane.current_fit[0] * (nonzeroy**2) + left_lane.current_fit[1] * nonzeroy + left_lane.current_fit[2] - margin)) & (nonzerox < (left_lane.current_fit[0] * (nonzeroy**2) + left_lane.current_fit[1] * nonzeroy + left_lane.current_fit[2] + margin))) right_lane_inds = ( (nonzerox > (right_lane.current_fit[0] * (nonzeroy**2) + right_lane.current_fit[1] * nonzeroy + right_lane.current_fit[2] - margin)) & (nonzerox < (right_lane.current_fit[0] * (nonzeroy**2) + right_lane.current_fit[1] * nonzeroy + right_lane.current_fit[2] + margin))) # Extract left and right line pixel positions leftx = nonzerox[left_lane_inds] lefty = nonzeroy[left_lane_inds] rightx = nonzerox[right_lane_inds] righty = nonzeroy[right_lane_inds] # print(len(leftx)) # print(len(rightx)) # cv2.waitKey(0) # Saving successful pixel position values to the respective Line objects # количество пикселей в распознанной разметке. if (len(leftx) < 800): # if (len(leftx) < 1500): leftx = left_lane.allx lefty = left_lane.ally left_lane.detected = False else: left_lane.allx = leftx left_lane.ally = lefty if (len(rightx) < 800): # if (len(rightx) < 1500): rightx = right_lane.allx righty = right_lane.ally right_lane.detected = False else: right_lane.allx = rightx right_lane.ally = righty # # Возвращение на дорогу с линии. Когда длины массивов равны, принимает обе линии за одну, расположение левой и правой линии становится одинаковым # if (len(leftx) == len(rightx)): leftx = left_lane.allx lefty = left_lane.ally left_lane.detected = False rightx = right_lane.allx righty = right_lane.ally right_lane.detected = False else: right_lane.allx = rightx right_lane.ally = righty # Fit a second order polynomial to each left_fit = np.polyfit(lefty, leftx, 2) right_fit = np.polyfit(righty, rightx, 2) # print(left_fit) # Sanity check if (left_lane.current_fit[0] == False): left_lane.current_fit = left_fit right_lane.current_fit = right_fit # print ("____") # print(abs(left_lane.current_fit[1] - left_fit[1])) if (abs(left_lane.current_fit[1] - left_fit[1]) > 0.18): left_lane.current_fit = left_lane.best_fit left_lane.detected = False else: left_lane.current_fit = left_fit left_lane.recent_xfitted.pop() left_lane.recent_xfitted.appendleft(left_lane.current_fit) avg = np.array([0, 0, 0], dtype='float') for element in left_lane.recent_xfitted: avg = avg + element left_lane.best_fit = avg / (len(left_lane.recent_xfitted)) if (abs(right_lane.current_fit[1] - right_fit[1]) > 0.18): right_lane.current_fit = right_lane.best_fit right_lane.detected = False else: right_lane.current_fit = right_fit right_lane.recent_xfitted.pop() right_lane.recent_xfitted.appendleft(right_lane.current_fit) avg = np.array([0, 0, 0], dtype='float') for element in right_lane.recent_xfitted: avg = avg + element right_lane.best_fit = avg / (len(right_lane.recent_xfitted)) if (abs(right_lane.current_fit[1] - right_fit[1]) > 0.38 and abs(left_lane.current_fit[1] - left_fit[1]) < 0.1): right_lane.current_fit[0] = left_lane.current_fit[0] right_lane.current_fit[1] = left_lane.current_fit[1] right_lane.current_fit[2] = left_lane.current_fit[2] + 600 right_lane.recent_xfitted.pop() right_lane.recent_xfitted.appendleft(right_lane.current_fit) avg = np.array([0, 0, 0], dtype='float') for element in right_lane.recent_xfitted: avg = avg + element right_lane.best_fit = avg / (len(right_lane.recent_xfitted)) if (abs(left_lane.current_fit[1] - left_fit[1]) > 0.38 and abs(right_lane.current_fit[1] - right_fit[1]) < 0.1): leftx = left_lane.allx lefty = left_lane.ally left_lane.detected = False rightx = right_lane.allx righty = right_lane.ally right_lane.detected = False # print("________________________________________") # cv2.waitKey(0) # какая то хрень, реагирует на сильные уходы линии за пределы видимости, а также моменты, когда линия вернулась left_lane.current_fit = left_fit left_lane.recent_xfitted.pop() left_lane.recent_xfitted.appendleft(left_lane.current_fit) avg = np.array([0, 0, 0], dtype='float') for element in left_lane.recent_xfitted: avg = avg + element left_lane.best_fit = avg / (len(left_lane.recent_xfitted)) # Generate x and y values for plotting # ploty = np.linspace(0, 720-1, 720 ) ploty = np.linspace(0, binary_warped.shape[0] - 1, binary_warped.shape[0]) left_fitx = left_lane.current_fit[0] * ploty**2 + left_lane.current_fit[ 1] * ploty + left_lane.current_fit[2] right_fitx = right_lane.current_fit[0] * ploty**2 + right_lane.current_fit[ 1] * ploty + right_lane.current_fit[2] out_img[nonzeroy[left_lane_inds], nonzerox[left_lane_inds]] = [255, 0, 0] out_img[nonzeroy[right_lane_inds], nonzerox[right_lane_inds]] = [0, 0, 255] cv2.imshow("out2", out_img) return ploty, lefty, righty, leftx, rightx, left_fitx, right_fitx, out_img
aligned_frames = align.process(frames) # Get aligned frames aligned_depth_frame = aligned_frames.get_depth_frame() # aligned_depth_frame is a 640x480 depth image color_frame = aligned_frames.get_color_frame() # Validate that both frames are valid if not aligned_depth_frame or not color_frame: continue depth_image = np.asanyarray(aligned_depth_frame.get_data()) color_image = np.asanyarray(color_frame.get_data()) # Remove background - Set pixels further than clipping_distance to grey grey_color = 153 depth_image_3d = np.dstack((depth_image,depth_image,depth_image)) #depth image is 1 channel, color is 3 channels bg_removed = np.where((depth_image_3d > clipping_distance) | (depth_image_3d <= 0), grey_color, color_image) # Render images depth_colormap = cv2.applyColorMap(cv2.convertScaleAbs(depth_image, alpha=0.03), cv2.COLORMAP_JET) ret1, bw_img1 = cv2.threshold(bg_removed,127,255,cv2.THRESH_BINARY_INV) ret2, bw_img2 = cv2.threshold(depth_colormap,127,255,cv2.THRESH_BINARY) bitmap=cv2.resize(bw_img1,(200,200),cv2.CV_16S,1) bitmap = bitmap.astype('uint8') newimg=cv2.multiply(bitmap,255) x_batch=newimg.reshape(1,image_size,image_size,3) feed_dict_testing = { x:x_batch, y_true: y_test_images} result=sess.run(y_pred, feed_dict=feed_dict_testing) images = np.hstack((newimg, newimg))
# -*- coding: utf-8 -*- from __future__ import unicode_literals import numpy as np a = np.arange(11, 20).reshape(3, 3) print(a) b = np.arange(21, 30).reshape(3, 3) print(b) # c = np.vstack((a, b)) c = np.concatenate((a, b), axis=0) print(c) # d = np.hstack((a, b)) d = np.concatenate((a, b), axis=1) print(d) e = np.dstack((a, b)) print(e) a = a.ravel() print(a) b = b.ravel() print(b) f = np.row_stack((a, b)) print(f) # g = np.hstack((a.reshape(-1, 1), b.reshape(-1, 1))) g = np.column_stack((a, b)) print(g)
def warp(image, inverse_map, map_args={}, output_shape=None, order=1, mode='constant', cval=0., clip=True, preserve_range=False): """Warp an image according to a given coordinate transformation. Parameters ---------- image : ndarray Input image. inverse_map : transformation object, callable ``cr = f(cr, **kwargs)``, or ndarray Inverse coordinate map, which transforms coordinates in the output images into their corresponding coordinates in the input image. There are a number of different options to define this map, depending on the dimensionality of the input image. A 2-D image can have 2 dimensions for gray-scale images, or 3 dimensions with color information. - For 2-D images, you can directly pass a transformation object, e.g. `skimage.transform.SimilarityTransform`, or its inverse. - For 2-D images, you can pass a ``(3, 3)`` homogeneous transformation matrix, e.g. `skimage.transform.SimilarityTransform.params`. - For 2-D images, a function that transforms a ``(M, 2)`` array of ``(col, row)`` coordinates in the output image to their corresponding coordinates in the input image. Extra parameters to the function can be specified through `map_args`. - For N-D images, you can directly pass an array of coordinates. The first dimension specifies the coordinates in the input image, while the subsequent dimensions determine the position in the output image. E.g. in case of 2-D images, you need to pass an array of shape ``(2, rows, cols)``, where `rows` and `cols` determine the shape of the output image, and the first dimension contains the ``(row, col)`` coordinate in the input image. See `scipy.ndimage.map_coordinates` for further documentation. Note, that a ``(3, 3)`` matrix is interpreted as a homogeneous transformation matrix, so you cannot interpolate values from a 3-D input, if the output is of shape ``(3,)``. See example section for usage. map_args : dict, optional Keyword arguments passed to `inverse_map`. output_shape : tuple (rows, cols), optional Shape of the output image generated. By default the shape of the input image is preserved. Note that, even for multi-band images, only rows and columns need to be specified. order : int, optional The order of interpolation. The order has to be in the range 0-5: - 0: Nearest-neighbor - 1: Bi-linear (default) - 2: Bi-quadratic - 3: Bi-cubic - 4: Bi-quartic - 5: Bi-quintic mode : {'constant', 'edge', 'symmetric', 'reflect', 'wrap'}, optional Points outside the boundaries of the input are filled according to the given mode. Modes match the behaviour of `numpy.pad`. cval : float, optional Used in conjunction with mode 'constant', the value outside the image boundaries. clip : bool, optional Whether to clip the output to the range of values of the input image. This is enabled by default, since higher order interpolation may produce values outside the given input range. preserve_range : bool, optional Whether to keep the original range of values. Otherwise, the input image is converted according to the conventions of `img_as_float`. Also see https://scikit-image.org/docs/dev/user_guide/data_types.html Returns ------- warped : double ndarray The warped input image. Notes ----- - The input image is converted to a `double` image. - In case of a `SimilarityTransform`, `AffineTransform` and `ProjectiveTransform` and `order` in [0, 3] this function uses the underlying transformation matrix to warp the image with a much faster routine. Examples -------- >>> from skimage.transform import warp >>> from skimage import data >>> image = data.camera() The following image warps are all equal but differ substantially in execution time. The image is shifted to the bottom. Use a geometric transform to warp an image (fast): >>> from skimage.transform import SimilarityTransform >>> tform = SimilarityTransform(translation=(0, -10)) >>> warped = warp(image, tform) Use a callable (slow): >>> def shift_down(xy): ... xy[:, 1] -= 10 ... return xy >>> warped = warp(image, shift_down) Use a transformation matrix to warp an image (fast): >>> matrix = np.array([[1, 0, 0], [0, 1, -10], [0, 0, 1]]) >>> warped = warp(image, matrix) >>> from skimage.transform import ProjectiveTransform >>> warped = warp(image, ProjectiveTransform(matrix=matrix)) You can also use the inverse of a geometric transformation (fast): >>> warped = warp(image, tform.inverse) For N-D images you can pass a coordinate array, that specifies the coordinates in the input image for every element in the output image. E.g. if you want to rescale a 3-D cube, you can do: >>> cube_shape = np.array([30, 30, 30]) >>> cube = np.random.rand(*cube_shape) Setup the coordinate array, that defines the scaling: >>> scale = 0.1 >>> output_shape = (scale * cube_shape).astype(int) >>> coords0, coords1, coords2 = np.mgrid[:output_shape[0], ... :output_shape[1], :output_shape[2]] >>> coords = np.array([coords0, coords1, coords2]) Assume that the cube contains spatial data, where the first array element center is at coordinate (0.5, 0.5, 0.5) in real space, i.e. we have to account for this extra offset when scaling the image: >>> coords = (coords + 0.5) / scale - 0.5 >>> warped = warp(cube, coords) """ if image.size == 0: raise ValueError("Cannot warp empty image with dimensions", image.shape) image = convert_to_float(image, preserve_range) input_shape = np.array(image.shape) if output_shape is None: output_shape = input_shape else: output_shape = safe_as_int(output_shape) warped = None if order == 2: # When fixing this issue, make sure to fix the branches further # below in this function warn("Bi-quadratic interpolation behavior has changed due " "to a bug in the implementation of scikit-image. " "The new version now serves as a wrapper " "around SciPy's interpolation functions, which itself " "is not verified to be a correct implementation. Until " "skimage's implementation is fixed, we recommend " "to use bi-linear or bi-cubic interpolation instead.") if order in (0, 1, 3) and not map_args: # use fast Cython version for specific interpolation orders and input matrix = None if isinstance(inverse_map, np.ndarray) and inverse_map.shape == (3, 3): # inverse_map is a transformation matrix as numpy array matrix = inverse_map elif isinstance(inverse_map, HOMOGRAPHY_TRANSFORMS): # inverse_map is a homography matrix = inverse_map.params elif (hasattr(inverse_map, '__name__') and inverse_map.__name__ == 'inverse' and get_bound_method_class(inverse_map) in HOMOGRAPHY_TRANSFORMS): # inverse_map is the inverse of a homography matrix = np.linalg.inv(inverse_map.__self__.params) if matrix is not None: matrix = matrix.astype(image.dtype) ctype = 'float32_t' if image.dtype == np.float32 else 'float64_t' if image.ndim == 2: warped = _warp_fast[ctype](image, matrix, output_shape=output_shape, order=order, mode=mode, cval=cval) elif image.ndim == 3: dims = [] for dim in range(image.shape[2]): dims.append(_warp_fast[ctype](image[..., dim], matrix, output_shape=output_shape, order=order, mode=mode, cval=cval)) warped = np.dstack(dims) if warped is None: # use ndi.map_coordinates if (isinstance(inverse_map, np.ndarray) and inverse_map.shape == (3, 3)): # inverse_map is a transformation matrix as numpy array, # this is only used for order >= 4. inverse_map = ProjectiveTransform(matrix=inverse_map) if isinstance(inverse_map, np.ndarray): # inverse_map is directly given as coordinates coords = inverse_map else: # inverse_map is given as function, that transforms (N, 2) # destination coordinates to their corresponding source # coordinates. This is only supported for 2(+1)-D images. if image.ndim < 2 or image.ndim > 3: raise ValueError("Only 2-D images (grayscale or color) are " "supported, when providing a callable " "`inverse_map`.") def coord_map(*args): return inverse_map(*args, **map_args) if len(input_shape) == 3 and len(output_shape) == 2: # Input image is 2D and has color channel, but output_shape is # given for 2-D images. Automatically add the color channel # dimensionality. output_shape = (output_shape[0], output_shape[1], input_shape[2]) coords = warp_coords(coord_map, output_shape) # Pre-filtering not necessary for order 0, 1 interpolation prefilter = order > 1 ndi_mode = _to_ndimage_mode(mode) warped = ndi.map_coordinates(image, coords, prefilter=prefilter, mode=ndi_mode, order=order, cval=cval) _clip_warp_output(image, warped, order, mode, cval, clip) return warped
exampleImg_bin, Minv = pipeline(exampleImg) left_fit, right_fit, left_lane_inds, right_lane_inds, visualization_data = sliding_window_polyfit( exampleImg_bin) h = exampleImg.shape[0] left_fit_x_int = left_fit[0] * h**2 + left_fit[1] * h + left_fit[2] right_fit_x_int = right_fit[0] * h**2 + right_fit[1] * h + right_fit[2] #print('fit x-intercepts:', left_fit_x_int, right_fit_x_int) rectangles = visualization_data[0] histogram = visualization_data[1] # Create an output image to draw on and visualize the result out_img = np.uint8( np.dstack((exampleImg_bin, exampleImg_bin, exampleImg_bin)) * 255) # Generate x and y values for plotting ploty = np.linspace(0, exampleImg_bin.shape[0] - 1, exampleImg_bin.shape[0]) left_fitx = left_fit[0] * ploty**2 + left_fit[1] * ploty + left_fit[2] right_fitx = right_fit[0] * ploty**2 + right_fit[1] * ploty + right_fit[2] for rect in rectangles: # Draw the windows on the visualization image cv2.rectangle(out_img, (rect[2], rect[0]), (rect[3], rect[1]), (0, 255, 0), 2) cv2.rectangle(out_img, (rect[4], rect[0]), (rect[5], rect[1]), (0, 255, 0), 2) # Identify the x and y positions of all nonzero pixels in the image nonzero = exampleImg_bin.nonzero() nonzeroy = np.array(nonzero[0]) nonzerox = np.array(nonzero[1]) out_img[nonzeroy[left_lane_inds], nonzerox[left_lane_inds]] = [255, 0, 0]
# Define the Hough transform parameters # Make a blank the same size as our image to draw on rho = 2 theta = np.pi / 180 threshold = 30 min_line_length = 10 max_line_gap = 10 line_image = np.copy(image) * 0 #creating a blank to draw lines on # Run Hough on edge detected image lines = cv2.HoughLinesP(masked_edges, rho, theta, threshold, np.array([]), min_line_length, max_line_gap) print(lines) # Iterate over the output "lines" and draw lines on the blank for line in lines: for x1, y1, x2, y2 in line: cv2.line(line_image, (x1, y1), (x2, y2), (255, 0, 0), 2) # Create a "color" binary image to combine with line image color_edges = np.dstack((edges, edges, edges)) # Draw the lines on the edge image combo = cv2.addWeighted(color_edges, 0.8, line_image, 1, 0) # Display the image plt.imshow(combo) # plt.imshow(lines) plt.show()
print('Done!') # From that, simulate state vote shares print('Simulating state vote shares....', end=' ') ## Creating arrays for the known state-level variables ### natl_pct_lag last_natl_vote = natl_results\ .loc[natl_results['year'] == 2017, ['party', 'pct']]\ .pivot_table(columns = 'party', values = 'pct')\ .to_numpy() last_natl_vote_stack = np.concatenate((last_natl_vote, ) * n_sims) natl_pct_change = natl_poll_sims - last_natl_vote_stack natl_pct_change_contrib = np.dstack( (natl_pct_change * all_party_state_lm.coef_[1], ) * 16) ### pct_lag last_state_vote = state_results\ .loc[state_results['year'] == 2017, ['state', 'party', 'pct']]\ .pivot_table(index = 'state', columns = 'party', values = 'pct')\ .to_numpy() # n_sims x 6 x 16 version last_state_vote_stack = np.dstack((last_state_vote, ) * n_sims).T last_state_vote_contrib = np.dstack( (last_state_vote * all_party_state_lm.coef_[0], ) * n_sims).T ### Intercept state_intercept_contrib = np.full(shape=(n_sims, 6, 16),
# zero all elements above diagonal msk1 = np.tril(tmpl, idx2) # zero all elements below diagonal msk2 = np.triu(tmpl, idx1) # combine masks msk = np.logical_and(msk1, msk2) msk = np.logical_and(msk, aperture) mskObli1[:, :, i] = msk mskObli2[:, :, i] = np.fliplr(msk) mskBarObli = np.concatenate((mskObli1, mskObli2), axis=2) mskBar = np.concatenate((mskBarCard, mskBarObli), axis=2) # add first image, which will be zero-only image mskBar = np.dstack((np.zeros((xpix, ypix)), mskBar)) # %% """ (2a) square apertures in cardinal orientation""" # get indices to divide along width dimension in n equally szied segments xsplitIdx = np.split(np.arange(xpix * varSupSmp), nsplits / 2) # get indices to divide along height dimension in n equally szied segments ysplitIdx = np.split(np.arange(ypix * varSupSmp), nsplits / 2) # create empty masks mskSquareCard = np.empty([xpix, ypix, (nsplits * nsplits) / 4]) # combine conditions iterables = [ np.arange(nsplits / 2).astype(int),
def search_around_poly( self, binary_warped, previous_left_fit, previous_right_fit, debug_img=False ): """ :param binary_warped: Our image processing pipeline output image. :param previous_left_fit: Previous LH polynomial coefficients. :param previous_right_fit: Previous RH polynomial coefficients. :param debug_img: Bool, if True return a debug image. :return: new_left_fit: New LH polynomial coefficients. new_right_fit: New Rh polynomial coefficients. left_fitx: Left fitted x points based on the polynomial coefficients. right_fitx: Right fitted x points based on the polynomial coefficients. ploty: Y points for both lines. result: If debug_img True then return a debug image else return None. """ margin = self.params["sliding_windows"]["margin"] # Grab activated pixels nonzero = binary_warped.nonzero() nonzeroy = np.array(nonzero[0]) nonzerox = np.array(nonzero[1]) # Here we see if the non zero point is greater than or less that out polynomial point +/- our margin on the x axis left_lane_inds = ( nonzerox > ( previous_left_fit[0] * (nonzeroy ** 2) + previous_left_fit[1] * nonzeroy + previous_left_fit[2] - margin ) ) & ( nonzerox < ( previous_left_fit[0] * (nonzeroy ** 2) + previous_left_fit[1] * nonzeroy + previous_left_fit[2] + margin ) ) right_lane_inds = ( nonzerox > ( previous_right_fit[0] * (nonzeroy ** 2) + previous_right_fit[1] * nonzeroy + previous_right_fit[2] - margin ) ) & ( nonzerox < ( previous_right_fit[0] * (nonzeroy ** 2) + previous_right_fit[1] * nonzeroy + previous_right_fit[2] + margin ) ) # Again, extract left and right line pixel positions leftx = nonzerox[left_lane_inds] lefty = nonzeroy[left_lane_inds] rightx = nonzerox[right_lane_inds] righty = nonzeroy[right_lane_inds] # Fit new polynomials new_left_fit, new_right_fit, new_left_m, new_right_m, left_fitx, right_fitx, ploty, out_img = self.fit_poly( binary_warped.shape, leftx, lefty, rightx, righty, out_img=np.dstack((binary_warped, binary_warped, binary_warped)) * 255, ) if debug_img: # Create an image to draw on and an image to show the selection window out_img = np.dstack((binary_warped, binary_warped, binary_warped)) * 255 window_img = np.zeros_like(out_img) # Color in left and right line pixels out_img[nonzeroy[left_lane_inds], nonzerox[left_lane_inds]] = [255, 0, 0] out_img[nonzeroy[right_lane_inds], nonzerox[right_lane_inds]] = [0, 0, 255] # Generate a polygon to illustrate the search window area # And recast the x and y points into usable format for cv2.fillPoly() left_line_window1 = np.array( [np.transpose(np.vstack([left_fitx - margin, ploty]))] ) left_line_window2 = np.array( [np.flipud(np.transpose(np.vstack([left_fitx + margin, ploty])))] ) left_line_pts = np.hstack((left_line_window1, left_line_window2)) right_line_window1 = np.array( [np.transpose(np.vstack([right_fitx - margin, ploty]))] ) right_line_window2 = np.array( [np.flipud(np.transpose(np.vstack([right_fitx + margin, ploty])))] ) right_line_pts = np.hstack((right_line_window1, right_line_window2)) # Draw the lane onto the warped blank image cv2.fillPoly(window_img, np.int_([left_line_pts]), (0, 255, 0)) cv2.fillPoly(window_img, np.int_([right_line_pts]), (0, 255, 0)) result = cv2.addWeighted(out_img, 1, window_img, 0.3, 0) # Draw new polylines draw_points_l = (np.asarray([left_fitx, ploty]).T).astype(np.int32) draw_points_r = (np.asarray([right_fitx, ploty]).T).astype(np.int32) cv2.polylines(result, [draw_points_l], False, (0, 255, 255), thickness=3) cv2.polylines(result, [draw_points_r], False, (0, 255, 255), thickness=3) return ( new_left_fit, new_right_fit, new_left_m, new_right_m, left_fitx, right_fitx, ploty, result, ) else: result = None return ( new_left_fit, new_right_fit, new_left_m, new_right_m, left_fitx, right_fitx, ploty, result, )
ax2.set_title('Pipeline Result', fontsize=40) plt.subplots_adjust(left=0., right=1, top=0.9, bottom=0.) ##################################### FINDING LINES WITH SLIDING WINDOW ############################### # Implement Sliding Windows and Fit a Polynomial import numpy as np import cv2 import matplotlib.pyplot as plt # Assuming you have created a warped binary image called "binary_warped" # Take a histogram of the bottom half of the image histogram = np.sum(binary_warped[binary_warped.shape[0] / 2:, :], axis=0) # Create an output image to draw on and visualize the result out_img = np.dstack((binary_warped, binary_warped, binary_warped)) * 255 # Find the peak of the left and right halves of the histogram # These will be the starting point for the left and right lines midpoint = np.int(histogram.shape[0] / 2) leftx_base = np.argmax(histogram[:midpoint]) rightx_base = np.argmax(histogram[midpoint:]) + midpoint # Choose the number of sliding windows nwindows = 9 # Set height of windows window_height = np.int(binary_warped.shape[0] / nwindows) # Identify the x and y positions of all nonzero pixels in the image nonzero = binary_warped.nonzero() nonzeroy = np.array(nonzero[0]) nonzerox = np.array(nonzero[1]) # Current positions to be updated for each window
from perception import perception_step from decision import decision_step from supporting_functions import update_rover, create_output_images # Initialize socketio server and Flask application # (learn more at: https://python-socketio.readthedocs.io/en/latest/) sio = socketio.Server() app = Flask(__name__) # Read in ground truth map and create 3-channel green version for overplotting # NOTE: images are read in by default with the origin (0, 0) in the upper left # and y-axis increasing downward. ground_truth = mpimg.imread('../calibration_images/map_bw.png') # This next line creates arrays of zeros in the red and blue channels # and puts the map into the green channel. This is why the underlying # map output looks green in the display image ground_truth_3d = np.dstack( (ground_truth * 0, ground_truth * 255, ground_truth * 0)).astype(np.float) # Define RoverState() class to retain rover state parameters class RoverState(): def __init__(self): self.start_time = None # To record the start time of navigation self.total_time = None # To record total duration of naviagation self.finish_time = None # To record time when navigation was finished self.img = None # Current camera image self.pos = None # Current position (x, y) self.yaw = None # Current yaw angle self.pitch = None # Current pitch angle self.roll = None # Current roll angle self.vel = None # Current velocity self.steer = 0 # Current steering angle