def test(args): print('Welcome to the world of neural networks!') activation = 'sigmoid' if (args.dataset == 'MNIST'): inSize = 28 * 28 else: inSize = 200 * 200 NoImages = len(os.listdir(args.test)) inputs = np.zeros((NoImages, inSize)) itrNo = 0 for imgName in os.listdir(args.test): imgName = args.test + '/' + imgName if (args.dataset == 'Cat-Dog'): img = util.rgb2gray(mpimg.imread(imgName)) else: img = mpimg.imread(imgName) inputs[itrNo, :] = img.reshape(inSize) itrNo += 1 network = mlp.MLNN() fileName = '../Model/' + args.dataset + '.npy' numLayers = network.loadWeights(fileName) network.forwardProp(inputs) predict = network.predict(network.nnLayers[numLayers - 1].output) predicted = np.argmax(predict, axis=1) print(predicted)
def calibrate_cam( img_filenames, n_x, n_y, draw=-1 ) : """Perform camera calibration via cv2.findChessboardCorners from a bunch of images stored in the paths contained in the list img_filenames. Return the camera matrix and the distortion coefficients """ imgpoints = [] objpoints = [] objp = np.zeros( (n_x*n_y, 3), dtype=np.float32) objp[:,:2] = np.mgrid[0:n_x, 0:n_y].T.reshape(-1,2) for i, fname in enumerate( img_filenames ) : img = u.rgb_read( fname ) #gray = u.rgb2gray( img ) ret, corners = cv2.findChessboardCorners(u.rgb2gray( img ), (n_x,n_y), None) if draw == i : cv2.drawChessboardCorners(img, (n_x, n_y), corners, ret) plt.imshow( img ) if ret : imgpoints.append( corners ) objpoints.append( objp ) print( f"img_filename = {fname} ret={ret} len(corners)=" f"{len(corners) if corners is not None else 0} len(imgpoints)={len(imgpoints)}" ) #perform calibration _, mtx, dist, _, _ = cv2.calibrateCamera( objpoints, imgpoints, (img.shape[1], img.shape[0]), None, None) return mtx, dist
def my_prob_map(image): g_im = rgb2gray(image) sx = ndimage.sobel(g_im, axis=0) sy = ndimage.sobel(g_im, axis=1) sobel = np.hypot(sx, sy) sobel *= 1 / np.max(sobel) p_map = signal.convolve2d(sobel, np.ones((5, 5)), mode='same') p_map /= np.sum(p_map) return p_map.flatten()
def main(): args = handle_args() filename = args.filename rgb_img = util.load_img(filename) if np.max(rgb_img) > 1: rgb_img = rgb_img / 255 grey_img = util.rgb2gray(rgb_img) #dots_g = stippling.dot(grey_img, style='grid') #dots_v = stippling.dot(grey_img, style='voronoi', num_dots=10000) dots_cg = stippling.dot(grey_img, style='cgrid', num_dots=10000) #dots_dither = stippling.dot(grey_img, style='dithering') dots = dots_cg # fig = plt.figure("Input") # ax = fig.add_subplot(1,2,1) # stippling.show_dots(dots_cg, ax) # plt.show() print('Starting TSP') line = tsp.tsp(dots, style='rnn') print('Altering tour') line = tsp.alter_tour(line, max_len=100) if DISP: fig = plt.figure("Input") ax = fig.add_subplot(1,2,1) ax.imshow(rgb_img) ax = fig.add_subplot(1,2,2) ax.imshow(grey_img, cmap='Greys_r') fig = plt.figure("Art") ax = fig.add_subplot(1,3,1) ax.set_aspect('equal') #stippling.show_dots(dots_g, ax) ax = fig.add_subplot(1,3,2) ax.set_aspect('equal') stippling.show_dots(dots_cg, ax) ax = fig.add_subplot(1,3,3) ax.set_aspect('equal') #stippling.show_dots(dots_dither, ax) fig = plt.figure("TSP") ax = fig.add_subplot(1,1,1) ax.set_aspect('equal') postprocess(line, grey_img, 'thickness', ax) plt.show()
def img2seam(color_img): """ To encapsulate the process of calculating energy function and blablabla. The input color_img is intact without any modification. Another words, the seam has been calculated but hasn't been applied to the img. Return y_seam, a Python list. """ gray_img = rgb2gray(color_img) energies = map_gradient(gray_img) c_energies, c_paths = cumulative_energy(energies) y_end = c_energies[-1].argmin() y_seam = search_seam(c_paths, y_end) return y_seam
def main(): print("Test") # import image save = "example42" save = None img = plt.imread("./example3.jpg") size = img.shape if size[0] > 250: h = size[0] w = size[1] while max(h, w) > 250: h = h // 2 w = w // 2 img = imresize(img, (h, w)) # convert to grayscale if image has 3rd rank (RGBA channels) if len(img.shape) == 3: img = rgb2gray(img) # normalize img = img.astype(float) / np.max(img) width = img.shape[1] height = img.shape[0] ## Main Chan-Vese process # get a shape from mouse clicking on GUI shapes = create_shape_on_image(img, max_shape_num=1) shape = shapes[0] # create a mask from the shape mask = create_mask_from_shape(width, height, shape) # show the mask imagesc(mask.astype(int), colorbar=False, title="The Mask") # create a Chan-Vese Model model = ChanVeseModel(img, mask, check_same_every_n_iter=50) i = 0 model.draw(save=save) model.draw(save=save) while not model.done(): model.iterate(_lambda1=1, _lambda2=1, _mu=.1, _nu=.02, _dt=.5) if i % 10 == 9: model.draw(save=save) i += 1 model.draw(block=True)
def gen_sp_texture_descriptors(superpixels, cell_size): box_size = np.array([cell_size, cell_size]) gray = rgb2gray(im2single(superpixels.image)) img_data_pad = pad(gray, cell_size, 'constant', constant_values=0) n_sps = superpixels.n_superpixels features = None for sp_idx in range(n_sps): one_sp_mask = superpixels.mask(sp_idx) image_under_sp, _ = gen_box_img_pad(img_data_pad, one_sp_mask, box_size) one_feat = local_binary_pattern(image_under_sp, _N_LBP_ANGLES, cell_size/2, method='uniform').flatten().T if features is None: features = np.zeros((n_sps, one_feat.shape[0])) features[sp_idx,:] = one_feat[:] return features
def perform_computations(self): img = np.asarray( Image.open(self.args.image_file).rotate( self.args.rotate, expand=True)).astype('float') logging.info("Loaded image {}".format(self.args.image_file)) logging.info("Image has size {}x{}".format(img.shape[0], img.shape[1])) # Greyscale images are 2d arrays, color ones are 3d (x, y) if len(img.shape) > 2: logging.info("Image is in color, changing to greyscale") img = util.rgb2gray(img) img = (255 - img) img[img < self.args.floor] = self.args.floor img[img > self.args.ceil] = self.args.ceil img = img**self.args.power # Convert each point to the probability of containing a point img = self.args.points * img / np.sum(img) actual_points = 0 points = [] for ii in range(img.shape[0]): for jj in range(img.shape[1]): if img[ii][jj] > random.random(): actual_points += 1 points.append((ii, jj)) pc = copy.copy(points) logging.info("Points requested: {}".format(self.args.points)) logging.info("Actual points generated: {}".format(actual_points)) points = self._sort_points(points) points = np.array(points) tck, u = scipy.interpolate.splprep(points.T, u=None, s=0.0, per=1) u_new = np.linspace(u.min(), u.max(), len(points) * 10) x_new, y_new = scipy.interpolate.splev(u_new, tck, der=0) return {"points": pc, "x": x_new, "y": y_new}
def perform_computations(self): """Given the filename of an image, the number of random points to drop on there and a power representing the mapping between pixel darkness and probability of containing a point, place points on pixels weighted by their darkness, then do a voronoi tessellation on the resulting points and return the scipy voronoi tesselation object """ img = np.asarray( Image.open(self.args.filename).rotate(self.args.rotate, expand=True)).astype('float') logging.info("Loaded image {}".format(self.args.filename)) logging.info("Image has size {}x{}".format(img.shape[0], img.shape[1])) # Greyscale images are 2d arrays, color ones are 3d (x, y) if len(img.shape) > 2: logging.info("Image is in color, changing to greyscale") img = util.rgb2gray(img) img = (255 - img) img[img < self.args.floor] = self.args.floor img[img > self.args.ceil] = self.args.ceil img = img**self.args.power # Convert each point to the probability of containing a point img = self.args.vertices * img / np.sum(img) actual_points = 0 points = [] for ii in range(img.shape[0]): for jj in range(img.shape[1]): if img[ii][jj] > random.random(): actual_points += 1 points.append((ii, jj)) logging.info("Points requested: {}".format(self.args.vertices)) logging.info("Actual points generated: {}".format(actual_points)) return {"image": img, "voronoi": Voronoi(points)}
def perform_computations(self): n = 1 image = np.asarray(Image.open( self.args.image_file).rotate(180)).astype('float') if len(image.shape) > 2: image = util.rgb2gray(image) image[image > 200] = 255 xcoords = np.linspace(-self.args.scale_factor, self.args.scale_factor, image.shape[0]) ycoords = np.linspace(-self.args.scale_factor, self.args.scale_factor, image.shape[1]) theta = ((np.linspace(0.0001, 1, num=self.args.points))**0.5 * self.args.revolutions * 2 * np.pi) r = theta**(1 / n) r = (r / max(abs(r))) * self.args.scale_factor xx = np.cos(theta) yy = np.sin(theta) xxc = np.copy(xx) yyc = np.copy(yy) xx *= r yy *= r darkness = scipy.interpolate.interpn(np.stack([ycoords, xcoords]), image, (xx, yy)) darkness = (255 - darkness) / 255.0 darkness = darkness r = theta**(1 / n) r += darkness * self.args.max_deviation * np.sin(6000 * theta * r) r = (r / max(abs(r))) * self.args.scale_factor return {"x": r * xxc, "y": r * yyc, "r": r}
def __getitem__(self, index): filename = "" if self.mode == "train": filename = index + 1 elif self.mode == "test": filename = str(int(index + 1) + self.train_samples) ms_orig = np.load('./dataset/new_npy/msimage_{}.npy'.format(filename)) #pan_orig = np.load('./dataset/new_npy/panimage_{}.npy'.format(filename)) gray_ms = rgb2gray(ms_orig) ms_orig = ms_orig.astype(np.float32) gray_ms = gray_ms.astype(np.float32) ms_norm = np.array( [scale_range(i, -1, 1) for i in ms_orig.transpose((2, 0, 1))]) gray_ms_norm = scale_range(gray_ms, -1, 1) if self.random_downsampling: reduce_size = np.random.randint(20, 80) else: reduce_size = 64 ms_down = [resize(i, (reduce_size, reduce_size), 3) for i in ms_norm] ms_up = [resize(i, (256, 256), 3) for i in ms_down] ms_up = np.clip(ms_up, -1.0, 1.0) inp = np.concatenate((ms_up, np.expand_dims(gray_ms_norm, axis=0)), axis=0) out = ms_norm del ms_orig #del pan_orig del ms_down del gray_ms return inp.astype(np.float32), out, index
def get_obs(self): # Show window with environment self.env.render() if self.img_mode: # Get matrix of pixels of environment env_pxls = self.env.render(mode='rgb_array') # Convert to grayscale env_pxls = util.rgb2gray(env_pxls) # Cut target area env_pxls = env_pxls[ENV_HEIGHT_SLICE, ENV_WIDTH_SLICE] # Reduce image size env_pxls = util.resize(env_pxls, (self.img_size)) # Normalize values env_pxls = env_pxls.astype('float32') / MAX_IMAGE_BRIGHT # Expand tensor (height, width, 1) tensor = np.expand_dims(env_pxls, axis=2) # Add previous states (height, width, num_prev_states + 1) for i in range(self.num_prev_states): tensor = np.append(tensor, self.prev_states[:, :, i:i + 1], axis=2) # Expand tensor (1, height, width, num_prev_states + 1) tensor = np.expand_dims(tensor, axis=0) # Save last observation self.last_obs = tensor else: # Normalize parameters self.last_obs[0] = (self.last_obs[0] + MAX_SHIFT) / (2 * MAX_SHIFT) self.last_obs[1] = (self.last_obs[1] + 2) / 4 self.last_obs[2] = (self.last_obs[2] + MAX_ANGLE) / (2 * MAX_ANGLE) self.last_obs[3] = (self.last_obs[3] + 2) / 4 return self.last_obs
""" Laplacian obtained using learned CellNN templates. """ from matplotlib import pyplot as plt from matplotlib import image as mpimg import numpy as np from scipy import ndimage from util import converge, rgb2gray # img = mpimg.imread("lena_std.tif") img = mpimg.imread("baboon.jpg") img_gray = rgb2gray(img) laplace_template = np.array([[-1, -1, -1], [-1, 8, -1], [-1, -1, -1]]) target = ndimage.convolve(img_gray / np.abs(img_gray).max(), laplace_template) target = target / (np.abs(target).max()) def train(): try: npzfile = np.load("laplace.npz") rmses = npzfile["rmses"].tolist() A = npzfile["A"] B = npzfile["B"] I = npzfile["I"] except Exception: rmses = [] A = np.zeros((3, 3)) B = np.zeros((3, 3))
k_neighbors.append(cube) haar_neighbors = haar_2d(k_neighbors, haar_matrix, inv_haar_matrix, noise_sigma=noise_sigma, gamma=gamma, enhance=enhance) for index, neighbor in enumerate(neighbors): weight[i+neighbor[0]:i+neighbor[0]+cube_size,j+neighbor[1]:j+neighbor[1]+cube_size] += 1 tmp_img[i+neighbor[0]:i+neighbor[0]+cube_size,j+neighbor[1]:j+neighbor[1]+cube_size] += haar_neighbors[index] weight[weight==0] = 1 haar_img = tmp_img / weight return haar_img if __name__=="__main__": #img_file = './bm3d_demos/image_Lena512rgb.png' #img_file = './data/filter_test/slice_4.png' img_file = './data/filter_test/Lena-original-gray.png' img_rgb = np.array(Image.open(img_file)) img = rgb2gray(img_rgb) if img_rgb.ndim > 2 else img_rgb #plt.imshow(img, cmap='gray') #plt.show() img = np.array(img, dtype=np.float32) img = img / np.amax(img) sigmas = estimate_sigma(img, multichannel=False) noise_sigma = np.mean(sigmas) print(noise_sigma) img_haar = nonlocal_haar(img, noise_sigma=noise_sigma*0.25, gamma=3) #img_haar_2 = nonlocal_haar(img, noise_sigma=noise_sigma*0.15, gamma=5) fig, axes = plt.subplots(1) diff = img_haar - img axes.imshow(np.concatenate((img/np.amax(img), img_haar/np.amax(img_haar)), axis=1), cmap='gray') #axes[0].imshow(img, cmap='gray') #axes[1].imshow(img_haar, cmap='gray')
import matplotlib.pyplot as plt DATA_DIR = "C:/_DATA/autonomous-driving-nd/Adv_Lane_finding" orig = u.rgb_read(DATA_DIR + '/color-shadow-example.jpg') #%% Preproc sobel_kernel = 7 hls = cv2.cvtColor(orig, cv2.COLOR_RGB2HLS) l_channel = hls[:, :, 1] s_channel = hls[:, :, 2] gray = u.rgb2gray(orig) sobelx = cv2.Sobel(l_channel, cv2.CV_32F, 1, 0, ksize=sobel_kernel) sobely = cv2.Sobel(l_channel, cv2.CV_32F, 0, 1, ksize=sobel_kernel) s_sobelx = cv2.Sobel(s_channel, cv2.CV_32F, 1, 0, ksize=sobel_kernel) s_sobely = cv2.Sobel(s_channel, cv2.CV_32F, 0, 1, ksize=sobel_kernel) #sobelx = cv2.Sobel(s_channel, cv2.CV_32F, 1, 0, ksize=sobel_kernel) #sobely = cv2.Sobel(s_channel, cv2.CV_32F, 0, 1, ksize=sobel_kernel) abs_sob_x = np.abs(sobelx) abs_sob_y = np.abs(sobely) scaled_x = np.uint8(255 * abs_sob_x / abs_sob_x.max())
k2 = params['k2'] r0 = params['r0'] cx = params['cx'] cy = params['cy'] north = params['north'] deltatetha = params['deltatetha'] coordinatesx = np.cos(north + theta_coordinates) * r0 + cx coordinatesy = np.sin(north + theta_coordinates) * r0 + cy list_of_image = glob.glob("current*.JPG") for fnimg in list_of_image: im = scipy.ndimage.imread(fnimg) ar = np.array(im) ar = util.rgb2gray(ar) fig = plt.figure() ax = fig.add_subplot(111) plt.imshow(ar, interpolation="nearest", cmap = plt.get_cmap("Greys_r")) northx, northy = util.get_image_coordinates(np.deg2rad(0), np.deg2rad(24)) eastx, easty = util.get_image_coordinates(np.deg2rad(90), np.deg2rad(20)) ax.annotate('N', xy=(northx, northy), rotation=deltatetha, horizontalalignment='center', verticalalignment='center') ax.annotate('E', xy=(eastx, easty), rotation=deltatetha, horizontalalignment='center', verticalalignment='center')
k2 = params['k2'] r0 = params['r0'] cx = params['cx'] cy = params['cy'] north = params['north'] deltatetha = params['deltatetha'] coordinatesx = np.cos(north + theta_coordinates) * r0 + cx coordinatesy = np.sin(north + theta_coordinates) * r0 + cy list_of_image = glob.glob("current*.JPG") for fnimg in list_of_image: im = scipy.ndimage.imread(fnimg) ar = np.array(im) ar = util.rgb2gray(ar) fig = plt.figure() ax = fig.add_subplot(111) plt.imshow(ar, interpolation="nearest", cmap=plt.get_cmap("Greys_r")) northx, northy = util.get_image_coordinates(np.deg2rad(0), np.deg2rad(24)) eastx, easty = util.get_image_coordinates(np.deg2rad(90), np.deg2rad(20)) ax.annotate('N', xy=(northx, northy), rotation=deltatetha, horizontalalignment='center', verticalalignment='center')
from util import converge, rgb2gray import os img = [] img_gray = [] target = [] train_dir = os.path.join(os.getcwd(), "edge_detection/training/images") train_result_dir = os.path.join(os.getcwd(), "edge_detection/training/1st_manual") for file in os.listdir(train_dir): print(file) file = os.path.join(train_dir, file) img.append(mpimg.imread(file)) img_gray.append(rgb2gray(img[-1])) for file in os.listdir(train_result_dir): print(file) file = os.path.join(train_result_dir, file) target.append(mpimg.imread(file)) def train(): try: npzfile = np.load("edge.npz") rmses = npzfile["rmses"].tolist() A = npzfile["A"] B = npzfile["B"] I = npzfile["I"] except Exception:
fold = folderName + '{}/'.format(i) else: if(i==0): fold = folderName + 'cat/' else: fold = folderName + 'dog/' imgNo = 0 files = fileName[i] filenames = files[itrNo*NoImages:(itrNo+1)*NoImages] for imgName in filenames: imgName = fold+imgName imgNo += 1 if(dataset == 'Cat-Dog'): img = util.rgb2gray(mpimg.imread(imgName)) else: img = mpimg.imread(imgName) inputs[imgNo,:] = img.reshape(inSize) labels[imgNo,:] = labMat[i] #itrNo += 1 print(inputs.shape) model.fit(inputs, labels, epochs=20, batch_size=1,verbose=0) itrNo = itrNo+1 for i in range(numClass): if(dataset == 'MNIST'): fold = folderName + '{}/'.format(i) else: if(i==0): fold = folderName + 'cat/'
def trainAndtest(args): print('Training and testing in progress...') if(args.dataset != 'MNIST' and args.dataset != 'Cat-Dog'): print('Please provide valid dataset (MNIST / Cat-Dog)') exit(0) if(args.configuration == None): print('Please provide configuration of the network') s = args.configuration[1:-1] s1 = s.split(' ') activation = 'sigmoid' numLayers = len(s1)#3 numNodes = np.zeros(numLayers, dtype=int) for i in range(numLayers): numNodes[i] = int(s1[i]) if(args.dataset == 'MNIST'): numClass = 10 inSize = 28*28 NoImages = 0 for i in range(10): folderName = args.train + '/' folderName = folderName + '{}/'.format(i) NoImages += len(os.listdir(folderName)) #maxImage = 30 #NoImages = maxImage*10 inputs = np.zeros((NoImages,inSize)) labMat = np.eye(numClass,numClass) labels = np.zeros((NoImages,numClass)) itrNo = 0 for i in range(numClass): folderName = args.train + '/' imgNo = 0 folderName = folderName + '{}/'.format(i) for imgName in os.listdir(folderName): imgName = folderName+imgName imgNo += 1 #if(imgNo<=maxImage): img = mpimg.imread(imgName) inputs[itrNo,:] = img.reshape(28*28) labels[itrNo,:] = labMat[i] itrNo += 1 if(args.dataset == 'Cat-Dog'): numClass = 2 inSize = 200*200 NoImages = 0 for i in range(numClass): folderName = args.train + '/' if (i==0): folderName = folderName + 'cat/' else: folderName = folderName + 'dog/' NoImages += len(os.listdir(folderName)) #maxImage = 2 #NoImages = maxImage*numClass inputs = np.zeros((NoImages,inSize)) labMat = np.eye(numClass,numClass) labels = np.zeros((NoImages,numClass)) itrNo = 0 for i in range(numClass): folderName = args.train + '/' imgNo = 0 if (i==0): folderName = folderName + 'cat/' else: folderName = folderName + 'dog/' for imgName in os.listdir(folderName): imgName = folderName+imgName imgNo += 1 #if(imgNo<=maxImage): img = util.rgb2gray(mpimg.imread(imgName)) inputs[itrNo,:] = img.reshape(inSize) labels[itrNo,:] = labMat[i] itrNo += 1 in_train, in_test, label_train, label_test = train_test_split(inputs, labels, test_size=0.1) scaler = StandardScaler() scaler.fit(in_train) in_train = scaler.transform(in_train) in_test = scaler.transform(in_test) if(numNodes[0] != inSize): print('Input neurons specified does not match image size') exit(0) if(numNodes[-1] != numClass): print('Output neurons specified does not match number of classes') exit(0) network = mlp.MLNN() network.initializeStruct(numLayers,numNodes,activation) network.initializeWeights() error, predicted = network.train(in_train,label_train) saveFileName = '../Model/' + args.dataset + '.npy' network.saveModel(saveFileName) network.forwardProp(in_train) predicted = network.predict(network.nnLayers[numLayers-1].output) accuracy = util.getAccuracy(np.argmax(label_train,axis=1),np.argmax(predicted,axis=1)) f1_macro,f1_micro = util.fi_macro_micro(np.argmax(label_train,axis=1),np.argmax(predicted,axis=1), numClass) print('PERFORMANCE ON TRAINING DATA:') print(accuracy, f1_macro, f1_micro) network.forwardProp(in_test) predicted = network.predict(network.nnLayers[numLayers-1].output) accuracy = util.getAccuracy(np.argmax(label_test,axis=1),np.argmax(predicted,axis=1)) f1_macro,f1_micro = util.fi_macro_micro(np.argmax(label_test,axis=1),np.argmax(predicted,axis=1), numClass) print('PERFORMANCE ON VALIDATION DATA') print(accuracy, f1_macro, f1_micro) NoImages = len(os.listdir(args.test)) inputs = np.zeros((NoImages,inSize)) itrNo = 0 for imgName in os.listdir(args.test): imgName = args.test+ '/' + imgName if(args.dataset == 'Cat-Dog'): img = util.rgb2gray(mpimg.imread(imgName)) else: img = mpimg.imread(imgName) inputs[itrNo,:] = img.reshape(inSize) itrNo += 1 network.forwardProp(inputs) predict = network.predict(network.nnLayers[numLayers-1].output) predicted = np.argmax(predict,axis=1) print(predicted)
def my_fill_depth_colorization(color, depth, centroid_mask, penalty=1): """ Preprocesses the kinect depth image using a gray scale version of the RGB image as a weighting for the smoothing. This code is a slight adaptation of Anat Levin's colorization code: http://www.cs.huji.ac.il/~yweiss/Colorization/ Args: color - HxWx3 matrix; the RGB image, with each component in [0,1]. depth - HxW matrix; a depth image in absolute (meters) space. penalty - a penalty value between 0 and 1 for the current depth values. Returns: A denoised depth image. """ assert len(depth.shape) == 2 maxImgAbsDepth = np.amax(depth[centroid_mask]) depth = depth / maxImgAbsDepth depth[depth > 1] = 1 #-depth[np.where(depth > 1)] = 1 [H, W] = depth.shape numPix = H * W indsM = np.arange(numPix, dtype=int).reshape((H, W)) grayImg = rgb2gray(color) winRad = 1 leng = 0 absImgNdx = 0 cols = np.zeros( (numPix * (2 * winRad + 1)**2, )) # TODO: may need to remove the +1 rows = np.zeros( (numPix * (2 * winRad + 1)**2, )) # TODO: may need to remove the +1 vals = np.zeros( (numPix * (2 * winRad + 1)**2, )) # TODO: may need to remove the +1 gvals = np.zeros( ((2 * winRad + 1)**2, )) # TODO: may need to remove the +1 for j in range(W): for i in range(H): absImgNdx = absImgNdx + 1 # Count the number of points in the current window. nWin = 0 for ii in range(max(0, i - winRad), min(i + winRad + 1, H)): for jj in range(max(0, j - winRad), min(j + winRad + 1, W)): if ii == i and jj == j: continue leng += 1 nWin += 1 rows[leng] = absImgNdx cols[leng] = indsM[ii, jj] g = grayImg[ii, jj] gvals[nWin] = g curVal = grayImg[i, j] gvals[nWin] = curVal c_var = np.mean(np.square(gvals[0:nWin] - np.mean(gvals[0:nWin]))) min_sq_gval = np.amin(np.square(gvals[0:nWin] - curVal)) csig = max(c_var * 0.6, 0.000002, -min_sq_gval / math.log(0.01)) gvals[0:nWin] = np.exp(-np.square(gvals[0:nWin] - curVal) / csig) gvals[0:nWin] = gvals[0:nWin] / np.sum(gvals[0:nWin]) vals[leng - nWin:leng] = -gvals[0:nWin] # Now the self-reference (along the diagonal). leng += 1 rows[leng] = absImgNdx cols[leng] = absImgNdx vals[leng] = 1 #- sum(gvals[0:nWin]) rows = rows[0:leng] cols = cols[0:leng] vals = vals[0:leng] A = sparse(rows, cols, vals, numPix + 1, numPix + 1) print(A.shape) rows = range(centroid_mask.size) cols = range(centroid_mask.size) vals = penalty * centroid_mask.flatten() G = sparse(rows, cols, vals, numPix + 1, numPix + 1) print(G.shape) y = (vals.flatten() * depth.flatten()) y = np.vstack((y.reshape((-1, 1)), np.array([[1]]))) new_vals = scipy.sparse.linalg.lsqr((A + G), y)[:1] new_vals = np.array(new_vals, dtype='float') new_vals = new_vals.T new_vals = new_vals[:-1] return np.reshape(new_vals, (H, W)) * maxImgAbsDepth