我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用matplotlib.image.imread()。
def get_points(): # prepare object points, like (0,0,0), (1,0,0), (2,0,0) ....,(6,5,0) objp = np.zeros((6*8,3), np.float32) objp[:,:2] = np.mgrid[0:8, 0:6].T.reshape(-1 , 2) # Arrays to store object points and image points from all the images. objpoints = [] # 3d points in real world space imgpoints = [] # 2d points in image plane. # Make a list of calibration images images = glob.glob('calibration_wide/GO*.jpg') # Step through the list and search for chessboard corners for idx, fname in enumerate(images): img = cv2.imread(fname) gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) # Find the chessboard corners ret, corners = cv2.findChessboardCorners(gray, (8,6), None) # If found, add object points, image points if ret == True: objpoints.append(objp) imgpoints.append(corners) # Draw and display the corners cv2.drawChessboardCorners(img, (8,6), corners, ret) #write_name = 'corners_found'+str(idx)+'.jpg' #cv2.imwrite(write_name, img) cv2.imshow('img', img) cv2.waitKey(500) cv2.destroyAllWindows() return objpoints, imgpoints
def main(): # reading in an image #image = (mpimg.imread('test_images/solidWhiteRight.jpg') * 255).astype('uint8') #image = (mpimg.imread('test_images/solidWhiteCurve.jpg') * 255).astype('uint8') #image = (mpimg.imread('test_images/solidYellowCurve.jpg') * 255).astype('uint8') #image = (mpimg.imread('test_images/solidYellowCurve2.jpg') * 255).astype('uint8') #image = (mpimg.imread('test_images/solidYellowLeft.jpg') * 255).astype('uint8') image = (mpimg.imread('test_images/whiteCarLaneSwitch.jpg') * 255).astype('uint8') processImage = process_image(image) plt.imshow(processImage) plt.show() # Make video white_output = 'white.mp4' clip1 = VideoFileClip("solidWhiteRight.mp4") white_clip = clip1.fl_image(process_image) #NOTE: this function expects color images!! white_clip.write_videofile(white_output, audio=False) # Make video yellow_output = 'yellow.mp4' clip2 = VideoFileClip('solidYellowLeft.mp4') yellow_clip = clip2.fl_image(process_image) yellow_clip.write_videofile(yellow_output, audio=False)
def save_rotated_test_images(): # # DESCRIPTION # This function rotates the test image and create four patches of 400 * 400 # It then saves those 32 images in the test_set_images folder of each image # # # Loop over all images for i in range(1,51): # Load image image = mpimg.imread('test_set_images/test_'+str(i)+'/test_'+str(i)+'.png') rotations = mk_rotations(image) rota_count = 0 for rotation in rotations: patches = make_4_patch(rotation) patch_count = 0 for patch in patches: patch = format_image(patch) Image.fromarray(patch).save('test_set_images/test_'+str(i)+'/Test_'+str(i)+'_rota'+str(rota_count)+'_patch'+str(patch_count)+'.png') patch_count += 1 rota_count+=1 print('Writing image ',i)
def extract_labels(filename, num_images): gt_imgs = [] for i in range(1, num_images+1): imageid = 'training_big/Truth/satImage_'+ '%.3d' % i for j in range(8): image_filename = imageid + "_rota"+str(np.int(j))+".png" if os.path.isfile(image_filename): img = mpimg.imread(image_filename) gt_imgs.append(img) else: print ('File ' + image_filename + ' does not exist') num_images = len(gt_imgs) gt_patches = [img_crop(gt_imgs[i], IMG_PATCH_SIZE, IMG_PATCH_SIZE) for i in range(num_images)] data = np.asarray([gt_patches[i][j] for i in range(len(gt_patches)) for j in range(len(gt_patches[i]))]) labels = np.asarray([value_to_class(np.mean(data[i])) for i in range(len(data))]) # Convert to dense 1-hot representation. return labels.astype(np.float32) ##Return the error rate based on dense predictions and 1-hot labels.
def load_stereo_pairs(imL_files, imR_files, **kwargs): """Helper method to read stereo image pairs.""" StereoPair = namedtuple('StereoPair', 'left, right') impairs = [] for imfiles in zip(imL_files, imR_files): # Convert to uint8 and BGR for OpenCV if requested imformat = kwargs.get('format', '') if imformat is 'cv2': imL = np.uint8(mpimg.imread(imfiles[0]) * 255) imR = np.uint8(mpimg.imread(imfiles[1]) * 255) # Convert RGB to BGR if len(imL.shape) > 2: imL = imL[:, :, ::-1] imR = imR[:, :, ::-1] else: imL = mpimg.imread(imfiles[0]) imR = mpimg.imread(imfiles[1]) impairs.append(StereoPair(imL, imR)) return impairs
def create_thumbnail(infile, thumbfile, width=300, height=300, cx=0.5, cy=0.5, border=4): baseout, extout = op.splitext(thumbfile) im = image.imread(infile) rows, cols = im.shape[:2] x0 = int(cx * cols - .5 * width) y0 = int(cy * rows - .5 * height) xslice = slice(x0, x0 + width) yslice = slice(y0, y0 + height) thumb = im[yslice, xslice] thumb[:border, :, :3] = thumb[-border:, :, :3] = 0 thumb[:, :border, :3] = thumb[:, -border:, :3] = 0 dpi = 100 fig = plt.figure(figsize=(width / dpi, height / dpi), dpi=dpi) ax = fig.add_axes([0, 0, 1, 1], aspect='auto', frameon=False, xticks=[], yticks=[]) ax.imshow(thumb, aspect='auto', resample=True, interpolation='bilinear') fig.savefig(thumbfile, dpi=dpi) return fig
def graphviz_plot(graph, fname="tmp_dotgraph.dot", show=True): if os.path.exists(fname): print("WARNING: Overwriting existing file {} for new plots".format(fname)) f = open(fname,'w') f.writelines('digraph G {\nnode [width=.3,height=.3,shape=octagon,style=filled,color=skyblue];\noverlap="false";\nrankdir="LR";\n') for i in graph: for j in graph[i]: s= ' '+ i s += ' -> ' + j + ' [label="' + str(graph[i][j]) + '"]' s+=';\n' f.writelines(s) f.writelines('}') f.close() graphname = fname.split(".")[0] + ".png" pe(["dot", "-Tpng", fname, "-o", graphname]) if show: plt.imshow(mpimg.imread(graphname)) plt.show()
def exportPlot(self): # Combine to one self.plot_output_file, filetype = QtWidgets.QFileDialog.getSaveFileName(self, "Export Plots To...", "", "PDF(*.pdf)") if filetype == "PDF(*.pdf)": pdf = matplotlib.backends.backend_pdf.PdfPages(self.plot_output_file) for key in list(self.plot_file.keys()): for i in range(1, len(self.plot_file[key])): fig = plt.figure() img = mpimg.imread(self.plot_file[key][i]) plt.imshow(img) plt.axis('off') pdf.savefig(fig) plt.clf() plt.close() pdf.close() sys.stdout.write("Output plot files to %s"%(self.plot_output_file)) self.exportPlot_flag = 1
def run(self): if self.args is None: args = [] else: args = self.args if self.kwargs is None: kwargs = {} else: kwargs = self.kwargs comp_imgs = [] tmpdir = tempfile.mkdtemp() image_prefix = os.path.join(tmpdir,"test_img") self.image_func(image_prefix, *args, **kwargs) imgs = glob.glob(image_prefix+"*") assert(len(imgs) > 0) for img in imgs: img_data = mpimg.imread(img) os.remove(img) comp_imgs.append(zlib.compress(img_data.dumps())) return comp_imgs
def imscatter(x, y, image, ax=None, zoom=1): if ax is None: ax = plt.gca() try: image = plt.imread(image) except TypeError: # Likely already an array... pass im = OffsetImage(image, zoom=zoom) x, y = np.atleast_1d(x, y) artists = [] for x0, y0 in zip(x, y): ab = AnnotationBbox(im, (x0, y0), xycoords='data', frameon=False) artists.append(ax.add_artist(ab)) ax.update_datalim(np.column_stack([x, y])) ax.autoscale() return artists
def plot(error_index, dataset_path): img = mpimg.imread(dataset_path) plt.imshow(img) currentAxis = plt.gca() for index in error_index: row = index // 2 column = index % 2 currentAxis.add_patch( patches.Rectangle( xy=( 47 * 9 if column == 0 else 47 * 19, row * 57 ), width=47, height=57, linewidth=1, edgecolor='r', facecolor='none' ) ) fig = plt.gcf() fig.set_size_inches(11.40, 9.42) plt.savefig("fig_result.png", bbox_inches="tight", dpi=100) plt.show()
def print_images(output, img_path): seg_path = img_path.replace("jpg", "png") out_img = np.uint8(np.squeeze(output.asnumpy().argmax(axis=1))) out_img = Image.fromarray(out_img) out_img.putpalette(getpallete(256)) out_img.save(seg_path) # Display input print "Input Image:" img = mpimg.imread(img_path) plt.imshow(img) plt.show() # Display output print "Output Image:" img_out = mpimg.imread(seg_path) plt.imshow(img_out) plt.show()
def by_file_name(self, image_file_name): """ :param image_file_name: :return: image from file (or from cache) empty image if image_file_name empty or file does not exists """ if not image_file_name: return self._empty if image_file_name not in self._cache: try: self._cache[image_file_name] = mpimg.imread(image_file_name) except Exception as e: print('#'*5, ' Error reading image from {}:\n{}'.format(image_file_name, e)) return self._empty return self._cache[image_file_name]
def extract_data(filename, num_images): """Extract the images into a 4D tensor [image index, y, x, channels]. Values are rescaled from [0, 255] down to [-0.5, 0.5]. """ imgs = [] for i in range(1, num_images + 1): imageid = "satImage_%.3d" % i image_filename = filename + imageid + ".png" if os.path.isfile(image_filename): print('Loading ' + image_filename) img = mpimg.imread(image_filename) imgs.append(img) else: print('File ' + image_filename + ' does not exist') num_images = len(imgs) img_patches = [img_crop(imgs[i], IMG_PATCH_SIZE, IMG_PATCH_SIZE) for i in range(num_images)] data = [img_patches[i][j] for i in range(len(img_patches)) for j in range(len(img_patches[i]))] return numpy.asarray(data) # Assign a label to a patch v
def extract_labels(filename_base, num_images, num_of_transformations=6, patch_size=const.IMG_PATCH_SIZE, patch_stride=const.IMG_PATCH_STRIDE): """Extract the labels into a 1-hot matrix [image index, label index].""" gt_imgs = [] for i in range(1, num_images+1): imageid = "satImage_%.3d" % i image_filename = filename_base + imageid + ".png" if os.path.isfile(image_filename): print('Loading ' + image_filename) img = mpimg.imread(image_filename) gt_imgs.append(img) else: print('File ' + image_filename + ' does not exist') num_images = len(gt_imgs) print('Extracting patches...') gt_patches = [pem.label_img_crop(gt_imgs[i], patch_size, patch_stride, num_of_transformations) for i in range(num_images)] data = np.asarray([gt_patches[i][j] for i in range(len(gt_patches)) for j in range(len(gt_patches[i]))]) labels = np.asarray([value_to_class(np.mean(data[i])) for i in range(len(data))]) print(str(len(data)) + ' label patches extracted.') # Convert to dense 1-hot representation. return labels.astype(np.float32)
def extract_label_images(filename_base, num_images, patch_size=const.IMG_PATCH_SIZE, patch_stride=const.IMG_PATCH_STRIDE, img_base_name="satImage_%.3d"): """Extract labels from ground truth as label images.""" gt_imgs = [] for i in range(1, num_images+1): imageid = img_base_name % i image_filename = filename_base + imageid + ".png" if os.path.isfile(image_filename): print('Loading ' + image_filename) img = mpimg.imread(image_filename) gt_imgs.append(img) else: print('File ' + image_filename + ' does not exist') num_images = len(gt_imgs) print('Extracting patches...') gt_patches = [pixel_to_patch_labels(gt_imgs[i], patch_size, patch_stride) for i in range(num_images)] return gt_patches
def filter_bad_images(): index = 0 gt_index = arguments.gt_start predicted_index = arguments.predicted_start indices = [] filter_path = search(predicted_index) filter_filename = os.path.join(arguments.gt_path, "filter_close.txt") if os.path.exists(filter_filename): with open(filter_filename, "r") as filter_file: filter_close_indices = [int(line.strip()) for line in filter_file.readlines()] else: filter_close_indices = [] for image_index in range(arguments.samples): rgb = mpimg.imread(os.path.join(filter_path, arguments.filter_format.format(predicted_index))) if np.median(rgb) > 5 and gt_index not in filter_close_indices: indices.append((index, gt_index, predicted_index)) index += 1 gt_index += 1 predicted_index += 1 return indices
def read_file(filename, shape = None): if filename.lower().endswith(".exr"): depth_map = read_depth(filename) return depth_map, depth_map < 1000.0 elif filename.lower().endswith(".png"): depth_map = mpimg.imread(filename) if shape is not None: ih, iw = depth_map.shape h, w = shape if ih > 1024: depth_map = depth_map[::2, ::2] depth_map = zoom(depth_map, [float(h) / float(ih), w / float(iw)], order = 1) mask = depth_map < 0.99 depth_map = depth_map * 65536 / 1000 return depth_map, mask elif filename.lower().endswith(".npy"): return np.load(filename), None
def predict_image(self, test_img): """ predicts classes of input image :param test_img: filepath to image to predict on :return: segmented result """ # imgs = io.imread(test_img).astype('float').reshape(5, 216, 160) imgs = mpimg.imread(test_img).astype('float') imgs = rgb2gray(imgs).reshape(5, 216, 160) plist = [] # create patches_to_predict from an entire slice for img in imgs[:-1]: if np.max(img) != 0: img /= np.max(img) p = extract_patches_2d(img, (33, 33)) plist.append(p) patches_to_predict = np.array( zip(np.array(plist[0]), np.array(plist[1]), np.array(plist[2]), np.array(plist[3]))) # predict classes of each pixel based on model full_pred = self.model.predict_classes(patches_to_predict) fp1 = full_pred.reshape(184, 128) return fp1
def ret_val(): if os.path.isfile('./classifier_full.pickle'): f = open('classifier_full.pickle', 'rb') clf = pickle.load(f) digit_loc = get_image_src2() digit_image = mpimg.imread(digit_loc) gray_digit = np.dot(digit_image[...,:3], [0.299, 0.587, 0.114]) digit_display = gray_digit gray_digit = gray_digit.flatten() for i in range(len(gray_digit)): gray_digit[i] = 1.0 - gray_digit[i] gray_digit[i] = round(gray_digit[i], 8) return str(int(clf.predict([gray_digit])[0]))
def recolor_image(input_file, k=5): img = mpimg.imread(path_to_png_file) pixels = [pixel for row in img for pixel in row] clusterer = KMeans(k) clusterer.train(pixels) # this might take a while def recolor(pixel): cluster = clusterer.classify(pixel) # index of the closest cluster return clusterer.means[cluster] # mean of the closest cluster new_img = [[recolor(pixel) for pixel in row] for row in img] plt.imshow(new_img) plt.axis('off') plt.show() # # hierarchical clustering #
def fetch_image(i=0): #reading in an image from test images = ['test_images/solidWhiteCurve.jpg', 'test_images/solidWhiteRight.jpg', 'test_images/solidYellowCurve.jpg', 'test_images/solidYellowCurve2.jpg', 'test_images/solidYellowLeft.jpg', 'test_images/whiteCarLaneSwitch.jpg', 'test_images/challenge/test-14.jpg'] if(i > 6): i = 6 if(i<0): i=0 image = mpimg.imread(images[i]) return image # In[120]:
def testClassifier(): clf = pickle.load(open("classifier.p", "rb")) classes = numpy.loadtxt(dataset_root + 'classes.txt', dtype=str) classes = column(classes, 0) image_files = sorted(listdir( join(project_root, test_images_dir))) for image in image_files: features = extractFeatures( str(abspath(join(project_root, test_images_dir, image)))) prediction = clf.predict(features) img = mpimg.imread( str(abspath(join(project_root, test_images_dir, image)))) fig = plt.figure() fig.suptitle(classes[int(prediction[0])], fontsize=14, fontweight='bold') plt.imshow(img)
def read_3channel_images(image_filename, num_images, file_regex): images = [] for i in range(1, num_images + 1): imageid = file_regex % i filename = image_filename + imageid + ".png" if os.path.isfile(filename): print('Loading ' + filename) img = mpimg.imread(filename) tmp = np.array(img) if len(tmp.shape) == 3: img = img[:, :, :3] images.append(img) else: print('File ' + filename + ' does not exist') return np.array(images)
def tile_ims(filename, directory): """Load all images in the given directory and tile them into one.""" ims = [mpimg.imread(os.path.join(directory, f)) for f in sorted(os.listdir(directory))] ims = np.array(ims) ims = ims.transpose((0, 3, 1, 2)) # (n, h, w, c) -> (n, c, h ,w) save_ims(filename, ims)
def read_from_file(self, filepathname, normalize = True): # import image from file # todo warnings about file existing img = mpimg.imread(filepathname) img = MyRGBImg(img) if img.data.shape[2] == 4: colors = [] for i in range(3): channel = img.get_channel(i) colors.append(channel) # initializate the image myimage = MyRGBImg(data = np.zeros((img.data.shape[0], img.data.shape[1], 3))) for i in range(3): channel = colors[i] channel.data = np.transpose(channel.data) myimage.set_channel(channel, i) self.data = myimage.data else: self.data = img.data if normalize: self.limit(1)
def read_from_file(self, filepathname): ''' import image from file using the mpimg utility of matplotlib''' # todo warnings about file existing ? self.data = mpimg.imread(filepathname)
def draw(self, figure): """ Draw watermark Parameters ---------- figure : Matplotlib.figure.Figure Matplolib figure on which to draw """ X = mimage.imread(self.filename) figure.figimage(X, **self.kwargs)
def data_look(car_list, notcar_list): data_dict = {} # Define a key in data_dict "n_cars" and store the number of car images data_dict["n_cars"] = len(car_list) # Define a key "n_notcars" and store the number of notcar images data_dict["n_notcars"] = len(notcar_list) # Read in a test image, either car or notcar test_img = mpimg.imread(car_list[0]) # Define a key "image_shape" and store the test image shape 3-tuple data_dict["image_shape"] = test_img.shape # Define a key "data_type" and store the data type of the test image. data_dict["data_type"] = test_img.dtype # Return data_dict return data_dict
def main(): images = glob.glob('*.jpeg') cars = [] notcars = [] for image in images: if 'image' in image or 'extra' in image: notcars.append(image) else: cars.append(image) data_info = data_look(cars, notcars) # Just for fun choose random car / not-car indices and plot example images car_ind = np.random.randint(0, len(cars)) notcar_ind = np.random.randint(0, len(notcars)) # Read in car / not-car images car_image = mpimg.imread(cars[car_ind]) notcar_image = mpimg.imread(notcars[notcar_ind]) # Plot the examples fig = plt.figure() plt.subplot(121) plt.imshow(car_image) plt.title('Example Car Image') plt.subplot(122) plt.imshow(notcar_image) plt.title('Example Not-car Image') plt.show()
def find_matches(img, template_list): # Make a copy of the image to draw on # Define an empty list to take bbox coords bbox_list = [] # Iterate through template list # Read in templates one by one # Use cv2.matchTemplate() to search the image # using whichever of the OpenCV search methods you prefer # Use cv2.minMaxLoc() to extract the location of the best match # Determine bounding box corners for the match # Return the list of bounding boxes method = cv2.TM_CCOEFF_NORMED for temp in templist: tmp = mpimg.imread(temp) # Apply template Matching res = cv2.matchTemplate(img,tmp,method) min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(res) w, h = (tmp.shape[1], tmp.shape[0]) # If the method is TM_SQDIFF or TM_SQDIFF_NORMED, take minimum if method in [cv2.TM_SQDIFF, cv2.TM_SQDIFF_NORMED]: top_left = min_loc else: top_left = max_loc bottom_right = (top_left[0] + w, top_left[1] + h) bbox_list.append((top_left, bottom_right)) return bbox_list
def ColorSelector(): # Read in the image and print out some stats image = (mpimg.imread('test.png') * 255).astype('uint8') print('This image is: ', type(image), 'with dimensions:', image.shape) # Grab the x and y size and make a copy of the image ysize = image.shape[0] xsize = image.shape[1] color_select = np.copy(image) # Define color selection criteria # MODIFY THESE VARIABLES TO MAKE YOUR COLOR SELECTION red_threshold = 200 green_threshold = 200 blue_threshold = 200 rgb_threshold = [red_threshold, green_threshold, blue_threshold] print('Esta es la variable rgb_threshold: ', rgb_threshold) # Do a bitwise or with the "|" character to identify # pixels below the thresholds thresholds = (image[:, :, 0] < rgb_threshold[0]) \ | (image[:, :, 1] < rgb_threshold[1]) \ | (image[:, :, 2] < rgb_threshold[2]) print('Esta es la variable thresholds: ', thresholds) color_select[thresholds] = [0, 0, 0] # plt.imshow(color_select) # Uncomment the following code if you are running the code # locally and wish to save the image mpimg.imsave("test-after.png", color_select) # Display the image plt.imshow(color_select) plt.show()
def compare_entropy(name_img1,name_img2,method="rmq"): '''Compare two images by the Kullback-Leibler divergence Parameters ---------- name_img1 : string filename of image 1 (png format) name_img2 : string filename of image 2 (png format) Returns ------- S : float Kullback-Leibler divergence S = sum(pk * log(pk / qk), axis=0) Note ---- See http://docs.scipy.org/doc/scipy/reference/generated/scipy.stats.entropy.html ''' img1 = mpimg.imread(name_img1) img2 = mpimg.imread(name_img2) fimg1 = img1.flatten() fimg2 = img2.flatten() if method == "KL-div": eps = 0.0001 S = stats.entropy(fimg2+eps,fimg1+eps) S = numpy.log10(S) elif method == "rmq": fdiff=fimg1-fimg2 fdiff_sqr = fdiff**4 S = (fdiff_sqr.sum())**(old_div(1.,4)) return S,fimg1, fimg2
def load_img(self, img_path): """ Return an image object that can be immediately plotted with matplotlib """ with open_file(self.uuid, img_path) as f: return mpimg.imread(f)
def mask_to_submission_strings(image_filename): """Reads a single image and outputs the strings that should go into the submission file""" img_number = int(re.search(r"\d+", image_filename).group(0)) im = mpimg.imread(image_filename) patch_size = 16 for j in range(0, im.shape[1], patch_size): for i in range(0, im.shape[0], patch_size): patch = im[i:i + patch_size, j:j + patch_size] label = patch_to_label(patch) yield("{:03d}_{}_{},{}".format(img_number, j, i, label))
def get_images(images_directory, groundtruths_directory, num_images): # # DESCRIPTION # Loads each training image and its ground truth and creates tensors [numImages, 400, 400, 3] # # INPUTS # images_directory path to training images directory # groundtruths_directory path to the groundtruth images directory # num_images number of images to load # # OUTPUTS # images, ground_truth two tensors # images = [] ground_truth = [] for i in num_images: image_id = "satImage_%.3d" % i image_filename = image_id + ".png" image_path = images_directory + image_filename; groundtruth_image_path = groundtruths_directory + image_filename; if ((os.path.isfile(image_path))&(os.path.isfile(groundtruth_image_path))): print ('Loading ' + image_filename) loaded_image = mpimg.imread(image_path) loaded_gt_image = mpimg.imread(groundtruth_image_path) if ordering == "th": loaded_image = np.rollaxis(loaded_image,2) images.append(loaded_image) ground_truth.append(loaded_gt_image) else: print ('File ' + image_path + ' does not exist') return images, ground_truth
def predict_batch_test_images (model, batch_size = 1, max_image = 50): # # DESCRIPTION # Generator function batching each of the 32 mapped image from one test image # Once the image have been loaded they are predicted using the specified Keras Model # Once predicted the generator finally yields the batch to be treated in a for loop in predict_and_rebuild # # INPUTS # model keras model # batch_size set to 1 # max_image the max number of image loaded (50 test set) # # OUTPUTS # yield predictions a np.array of [:, 400, 400, 1] # images = np.zeros(shape=[8*4, 400, 400, 3], dtype=float) for i in range(1,max_image+1): count = 0 for rota_count in range(8): for patch_count in range(4): images[count, :,:,:] = mpimg.imread('test_set_images/test_'+str(i)+'/Test_'+str(i)+'_rota'+str(rota_count)+'_patch'+str(patch_count)+'.png') count += 1 if (count == 32): preds = model.predict(images, batch_size = batch_size, verbose=1) yield preds
def rotate_training(): ## Saves rotated versions of each image in the training set niter = 10 k=5 for i in np.linspace(1,100,100): truth = mpimg.imread('training/groundtruth/satImage_'+ '%.3d' % i +'.png') image = mpimg.imread('training/images/satImage_'+ '%.3d' % i +'.png') imgs = mk_rotations(image) truths = mk_rotations(truth) count =0 for im in imgs: im = format_image(im) Image.fromarray(im).save('training_big/Images/satImage_'+ '%.3d' % i +'_rota'+str(np.int(count))+'.png') count+=1 count =0 for im in imgs: im = format_image(im) Image.fromarray(im).save('training_big/Truth/satImage_'+ '%.3d' % i +'_rota'+str(np.int(count))+'.png') count+=1 print('Writing image ',i) return 0 ##Generation (or not) of the extended training set
def extract_data(filename, num_images): imgs = [] stars = [] ridges = [] print("loading, please wait") for i in range(1, num_images+1): imageid = 'satImage_'+ '%.3d' % i ##Load images for j in range(8): image_filename = 'training_big/Images/' + imageid + "_rota"+str(np.int(j))+".png" if os.path.isfile(image_filename): img = mpimg.imread(image_filename) n1,n2,n = img.shape imgs.append(img.astype(np.float32, copy=False)) else: print ('File ' + image_filename + ' does not exist') ##Format images num_images = len(imgs) IMG_WIDTH = imgs[0].shape[0] IMG_HEIGHT = imgs[0].shape[1] N_PATCHES_PER_IMAGE = (IMG_WIDTH/IMG_PATCH_SIZE)*(IMG_HEIGHT/IMG_PATCH_SIZE) img_patches = [img_crop(imgs[i], IMG_PATCH_SIZE, IMG_PATCH_SIZE) for i in range(num_images)] data = [img_patches[i][j] for i in range(len(img_patches)) for j in range(len(img_patches[i]))] return np.asarray(data) # Assign a label to a patch v
def loadVideoFolder(foldername): N = len(os.listdir(foldername)) #Assume numbering starts at zero f0 = scipy.misc.imread("%s/%i.png"%(foldername, 0)) IDims = f0.shape dim = len(f0.flatten()) I = np.zeros((N, dim)) I[0, :] = np.array(f0.flatten(), dtype=np.float32)/255.0 for i in range(1, N): f = scipy.misc.imread("%s/%i.png"%(foldername, i)) I[i, :] = np.array(f.flatten(), dtype=np.float32)/255.0 return (I, IDims) #Output video #I: PxN video array, IDims: Dimensions of each frame
def load_rgb(data_path): im_path = os.path.join(data_path,'image_2','*.png') im_files = sorted(glob.glob(im_path)) im_all = [] #tic = time.time() for iter_files in im_files: if len(im_all)<100 : im = np.uint8(mpimg.imread(iter_files) * 255) im_all.append(im) print(im.shape) return im_all
def readfile(path): try: data = img.imread(path) return data except: print("Error reading: ", path) return np.array([])
def readfile(path): try: data = img.imread(path) return data except: return np.array([])
def displayimage(path): data = img.imread(path) plt.imshow(data) plt.show() return
def test_pre_process_image(): matched, debug_gt, debug_matched_default_box, dbg_def_boxes ,dbg_cells, dbg_imgs = pre_process_images("tiny/data.pkl",["img00090526.jpg"]) img = mpimg.imread("tiny/img00090526.jpg") debug_draw_boxes(img, dbg_cells["img00090526.jpg"], (255,255,255),1) debug_draw_boxes(img, debug_gt["img00090526.jpg"], (255,0,0),1) debug_draw_boxes(img, debug_matched_default_box["img00090526.jpg"], (1,255,1),2) debug_draw_boxes(img, dbg_def_boxes["img00090526.jpg"], (1,0,255),1) imgplot = plt.imshow(img) plt.show()
def test_process_image_set(dirname): matched, debug_gt, debug_matched_default_box, dbg_def_boxes ,dbg_cells,dbg_imgs = pre_process_images(dirname+"/data.pkl") for img_name in dbg_imgs: print("loading ... ",img_name) img = mpimg.imread(img_name) # debug_draw_boxes(img, dbg_cells[img_name], (255,255,255),1) debug_draw_boxes(img, debug_gt[img_name], (255,0,0),1) debug_draw_boxes(img, debug_matched_default_box[img_name], (1,255,1),1) # debug_draw_boxes(img, dbg_def_boxes[img_name], (1,0,255),1) imgplot = plt.imshow(img) plt.show()
def _get_image(self, fname): """ Args fname : filename of image Returns the image as array with dim (w,h,channels) """ img = imgs.get(fname) if img == None: img = mpimg.imread(fname) img = (img-128)/128 imgs[fname] = img return img
def predict_boxes(self,model_name="trained-model"): """ Given a directory containing the dataset and images_path show objects detected for a random image. Args - dirname: Name of directory containing meta data of training run. Returns - Nothing. Shows the pic with detections. """ images_path = self.cfg.g("images_path") train_imgs = pickle.load(open(self.dirname+"/train.pkl","rb")) image_info = random.choice(list(train_imgs)) image_name = image_info['img_name'] p_conf, p_loc, p_probs = self.run_inference(image_name,model_name) non_zero_indices = np.where(p_conf > 0)[1] # DEBUGGING print("p_conf={} p_loc={} p_probs={}".format(p_conf.shape,p_loc.shape,p_probs.shape)) print("Non zero indices",non_zero_indices) for i in non_zero_indices: print(i,") location",p_loc[0][i*4:i*4+4],"probs", p_probs[0][i],"conf",p_conf[0][i]) boxes, confs = self.convert_coordinates_to_boxes(p_loc,p_conf,p_probs) print("Boxes BEFORE NMS") for i,a in enumerate(zip(boxes,confs)): print(i,a) boxes = non_max_suppression_fast(boxes,0.3) print("Boxes AFTER NMS") print(boxes) img = mpimg.imread(images_path+"/"+image_name) self.debug_draw_boxes(img,boxes,(0,255,0),2) plt.figure(figsize=(8,8)) plt.imshow(img) plt.show()