def set_image(self, params, images, background): """ creates a strip with the cell in different images images is a list of rgb images background is a grayscale image to use for the masks """ x0, y0, x1, y1 = self.box img = color.gray2rgb(np.zeros((x1 - x0 + 1, (len(images) + 4) * (y1 - y0 + 1)))) bx0 = 0 bx1 = x1 - x0 + 1 by0 = 0 by1 = y1 - y0 + 1 for im in images: img[bx0:bx1, by0:by1] = im[x0 : x1 + 1, y0 : y1 + 1] by0 = by0 + y1 - y0 + 1 by1 = by1 + y1 - y0 + 1 perim = self.perim_mask axial = self.sept_mask cyto = self.cyto_mask img[bx0:bx1, by0:by1] = color.gray2rgb(background[x0 : x1 + 1, y0 : y1 + 1] * self.cell_mask) by0 = by0 + y1 - y0 + 1 by1 = by1 + y1 - y0 + 1 img[bx0:bx1, by0:by1] = color.gray2rgb(background[x0 : x1 + 1, y0 : y1 + 1] * perim) by0 = by0 + y1 - y0 + 1 by1 = by1 + y1 - y0 + 1 img[bx0:bx1, by0:by1] = color.gray2rgb(background[x0 : x1 + 1, y0 : y1 + 1] * cyto) if params.find_septum: by0 = by0 + y1 - y0 + 1 by1 = by1 + y1 - y0 + 1 img[bx0:bx1, by0:by1] = color.gray2rgb(background[x0 : x1 + 1, y0 : y1 + 1] * axial) self.image = img_as_int(img)
def loadPictures(data): N_hieros = len(data) repertory, file = data['anchor'][0].split("_") label=str(data['label'][0]) picture="/Users/fgimbert/Documents/Dataset/Manual/Preprocessed/"+str(repertory)+"/"+str(file)+"_"+label+".png" #im = Image.open(picture) #img_x=im.size[0] #img_y=im.size[1] img_x=50 img_y=75 anchor, positive, negative = np.zeros((N_hieros,img_x*img_y,3)),np.zeros((N_hieros,img_x*img_y,3)),np.zeros((N_hieros,img_x*img_y,3)) labels_true = [] labels_wrong= [] for index, row in data.iterrows(): repertory, file = row['anchor'].split("_") label = row['label'] picture = path + str(repertory) + "/" + str( file) + "_" + str(label) + ".png" labels_true.append(label) img_gray = mpimg.imread(picture) img_rgb=gray2rgb(img_gray) anchor[index]=img_rgb.reshape(1,img_x*img_y,3) repertory, file = row['positive'].split("_") picture = path + str(repertory) + "/" + str( file) + "_" + str(label) + ".png" img_gray = mpimg.imread(picture) img_rgb = gray2rgb(img_gray) positive[index] = img_rgb.reshape(1, img_x * img_y, 3) repertory, file = row['negative'].split("_") label = row['neg_label'] picture = path + str(repertory) + "/" + str( file) + "_" + str(label) + ".png" labels_wrong.append(label) img_gray = mpimg.imread(picture) img_rgb = gray2rgb(img_gray) negative[index] = img_rgb.reshape(1, img_x * img_y, 3) return [anchor,positive,negative],labels_true,labels_wrong
def _apply(self, imgmsg, maskmsg): bridge = cv_bridge.CvBridge() img = bridge.imgmsg_to_cv2(imgmsg) if img.ndim == 2: img = gray2rgb(img) mask = bridge.imgmsg_to_cv2(maskmsg, desired_encoding='mono8') mask = mask.reshape(mask.shape[:2]) mask = gray2rgb(mask) # compute label roi = closed_mask_roi(mask) roi_labels = masked_slic(img=img[roi], mask=mask[roi], n_segments=20, compactness=30) if roi_labels is None: return labels = np.zeros(mask.shape, dtype=np.int32) # labels.fill(-1) # set bg_label labels[roi] = roi_labels if self.is_debugging: # publish debug slic label slic_labelmsg = bridge.cv2_to_imgmsg(labels) slic_labelmsg.header = imgmsg.header self.pub_slic.publish(slic_labelmsg) # compute rag g = rag_solidity(labels, connectivity=2) if self.is_debugging: # publish debug rag drawn image rag_img = draw_rag(labels, g, img) rag_img = img_as_uint(rag_img) rag_imgmsg = bridge.cv2_to_imgmsg( rag_img.astype(np.uint8), encoding='rgb8') rag_imgmsg.header = imgmsg.header self.pub_rag.publish(rag_imgmsg) # merge rag with solidity merged_labels = merge_hierarchical( labels, g, thresh=1, rag_copy=False, in_place_merge=True, merge_func=_solidity_merge_func, weight_func=_solidity_weight_func) merged_labels += 1 merged_labels[mask == 0] = 0 merged_labelmsg = bridge.cv2_to_imgmsg(merged_labels.astype(np.int32)) merged_labelmsg.header = imgmsg.header self.pub.publish(merged_labelmsg) if self.is_debugging: out = label2rgb(merged_labels, img) out = (out * 255).astype(np.uint8) out_msg = bridge.cv2_to_imgmsg(out, encoding='rgb8') out_msg.header = imgmsg.header self.pub_label.publish(out_msg)
def process_image(sub_folder, image, img_rows, img_cols): '''Process individual images''' # Using the mean pixel values used in VGG16 Model mean_pixel = [103.939, 116.779, 123.68] image_path = os.path.join(sub_folder, image) image_file = imread(image_path) # If the image has 4 channels, change it to 3 channels if len(image_file.shape) > 2 and image_file.shape[2] == 4: image_file = image_file[:,:,:-1] # If the image is in grey scale, change it to RGB if len(image_file.shape) < 3: image_file = gray2rgb(image_file) image_file = image_file.astype(np.float32, copy=False) # There are some images where the actual image we are interested is in # the right side. For such images remove the left half if image_file.shape[1] > image_file.shape[0]: new_shape = image_file.shape[1] / 2 image_file = image_file[:,new_shape:,:] # one more image pattern elif image_file.shape[0] == 1000 & image_file.shape[1] == 677: image_file = image_file[:,:455,:] image_resized = imresize(image_file, (img_rows, img_cols)) # normalize the image for c in xrange(3): image_resized[:, :, c] = image_resized[:, :, c] - mean_pixel[c] image_res = image_resized.transpose((2,0,1)) return image_res
def any2rgb(array, name=''): """ Returns a normalized float 3-channel array regardless of original dtype and channel. All valid pyparty images must pass this name : str Name of array which will be referenced in logger messages""" if not isinstance(array, np.ndarray): return to_normrgb(array) # ***** # Quick way to convert to float (don't use img_as_float becase we want # to enforce that upperlimit of 255 is checked array = array / 1.0 # Returns scalar for 1-channel OR 3-channel if array.max() > 1: # For 8-bit, divide by 255! if array.max() > COLORTYPE[1]: raise ColorError("Only 8bit ints are supported for now") array = array / COLORTYPE[1] if array.ndim == 3: # If RGBA if array.shape[2] == 4: array = array[..., 0:3] logger.warn("4-channel RGBA recieved; ignoring A channel") return array elif array.ndim == 2: logger.warn('%s color has been converted (1-channel to 3-channel RGB)' % name) return skcol.gray2rgb(array) raise ColorError('%s must be 2 or 3 dimensional array!' % name )
def hugh_circle_detection(image): # Load picture and detect edges edges = canny(image, sigma=3, low_threshold=10, high_threshold=50) fig, ax = plt.subplots(ncols=1, nrows=1, figsize=(5, 2)) # Detect two radii hough_radii = np.arange(15, 30, 2) hough_res = hough_circle(edges, hough_radii) centers = [] accums = [] radii = [] for radius, h in zip(hough_radii, hough_res): # For each radius, extract two circles num_peaks = 2 peaks = peak_local_max(h, num_peaks=num_peaks) centers.extend(peaks) accums.extend(h[peaks[:, 0], peaks[:, 1]]) radii.extend([radius] * num_peaks) # Draw the most prominent 5 circles image = color.gray2rgb(image) for idx in np.argsort(accums)[::-1][:5]: center_x, center_y = centers[idx] radius = radii[idx] cx, cy = circle_perimeter(center_y, center_x, radius) image[cy, cx] = (220, 20, 20) ax.imshow(image, cmap=plt.cm.gray) plt.show()
def prep_image(im, IMAGE_W, IMAGE_H, BGR=BGR, bw=False): if len(im.shape) == 2: im = im[:, :, np.newaxis] im = np.repeat(im, 3, axis=2) h, w, _ = im.shape if h*IMAGE_W < w*IMAGE_H: im = skimage.transform.resize(im, (IMAGE_H, w*IMAGE_H//h), preserve_range=True) else: im = skimage.transform.resize(im, (h*IMAGE_W//w, IMAGE_W), preserve_range=True) # Central crop h, w, _ = im.shape im = im[h//2-IMAGE_H//2:h//2+IMAGE_H-IMAGE_H//2, w//2-IMAGE_W//2: w//2-IMAGE_W//2 +IMAGE_W] rawim = im.astype('uint8') # Shuffle axes to c01 if bw: if BGR: im = im[:,:,::-1] bwim = gray2rgb(rgb2gray(im)) im = (bwim*bw+im.astype("float64")*(1.-bw)) if BGR: im = im[:,:,::-1] im = np.swapaxes(np.swapaxes(im, 1, 2), 0, 1) # Convert RGB to BGR if not BGR: im = im[::-1, :, :] im = im - MEAN_VALUES return rawim, floatX(im[np.newaxis])
def main(args): """ Entry point. """ # load the image img = imread(args.input) if img.ndim == 2: img = gray2rgb(img) elif img.shape[2] == 4: img = img[:, :, :3] upper_dim = max(img.shape[:2]) if upper_dim > args.max_dim: img = rescale(img, args.max_dim/float(upper_dim), order=3) # compute saliency start = timeit.default_timer() img_sal = compute_saliency(img) runtime = timeit.default_timer() - start print("Took {0} seconds.".format(runtime)) # save image (fname, ext) = os.path.splitext(args.input) out_path = fname + "_saliency" + ext imsave(out_path, img_sal)
def col_im(im, gt): im = np.asarray(im, dtype='float32') im = im*1./im.max() rgb_image = color.gray2rgb(im) im = rgb_image.copy() if gt is None: return im indices_0 = np.where(gt == 0) # nothing indices_1 = np.where(gt == 1) # necrosis indices_2 = np.where(gt == 2) # edema indices_3 = np.where(gt == 3) # non-enhancing tumor indices_4 = np.where(gt == 4) # enhancing tumor m0 = [1., 1., 1.] m1 = [1., 0., 0.] # red: necrosis m2 = [0.2, 1., 0.2] # green: edema m3 = [1., 1., 0.2] # yellow: non-enhancing tumor m4 = [1., 0.6, 0.2] # orange: enhancing tumor im[indices_0[0], indices_0[1], :] *= m0 im[indices_1[0], indices_1[1], :] *= m1 im[indices_2[0], indices_2[1], :] *= m2 im[indices_3[0], indices_3[1], :] *= m3 im[indices_4[0], indices_4[1], :] *= m4 return im
def transform(self, Xb, yb): Xb, yb = super(ReadImageBatchIteratorMixin, self).transform(Xb, yb) batch_size = min(Xb.shape[0], self.batch_size) num_channels = 1 if self.read_image_as_gray is True else 3 h = self.read_image_size[0] w = self.read_image_size[1] imgs = np.empty((batch_size, num_channels, h, w), dtype=np.float32) for i, path in enumerate(Xb): img_fname = os.path.join(self.read_image_prefix_path, path) if self.verbose > 2: print('Reading %s' % img_fname) img = imread(img_fname, as_grey=self.read_image_as_gray) if img.shape[0] != h or img.shape[1] != w: img = resize(img, (h, w)) else: img = img.astype(float) / 255 # When reading image as color image, convert grayscale image to RGB for consistency if len(img.shape) == 2 and self.read_image_as_gray is False: img = gray2rgb(img) # Transpose to bc01 if self.read_image_as_bc01 and self.read_image_as_gray is False: img = img.transpose(2, 0, 1) elif self.read_image_as_bc01 and self.read_image_as_gray is True: img = np.expand_dims(img, axis=0) imgs[i] = img return imgs, yb
def _apply(self, img_msg, label_msg): bridge = cv_bridge.CvBridge() img = bridge.imgmsg_to_cv2(img_msg) label_img = bridge.imgmsg_to_cv2(label_msg) # publish only valid label region applied = img.copy() applied[label_img == 0] = 0 applied_msg = bridge.cv2_to_imgmsg(applied, encoding=img_msg.encoding) applied_msg.header = img_msg.header self.pub_img.publish(applied_msg) # publish visualized label if img_msg.encoding in {'16UC1', '32SC1'}: # do dynamic scaling to make it look nicely min_value, max_value = img.min(), img.max() img = (img - min_value) / (max_value - min_value) * 255 img = gray2rgb(img) label_viz_img = label2rgb(label_img, img, bg_label=0) label_viz_img = mark_boundaries(label_viz_img, label_img, (1, 0, 0)) label_viz_img = (label_viz_img * 255).astype(np.uint8) label_viz_msg = bridge.cv2_to_imgmsg(label_viz_img, encoding='rgb8') label_viz_msg.header = img_msg.header self.pub_label_viz.publish(label_viz_msg) # publish mask if self._publish_mask: bg_mask = (label_img == 0) fg_mask = ~bg_mask bg_mask = (bg_mask * 255).astype(np.uint8) fg_mask = (fg_mask * 255).astype(np.uint8) fg_mask_msg = bridge.cv2_to_imgmsg(fg_mask, encoding='mono8') fg_mask_msg.header = img_msg.header bg_mask_msg = bridge.cv2_to_imgmsg(bg_mask, encoding='mono8') bg_mask_msg.header = img_msg.header self.pub_fg_mask.publish(fg_mask_msg) self.pub_bg_mask.publish(bg_mask_msg)
def markPath(mat, path, mark_as="red"): assert mark_as in ["red", "green", "blue", "black", "white"] if len(mat.shape) == 2: mat = color.gray2rgb(mat) ret = np.zeros(mat.shape) ret[:, :, :] = mat[:, :, :] # Preprocess image if np.max(ret) < 1.1 or np.max(ret) > 256: # matrix is in float numbers ret -= np.min(ret) ret /= np.max(ret) ret *= 256 # Determinate components if mark_as == "red": r, g, b = 255, 0, 0 elif mark_as == "green": r, g, b = 0, 255, 0 elif mark_as == "blue": r, g, b = 0, 0, 255 elif mark_as == "white": r, g, b = 255, 255, 255 elif mark_as == "black": r, b, b = 0, 0, 0 # Place R,G,B for i in path: ret[i[0], i[1], 0] = r ret[i[0], i[1], 1] = g ret[i[0], i[1], 2] = b return ret.astype("uint8")
def slics_3D(im, pseudo_3D=True, n_segments=100, get_slicewise=False): if im.ndim != 3: raise Exception('3D image is needed.') if not pseudo_3D: # need to convert to RGB image im_rgb = np.zeros((im.shape[0], im.shape[1], im.shape[2], 3)) im_rgb[:,:,:,0] = im im_rgb[:,:,:,1] = im im_rgb[:,:,:,2] = im suppxls = skiseg.slic(im_rgb, n_segments=n_segments, spacing=(2,1,1)) else: suppxls = np.zeros(im.shape) if get_slicewise: suppxls_slicewise = np.zeros(im.shape) offset = 0 for i in range(im.shape[0]): # suppxl = skiseg.slic(cv2.cvtColor(im[i,:,:], cv2.COLOR_GRAY2RGB), n_segments=n_segments) suppxl = skiseg.slic(skicol.gray2rgb(im[i,:,:]), n_segments=n_segments) suppxls[i,:,:] = suppxl + offset if get_slicewise: suppxls_slicewise[i,:,:] = suppxl offset = suppxls.max() + 1 if get_slicewise: return suppxls, suppxls_slicewise else: return suppxls
def markPath(mat, path, mark_as='red'): assert mark_as in ['red','green','blue','black','white'] if len(mat.shape) == 2: mat = color.gray2rgb(mat) ret = np.zeros(mat.shape) ret[:,:,:] = mat[:,:,:] # Preprocess image if np.max(ret) < 1.1 or np.max(ret) > 256: # matrix is in float numbers ret -= np.min(ret) ret /= np.max(ret) ret *= 256 # Determinate components if mark_as == 'red': r,g,b = 255,0,0 elif mark_as == 'green': r,g,b = 0,255,0 elif mark_as == 'blue': r,g,b = 0,0,255 elif mark_as == 'white': r,g,b = 255,255,255 elif mark_as == 'black': r,b,b = 0,0,0 # Place R,G,B for i in path: ret[i[0],i[1],0] = r ret[i[0],i[1],1] = g ret[i[0],i[1],2] = b return ret.astype('uint8')
def load_images(path): print 'reading file names ... ' names = [d for d in os.listdir (path) if d.endswith('.jpg')] names = natsorted(names) num_rows = len(names) print names print 'making dataset ... ' test_image = np.zeros((num_rows, num_features), dtype = float) label = np.zeros((num_rows, 1), dtype = int) file_names = [] i = 0 for n in names: print n.split('.')[0] image = imread(os.path.join(path, n)) if len(image.shape) == 3 and image.shape[2] == 3: image = image.transpose(2, 0, 1) test_image[i, 0:num_features] = np.reshape(image, (1, num_features)) label[i] = n.split('.')[0] i += 1 else: image = gray2rgb(image) image = image.transpose(2, 0, 1) test_image[i, 0:num_features] = np.reshape(image, (1, num_features)) label[i] = n.split('.')[0] i += 1 return test_image, label
def imreadconvert(Xname): X=imread(Xname).astype(np.float32) if len(X.shape)==3: X=X.transpose(2,0,1) return X else: return gray2rgb(X).transpose(2,0,1)
def get_image(fname): arr = io.imread(fname) if arr.ndim == 2: arr = color.gray2rgb(arr) arr = util.img_as_float(arr) assert arr.ndim == 3 return arr
def writeMask(self,mask): FILL_CHANNEL = 4 REMOVE_CHANNEL = 8 imgMasked = np.uint8(255 * color.gray2rgb(color.rgb2gray(self.image))) imgMasked[mask == FILL_CHANNEL] = 255 imgMasked[mask == REMOVE_CHANNEL] = 0 return imgMasked #imageOut
def get(self, uri): i = imread(uri) if len(i.shape) == 2: i = gray2rgb(i) else: i = i[:, :, :3] c = self._image_to_color.get(i) dbg = self._settings['debug'] if dbg is None: return c c, imgs = c b = splitext(basename(uri))[0] imsave(join(dbg, b + '-resized.jpg'), imgs['resized']) imsave(join(dbg, b + '-back.jpg'), img_as_float(imgs['back'])) imsave(join(dbg, b + '-skin.jpg'), img_as_float(imgs['skin'])) imsave(join(dbg, b + '-clusters.jpg'), imgs['clusters']) return c, { 'resized': join(dbg, b + '-resized.jpg'), 'back': join(dbg, b + '-back.jpg'), 'skin': join(dbg, b + '-skin.jpg'), 'clusters': join(dbg, b + '-clusters.jpg'), }
def vis_col_im(im, gt): indices_0 = np.where(gt == 0) # nothing indices_1 = np.where(gt == 1) # necrosis indices_2 = np.where(gt == 2) # edema indices_3 = np.where(gt == 3) # non-enhancing tumor indices_4 = np.where(gt == 4) # enhancing tumor im = np.asarray(im, dtype='float32') im = im*1./im.max() rgb_image = color.gray2rgb(im) m0 = [1., 1., 1.] m1 = [1., 0., 0.] m2 = [0.2, 1., 0.2] m3 = [1., 1., 0.2] m4 = [1., 0.6, 0.2] im = rgb_image.copy() im[indices_0[0], indices_0[1], :] *= m0 im[indices_1[0], indices_1[1], :] *= m1 im[indices_2[0], indices_2[1], :] *= m2 im[indices_3[0], indices_3[1], :] *= m3 im[indices_4[0], indices_4[1], :] *= m4 plt.imshow(im) plt.show() plt.close()
def vis_result(image, seg, gt, title1='Segmentation', title2='Ground truth', savefile=None): indices = np.where(seg >= 0.5) indices_gt = np.where(gt >= 0.5) im_norm = image / image.max() rgb_image = color.gray2rgb(im_norm) multiplier = [0., 1., 1.] multiplier_gt = [1., 1., 0.] im_seg = rgb_image.copy() im_gt = rgb_image.copy() im_seg[indices[0], indices[1], :] *= multiplier im_gt[indices_gt[0], indices_gt[1], :] *= multiplier_gt fig = plt.figure() a = fig.add_subplot(1, 2, 1) plt.imshow(im_seg) a.set_title(title1) a = fig.add_subplot(1, 2, 2) plt.imshow(im_gt) a.set_title(title2) if savefile is None: plt.show() else: plt.savefig(savefile) plt.close()
def load_images(filenames, rgb, crop=None): images = [] for filename in filenames: try: if rgb: image = imread(filename, as_grey=False) else: image = imread(filename, as_grey = True) x2, y2 = image.shape if not crop: crop = min(x2,y2) - min(x2,y2)%8 if x2 > crop and y2 > crop: x1 = (x2-crop)/2 x2 = x2 - x1 y1 = 0 y2 = copy.deepcopy(crop) image = image[x1:x2,y1:y2] image = np.array(image).astype(float) if not rgb: image = gray2rgb(image) #Applying the normalization suggested by the authors of VGGNet image[:,:,0] -= 103.939 image[:,:,1] -= 116.779 image[:,:,2] -= 123.68 images.append(image.T[0:3]) except FileNotFoundError: pass return np.array(images)
def add_rectangle(img, y0, x0, y1, x1, color="r", width=1): """Colors: 'r', 'g', 'b', 'w', 'k'""" im = np.copy(img) if im.ndim == 2: im = gray2rgb(im) max_val = 1 if np.max(img) > 1: max_val = 255 channel = 3 # Bogus value when color = 'w' or 'k' if color == "r": channel = 0 if color == "g": channel = 1 if color == "b": channel = 2 for i in range(width): yy0 = y0 + i xx0 = x0 + i yy1 = y1 - i xx1 = x1 - i rr, cc = line(yy0, xx0, yy1, xx0) # left im = paint_line(im, rr, cc, color, channel, max_val) rr, cc = line(yy1, xx0, yy1, xx1) # bottom im = paint_line(im, rr, cc, color, channel, max_val) rr, cc = line(yy1, xx1, yy0, xx1) # right im = paint_line(im, rr, cc, color, channel, max_val) rr, cc = line(yy0, xx1, yy0, xx0) # top im = paint_line(im, rr, cc, color, channel, max_val) return im
def rgb2caffe(im, downscale=True, out_size=(128, 171)): ''' Converts an RGB image to caffe format and downscales it as needed by C3D Parameters ---------- im an RGB image downscale Returns ------- a caffe image (channel,height, width) in BGR format ''' im=np.copy(im) if len(im.shape)==2: # Make sure the image has 3 channels im = color.gray2rgb(im) if downscale: h, w, _ = im.shape im = skimage.transform.resize(im, out_size, preserve_range=True) else: im=np.array(im,theano.config.floatX) im = np.swapaxes(np.swapaxes(im, 1, 2), 0, 1) # Convert to BGR im = im[::-1, :, :] return np.array(im,theano.config.floatX)
def imp_img(img_name): # read img = imread(img_name) # if gray convert to color if len(img.shape) == 2: img = gray2rgb(img) return img
def save_segmented_image(self, filepath_image, modality='t1c', show=False): ''' Creates an image of original brain with segmentation overlay and save it in ./predictions INPUT (1) str 'filepath_image': filepath to test image for segmentation, including file extension (2) str 'modality': imaging modality to use as background. defaults to t1c. options: (flair, t1, t1c, t2) (3) bool 'show': If true, shows output image. defaults to False. OUTPUT (1) if show is True, shows image of segmentation results (2) if show is false, returns segmented image. ''' modes = {'flair': 0, 't1': 1, 't1c': 2, 't2': 3} segmentation = self.predict_image(filepath_image, show=False) print 'segmentation = ' + str(segmentation) img_mask = np.pad(segmentation, (16, 16), mode='edge') ones = np.argwhere(img_mask == 1) twos = np.argwhere(img_mask == 2) threes = np.argwhere(img_mask == 3) fours = np.argwhere(img_mask == 4) test_im = io.imread(filepath_image) test_back = test_im.reshape(5, 216, 160)[modes[modality]] # overlay = mark_boundaries(test_back, img_mask) gray_img = img_as_float(test_back) # adjust gamma of image image = adjust_gamma(color.gray2rgb(gray_img), 0.65) sliced_image = image.copy() red_multiplier = [1, 0.2, 0.2] yellow_multiplier = [1, 1, 0.25] green_multiplier = [0.35, 0.75, 0.25] blue_multiplier = [0, 0.25, 0.9] print str(len(ones)) print str(len(twos)) print str(len(threes)) print str(len(fours)) # change colors of segmented classes for i in xrange(len(ones)): sliced_image[ones[i][0]][ones[i][1]] = red_multiplier for i in xrange(len(twos)): sliced_image[twos[i][0]][twos[i][1]] = green_multiplier for i in xrange(len(threes)): sliced_image[threes[i][0]][threes[i][1]] = blue_multiplier for i in xrange(len(fours)): sliced_image[fours[i][0]][fours[i][1]] = yellow_multiplier #if show=True show the prediction if show: print 'Showing...' io.imshow(sliced_image) plt.show() #save the prediction print 'Saving...' try: mkdir_p('./predictions/') io.imsave('./predictions/' + os.path.basename(filepath_image) + '.png', sliced_image) print 'prediction saved.' except: io.imsave('./predictions/' + os.path.basename(filepath_image) + '.png', sliced_image) print 'prediction saved.'
def overlay_cells(cells, image, colors): "Overlay the edges of each individual cell in the provided image" tmp = color.gray2rgb(image) for k in cells.keys(): c = cells[k] if c.selection_state == 1: col = colors[c.color_i][:3] for px in c.outline: x, y = px tmp[x, y] = col if c.sept_mask is not None: try: x0, y0, x1, y1 = c.box tmp[x0:x1, y0:y1] = mark_boundaries(tmp[x0:x1, y0:y1], img_as_int( c.sept_mask), color=col) except IndexError: c.selection_state = -1 return tmp
def test_gray2rgb(): x = np.array([0, 0.5, 1]) assert_raises(ValueError, gray2rgb, x) x = x.reshape((3, 1)) y = gray2rgb(x) assert_equal(y.shape, (3, 1, 3)) assert_equal(y.dtype, x.dtype) x = np.array([[0, 128, 255]], dtype=np.uint8) z = gray2rgb(x) assert_equal(z.shape, (1, 3, 3)) assert_equal(z[..., 0], x) assert_equal(z[0, 1, :], [128, 128, 128])
def add_alpha(img, mask=None): """ Adds a masked alpha channel to an image. Parameters ---------- img : (M, N[, 3]) ndarray Image data, should be rank-2 or rank-3 with RGB channels. If img already has alpha, nothing will be done. mask : (M, N[, 3]) ndarray, optional Mask to be applied. If None, the alpha channel is added with full opacity assumed (1) at all locations. """ # don't do anything if there is already an alpha channel # an alpha channel stores the transparency # value for each pixel. Zero means the pixel is # transparent and does not contribute to the image # return img print("ADD ALPHA", img.shape) if img.shape[2] > 3: return img # make sure the image is 3 channels if img.ndim == 2: img = gray2rgb(img) if mask is None: # create transparent mask # 1 should be fully transparent mask = np.ones(img.shape[:2], np.uint8) * 255 return np.dstack((img, mask))
def add_cross_to_image(im, xx, yy): image = color.gray2rgb(im.copy()) for h in range(-4, 5, 1): image[xx,yy+h] = (0, 1, 0) for v in range(-4, 5, 1): image[xx+v,yy] = (0, 1, 0) return image
def update_current_arena_pose(self): arena_starting_pose = rotate_point(self.last_robot_pose, diff_heading_deg(self.last_robot_pose.rotation.degrees, self.cu) self.current_arena_pose = self.current_arena_pose + convertPoseFromMmToInches(rotate_point(self.robot.pose, - last_robot_pose) async def __execute_directions(self, directions): print("Robot is at: ", self.robot.pose) await self.robot.turn_in_place(angle=directions.rotation.angle_z).wait_for_completed() print("ROBOT is at AFTER TURNING to be parallel to X: ", self.robot.pose) await self.robot.drive_straight(distance=distance_mm(directions.position.x * grid.scale), speed=speed_mmps(80)).wait_for_completed() print("ROBOT is at AFTER DRIVING in the X direction: ", self.robot.pose) await self.robot.turn_in_place(angle=degrees(90)).wait_for_completed() print("ROBOT is at AFTER TURNING to be parallel to Y: ", self.robot.pose) await self.robot.drive_straight(distance=distance_mm(directions.position.y * grid.scale), speed=speed_mmps(80)).wait_for_completed() print("ROBOT is at AFTER DRIVING in the Y direction: ", self.robot.pose) async def localize(self): # reset our location estimates conf = False self.current_arena_pose = Pose(0,0,0,angle_z=degrees(0)) self.pf = ParticleFilter(grid) # reset lift and head await self.robot.set_lift_height(0.0).wait_for_completed() await self.robot.set_head_angle(degrees(3)).wait_for_completed() while not conf: # move a little self.last_robot_pose = self.robot.pose await self.robot.turn_in_place(angle=degrees(20)).wait_for_completed() odometry = self.__compute_odometry() detected_markers, camera_image = await self.__marker_processing() # update, motion, and measurment with the odometry and marker data curr_x, curr_y, curr_h, conf = pf.update(odometry, detected_markers) # update gui self.gui.show_particles(self.pf.particles) self.gui.show_mean(curr_x, curr_y, curr_h) self.gui.show_camera_image(camera_image) self.gui.updated.set() self.current_arena_pose = Pose(curr_x , curr_y, 0, angle_z=degrees(curr_h)) def __compute_odometry(self, cvt_inch=True): ''' Compute the odometry given the current pose of the robot (use robot.pose) Input: - curr_pose: a cozmo.robot.Pose representing the robot's current location - cvt_inch: converts the odometry into grid units Returns: - 3-tuple (dx, dy, dh) representing the odometry ''' last_x, last_y, last_h = self.last_robot_pose.position.x, self.last_robot_pose.position.y, \ self.last_robot_pose.rotation.angle_z.degrees curr_x, curr_y, curr_h = self.robot.pose.position.x, self.robot.pose.position.y, \ self.robot.pose.rotation.angle_z.degrees dx, dy = rotate_point(curr_x-last_x, curr_y-last_y, -last_h) if cvt_inch: dx, dy = dx / grid.scale, dy / grid.scale return (dx, dy, diff_heading_deg(curr_h, last_h)) async def __marker_processing(self, show_diagnostic_image=False): ''' Obtain the visible markers from the current frame from Cozmo's camera. Since this is an async function, it must be called using await, for example: markers, camera_image = await marker_processing(robot, camera_settings, show_diagnostic_image=False) Input: - robot: cozmo.robot.Robot object - camera_settings: 3x3 matrix representing the camera calibration settings - show_diagnostic_image: if True, shows what the marker detector sees after processing Returns: - a list of detected markers, each being a 3-tuple (rx, ry, rh) (as expected by the particle filter's measurement update) - a PIL Image of what Cozmo's camera sees with marker annotations ''' # Wait for the latest image from Cozmo image_event = await self.robot.world.wait_for(cozmo.camera.EvtNewRawCameraImage, timeout=30) # Convert the image to grayscale image = np.array(image_event.image) image = color.rgb2gray(image) # Detect the markers markers, diag = detect.detect_markers(image, self.camera_settings, include_diagnostics=True) # Measured marker list for the particle filter, scaled by the grid scale marker_list = [marker['xyh'] for marker in markers] marker_list = [(x/self.grid.scale, y/self.grid.scale, h) for x,y,h in marker_list] # Annotate the camera image with the markers if not show_diagnostic_image: annotated_image = image_event.image.resize((image.shape[1] * 2, image.shape[0] * 2)) annotator.annotate_markers(annotated_image, markers, scale=2) else: diag_image = color.gray2rgb(diag['filtered_image']) diag_image = Image.fromarray(np.uint8(diag_image * 255)).resize((image.shape[1] * 2, image.shape[0] * 2)) annotator.annotate_markers(diag_image, markers, scale=2) annotated_image = diag_image return marker_list, annotated_image async def run(robot: cozmo.robot.Robot): cosimo = CozmoWarehouseWorker() cosimo.localize() cosimo.drive_to(cosimo.pick_up_pose) directions = goal_pose - last_pose current_pose = last_pose last_robot_pose = robot.pose print("SETTING LAST ROBOT POSE TO: ", last_robot_pose) print("SO WE GOING TO FOLLOW THIS TO PICKUP ZONE:", directions) await execute_directions(robot, directions) await robot.turn_in_place(angle=degrees(45)).wait_for_completed() print("LAST ROBOT POSE IS: ", last_robot_pose) print("CURRENT POSE IS:", robot.pose) print("WE THINK WE MOVED THIS MUCH TO GO TO PICKUP ZONE: ", convertPoseToInches(robot.pose - last_robot_pose)) last_robot_pose = robot.pose print("COZMO THINKS IT IS AT AFTER DRIVING TO PICKUPZONE: ", current_pose) # await robot.say_text('Ready for pick up!').wait_for_completed() while True: cube = await robot.world.wait_for_observed_light_cube(timeout=30) print("Found cube: %s" % cube) await robot.pickup_object(cube, num_retries=5).wait_for_completed() current_pose = current_pose + convertPoseToInches(robot.pose - last_robot_pose) print("WE THINK WE MOVED THIS MUCH TO PICK UP CUBE: ", convertPoseToInches(robot.pose - last_robot_pose)) last_robot_pose = robot.pose #cosimo.update_pose() print("COZMO THINKS IT IS AT AFTER PICKING UP CUBE: ", current_pose) #await look_around_until_converge(robot) # intialize an explorer after localized #cosimo = CozmoExplorer(robot, x_0=last_pose.position.x, y_0=last_pose.position.y, theta_0=last_pose.rotation.angle_z.radians) # move robot to pickup zone once localized #print("COZMO CONVERTED THAT TO A START AT:", cosimo.last_arena_pose) #current_pose = last_pose # rrt to drop zone and drop off cube for destination in drop_off_directions: directions = convertInchesToPose(destination) - current_pose await execute_directions(robot,directions) current_pose = current_pose + convertPoseToInches(robot.pose - last_robot_pose) print("WE THINK WE MOVED THIS MUCH TO FOLLOW DIRECTIONS: ", convertPoseToInches(robot.pose - last_robot_pose)) last_robot_pose = robot.pose print("COZMO THINKS IT IS AT AFTER FOLLOWING DIRECTIONS: ", current_pose) #await cosimo.go_to_goal(goal_node=dropoff_node) await robot.set_lift_height(0.0).wait_for_completed() # rrt to just in front of pick up zone # await cosimo.go_to_goal(goal_node=pickup_node) class CozmoThread(threading.Thread): def __init__(self): threading.Thread.__init__(self, daemon=False) def run(self): cozmo.robot.Robot.drive_off_charger_on_connect = False # Cozmo can stay on his charger cozmo.run_program(run, use_viewer=False) if __name__ == '__main__': # cozmo thread cozmo_thread = CozmoThread() cozmo_thread.start() # init gui.show_particles(pf.particles) gui.show_mean(0, 0, 0) gui.start()
def get_landmarks_from_image(self, image_or_path, detected_faces=None): """Predict the landmarks for each face present in the image. This function predicts a set of 68 2D or 3D images, one for each image present. If detect_faces is None the method will also run a face detector. Arguments: image_or_path {string or numpy.array or torch.tensor} -- The input image or path to it. Keyword Arguments: detected_faces {list of numpy.array} -- list of bounding boxes, one for each face found in the image (default: {None}) """ if isinstance(image_or_path, str): try: image = io.imread(image_or_path) except IOError: print("error opening file :: ", image_or_path) return None else: image = image_or_path if image.ndim == 2: image = color.gray2rgb(image) elif image.ndim == 4: image = image[..., :3] if detected_faces is None: detected_faces = self.face_detector.detect_from_image( image[..., ::-1].copy()) if len(detected_faces) == 0: print("Warning: No faces were detected.") return None torch.set_grad_enabled(False) landmarks = [] for i, d in enumerate(detected_faces): center = torch.FloatTensor( [d[2] - (d[2] - d[0]) / 2.0, d[3] - (d[3] - d[1]) / 2.0]) center[1] = center[1] - (d[3] - d[1]) * 0.12 scale = (d[2] - d[0] + d[3] - d[1]) / self.face_detector.reference_scale inp = crop(image, center, scale) inp = torch.from_numpy(inp.transpose((2, 0, 1))).float() inp = inp.to(self.device) inp.div_(255.0).unsqueeze_(0) out = self.face_alignment_net(inp)[-1].detach() if self.flip_input: out += flip(self.face_alignment_net(flip(inp))[-1].detach(), is_label=True) out = out.cpu() pts, pts_img = get_preds_fromhm(out, center, scale) pts, pts_img = pts.view(68, 2) * 4, pts_img.view(68, 2) if self.landmarks_type == LandmarksType._3D: heatmaps = np.zeros((68, 256, 256), dtype=np.float32) for i in range(68): if pts[i, 0] > 0: heatmaps[i] = draw_gaussian(heatmaps[i], pts[i], 2) heatmaps = torch.from_numpy(heatmaps).unsqueeze_(0) heatmaps = heatmaps.to(self.device) depth_pred = self.depth_prediciton_net( torch.cat((inp, heatmaps), 1)).data.cpu().view(68, 1) pts_img = torch.cat( (pts_img, depth_pred * (1.0 / (256.0 / (200.0 * scale)))), 1) landmarks.append(pts_img.numpy()) return landmarks
def ab_trans_main(train_it=True, cont=False): # Image transformer datagen = ImageDataGenerator(shear_range=0.2, zoom_range=0.2, rotation_range=20, horizontal_flip=True) # Generate training data model = abnet_trans(img_size) # Train model filepath = "ab_trans_model_best.h5" checkpoint = ModelCheckpoint(filepath, monitor='loss', verbose=0, save_weights_only=True, period=1, save_best_only=True) callbacks_list = [checkpoint] # opt = SGD(lr=0.0005, momentum=0.0, clipnorm=50.0, decay=1e-5) opt = Adam(lr=0.001, beta_2=0.5, clipnorm=50.0, epsilon=0.01) #0.001可以缓慢的学习 model.compile(optimizer=opt, loss='mse') filepath = "ab_trans_model.h5" if cont: model.load_weights(filepath) all_files = os.listdir(train_data_dir) steps_per_epoch = 512 # train_it = False inception = load_inception() if train_it: Xtest = get_Xtrainlimit(img_size, data_dir=test_data_dir, limit=64) # Xtrain = get_Xtrainlimit(img_size) # model.fit_generator(image_ab_gen(datagen, Xtrain, batch_size), epochs=1, # steps_per_epoch=int(Xtrain.shape[0]/batch_size), # callbacks=callbacks_list, verbose=1) model.fit_generator(image_a_b_gen_batches(all_files, batch_size, img_size, trans=True, inception=inception, data_dir=train_data_dir), validation_data=image_ab_valid( datagen, Xtest, batch_size, trans=True, inception=inception), epochs=20000, steps_per_epoch=int(len(all_files) / batch_size), callbacks=callbacks_list, verbose=1) model.save_weights(filepath) if not train_it and (not cont): model.load_weights(filepath) # Make predictions on validation images # model.load_weights(filepath) imgs, color_me = load_test(img_size, data_dir=test_data_dir) gray_imgs = my_rgb_to_gray(imgs) grayscaled_rgb = gray2rgb(gray_imgs) embed = create_inception_embedding(grayscaled_rgb, inception) # Test model # output = model.predict([color_me/100.0, color_me_embed]) output = model.predict([color_me, embed]) output = output * 128.0 # Output colorizations curs = [] for i in range(len(output)): cur = np.zeros((img_size, img_size, 3), dtype=np.float64) cur[:, :, 0] = color_me[i][:, :, 0] cur[:, :, 1:] = output[i] curs.append(cur) img = lab2rgb(cur) * 255.0 # pylo pyplot.imsave(result_dir + "img_" + str(i) + ".jpg", img.astype('uint8'))
def save_img(image_batch, mask_batch, prediction_ii, prediction_fcn, out_images_folder, tag, void_label, colors): """ Save image, segmentation, ground truth Parameters ---------- image_batch: batch of images (b, c, 0, 1) mask_batch: batch of ground truth labels (b, 0, 1) prediction_fcn: batch of fcn predictions (before iter. inf.) (b, c, 0, 1) or (b, 0, 1) prediction_ii: batch of prediction after iterative inference (b, c, 0, 1) or (b, 0, 1) out_images_folder: folder where to save images tag: str, name of the batch void_label: list of void labels colors: 2d matrix of colors (nclasses, rgb) """ # argmax predictions if needed if prediction_fcn.ndim == 4: prediction_fcn = prediction_fcn.argmax(1) if prediction_ii.ndim == 4: prediction_ii = prediction_ii.argmax(1) if mask_batch.ndim == 4: mask_batch = mask_batch.argmax(1) # apply void mask if needed if any(void_label): prediction_fcn[(mask_batch == void_label).nonzero()] = void_label[0] prediction_ii[(mask_batch == void_label)] = void_label[0] # fix img range if needed if image_batch.max() >= 1.0: image_batch /= 255 color_map = [tuple(el) for el in colors] # prepare image to save for each element in batch images = [] for j in xrange(prediction_ii.shape[0]): img = image_batch[j].transpose((1, 2, 0)) # convert labels to rgb label_prediction_fcn = my_label2rgb(prediction_fcn[j], colors=color_map) label_prediction_ii = my_label2rgb(prediction_ii[j], colors=color_map) # put predictions on top of images pred_fcn_on_img = my_label2rgboverlay(prediction_fcn[j], colors=color_map, image=img, alpha=0.2) pred_ii_on_img = my_label2rgboverlay(prediction_ii[j], colors=color_map, image=img, alpha=0.2) # put gt on top of image mask_on_img = my_label2rgboverlay(mask_batch[j], colors=color_map, image=img, alpha=0.2) if img.shape[2] == 1: img = gray2rgb(img.squeeze()) # combine images combined_image = np.concatenate( (img, mask_on_img, pred_fcn_on_img, pred_ii_on_img), axis=1) # prepare filename and save image out_name = os.path.join(out_images_folder, tag + '_img' + str(j)) np.savez(out_name + '.npz', combined_image) scipy.misc.toimage(combined_image).save(out_name + '.png') images.append(combined_image) return images
return output def hboost(im): #highboost kernel=np.array([[-1,-1,-1],[-1,17,-1],[-1,-1,-1]]) ker=np.flipud(np.fliplr(kernel)) output=np.zeros_like(im) impad=np.zeros((im.shape[0]+4,im.shape[1]+4)) impad[2:-2,2:-2]=im for x in xrange(im.shape[1]): for y in range(im.shape[0]): output[y,x]=(ker*impad[y:y+3,x:x+3]).sum() return output #laplace RGB newimage=laplace(img) #st=np.array(newimage) stacim=color.gray2rgb(newimage) plt.axis("off") plt.imshow(stacim) plt.show() #Blur neimage=average(img) plt.axis("off") plt.imshow(neimage,cmap=plt.get_cmap('gray')) plt.show() #horizontal himage=shor(img) plt.axis("off") plt.imshow(himage,cmap=plt.get_cmap('gray')) plt.show() #vertical vimage=sver(img)
# Load picture and detect edges image = img_as_ubyte(data.coins()[0:95, 70:370]) edges = canny(image, sigma=3, low_threshold=10, high_threshold=50) fig, ax = plt.subplots(ncols=1, nrows=1, figsize=(5, 2)) # Detect two radii hough_radii = np.arange(15, 30, 2) hough_res = hough_circle(edges, hough_radii) centers = [] accums = [] radii = [] for radius, h in zip(hough_radii, hough_res): # For each radius, extract two circles num_peaks = 2 peaks = peak_local_max(h, num_peaks=num_peaks) centers.extend(peaks) accums.extend(h[peaks[:, 0], peaks[:, 1]]) radii.extend([radius] * num_peaks) # Draw the most prominent 5 circles image = color.gray2rgb(image) for idx in np.argsort(accums)[::-1][:5]: center_x, center_y = centers[idx] radius = radii[idx] cx, cy = circle_perimeter(center_y, center_x, radius) image[cy, cx] = (220, 20, 20) ax.imshow(image, cmap=plt.cm.gray)
def __getitem__(self, idx): image = io.imread(self.rgb_files[idx]) if len(image.shape) < 3: image = color.gray2rgb(image) focal_length = float(np.loadtxt(self.calibration_files[idx])) # image will be normalized to standard height, adjust focal length as well f_scale_factor = self.image_height / image.shape[0] focal_length *= f_scale_factor pose = np.loadtxt(self.pose_files[idx]) pose = torch.from_numpy(pose).float() if self.init: if self.sparse: coords = torch.load(self.coord_files[idx]) else: depth = io.imread(self.coord_files[idx]) depth = depth.astype(np.float64) depth /= 1000 # from millimeters to meters elif self.eye: coords = torch.load(self.coord_files[idx]) else: coords = 0 if self.augment: scale_factor = random.uniform(self.aug_scale_min, self.aug_scale_max) angle = random.uniform(-self.aug_rotation, self.aug_rotation) # augment input image cur_image_transform = transforms.Compose([ transforms.ToPILImage(), transforms.Resize(int(self.image_height * scale_factor)), transforms.Grayscale(), transforms.ColorJitter(brightness=self.aug_brightness, contrast=self.aug_contrast), transforms.ToTensor(), transforms.Normalize(mean=[0.4], std=[0.25]) ]) image = cur_image_transform(image) # scale focal length focal_length *= scale_factor # rotate input image def my_rot(t, angle, order, mode='constant'): t = t.permute(1, 2, 0).numpy() t = rotate(t, angle, order=order, mode=mode) t = torch.from_numpy(t).permute(2, 0, 1).float() return t image = my_rot(image, angle, 1, 'reflect') if self.init: if self.sparse: # rotate and scale initalization targets coords_w = math.ceil( image.size(2) / Network.OUTPUT_SUBSAMPLE) coords_h = math.ceil( image.size(1) / Network.OUTPUT_SUBSAMPLE) coords = F.interpolate(coords.unsqueeze(0), size=(coords_h, coords_w))[0] coords = my_rot(coords, angle, 0) else: # rotate and scale depth maps depth = resize(depth, image.shape[1:], order=0) depth = rotate(depth, angle, order=0, mode='constant') # rotate ground truth camera pose angle = angle * math.pi / 180 pose_rot = torch.eye(4) pose_rot[0, 0] = math.cos(angle) pose_rot[0, 1] = -math.sin(angle) pose_rot[1, 0] = math.sin(angle) pose_rot[1, 1] = math.cos(angle) pose = torch.matmul(pose, pose_rot) else: image = self.image_transform(image) if self.init and not self.sparse: # generate initialization targets from depth map offsetX = int(Network.OUTPUT_SUBSAMPLE / 2) offsetY = int(Network.OUTPUT_SUBSAMPLE / 2) coords = torch.zeros( (3, math.ceil(image.shape[1] / Network.OUTPUT_SUBSAMPLE), math.ceil(image.shape[2] / Network.OUTPUT_SUBSAMPLE))) # subsample to network output size depth = depth[offsetY::Network.OUTPUT_SUBSAMPLE, offsetX::Network.OUTPUT_SUBSAMPLE] # construct x and y coordinates of camera coordinate xy = self.prediction_grid[:, :depth.shape[0], :depth. shape[1]].copy() # add random pixel shift xy[0] += offsetX xy[1] += offsetY # substract principal point (assume image center) xy[0] -= image.shape[2] / 2 xy[1] -= image.shape[1] / 2 # reproject xy /= focal_length xy[0] *= depth xy[1] *= depth # assemble camera coordinates trensor eye = np.ndarray((4, depth.shape[0], depth.shape[1])) eye[0:2] = xy eye[2] = depth eye[3] = 1 # eye to scene coordinates sc = np.matmul(pose.numpy(), eye.reshape(4, -1)) sc = sc.reshape(4, depth.shape[0], depth.shape[1]) # mind pixels with invalid depth sc[:, depth == 0] = 0 sc[:, depth > 1000] = 0 sc = torch.from_numpy(sc[0:3]) coords[:, :sc.shape[1], :sc.shape[2]] = sc return image, pose, coords, focal_length, self.rgb_files[idx]
def main(): args = get_args() # Get context from nnabla.ext_utils import get_extension_context logger.info("Running in %s" % args.context) ctx = get_extension_context(args.context, device_id=args.device_id, type_config=args.type_config) nn.set_default_context(ctx) nn.set_auto_forward(True) image = io.imread(args.test_image) if image.ndim == 2: image = color.gray2rgb(image) elif image.shape[-1] == 4: image = image[..., :3] if args.context == 'cudnn': if not os.path.isfile(args.cnn_face_detction_model): # Block of bellow code will download the cnn based face-detection model file provided by dlib for face detection # and will save it in the directory where this script is executed. print("Downloading the face detection CNN. Please wait...") url = "http://dlib.net/files/mmod_human_face_detector.dat.bz2" from nnabla.utils.data_source_loader import download download(url, url.split('/')[-1], False) # get the decompressed data. data = bz2.BZ2File(url.split('/')[-1]).read() # write to dat file. open(url.split('/')[-1][:-4], 'wb').write(data) face_detector = dlib.cnn_face_detection_model_v1( args.cnn_face_detction_model) detected_faces = face_detector( cv2.cvtColor(image[..., ::-1].copy(), cv2.COLOR_BGR2GRAY)) detected_faces = [[ d.rect.left(), d.rect.top(), d.rect.right(), d.rect.bottom() ] for d in detected_faces] else: face_detector = dlib.get_frontal_face_detector() detected_faces = face_detector( cv2.cvtColor(image[..., ::-1].copy(), cv2.COLOR_BGR2GRAY)) detected_faces = [[d.left(), d.top(), d.right(), d.bottom()] for d in detected_faces] if len(detected_faces) == 0: print("Warning: No faces were detected.") return None # Load FAN weights with nn.parameter_scope("FAN"): print("Loading FAN weights...") nn.load_parameters(args.model) # Load ResNetDepth weights if args.landmarks_type_3D: with nn.parameter_scope("ResNetDepth"): print("Loading ResNetDepth weights...") nn.load_parameters(args.resnet_depth_model) landmarks = [] for i, d in enumerate(detected_faces): center = [d[2] - (d[2] - d[0]) / 2.0, d[3] - (d[3] - d[1]) / 2.0] center[1] = center[1] - (d[3] - d[1]) * 0.12 scale = (d[2] - d[0] + d[3] - d[1]) / args.reference_scale inp = crop(image, center, scale) inp = nn.Variable.from_numpy_array(inp.transpose((2, 0, 1))) inp = F.reshape(F.mul_scalar(inp, 1 / 255.0), (1, ) + inp.shape) with nn.parameter_scope("FAN"): out = fan(inp, args.network_size)[-1] pts, pts_img = get_preds_fromhm(out, center, scale) pts, pts_img = F.reshape(pts, (68, 2)) * \ 4, F.reshape(pts_img, (68, 2)) if args.landmarks_type_3D: heatmaps = np.zeros((68, 256, 256), dtype=np.float32) for i in range(68): if pts.d[i, 0] > 0: heatmaps[i] = draw_gaussian(heatmaps[i], pts.d[i], 2) heatmaps = nn.Variable.from_numpy_array(heatmaps) heatmaps = F.reshape(heatmaps, (1, ) + heatmaps.shape) with nn.parameter_scope("ResNetDepth"): depth_pred = F.reshape( resnet_depth(F.concatenate(inp, heatmaps, axis=1)), (68, 1)) pts_img = F.concatenate(pts_img, depth_pred * (1.0 / (256.0 / (200.0 * scale))), axis=1) landmarks.append(pts_img.d) visualize(landmarks, image, args.output)
def convertToRGB(self, _image): return color.gray2rgb(_image)
resized_img = skimage.transform.resize(im, (224, 224), mode='reflect') img = resized_img.flatten() img = img[np.newaxis, :] if count == 0: X = img count = 1 else: X = np.vstack((X, img)) label = filename.replace('.bmp', '').split('_') y = np.append(y, label[0]) print(X.shape) print(y.shape) print("Complicate!") X = np.reshape(X, (len(X), 224, 224)) X = gray2rgb(X) def conbine_img(x, y): comb_x = [] comb_y = [] count = 0 for i in range(len(x)): if i == (len(x) - 1): break else: for j in range(len(x) - i - 1): ori_img = x[i] compare_img = x[j + i + 1] # img = np.zeros((1,224,224,2)) img = ori_img + compare_img
def image_segmentation(in_file_name, out_file_name, show_image): #example_ni1 = os.path.join(data_path, in_file_name) n1_img = nib.load(in_file_name) img_data = n1_img.get_data() print(img_data.shape) #save_example_ni1 = os.path.join(data_path, out_file_name) slice = np.zeros((176, 176, 208)) segm = np.zeros((176, 176, 208)) for i in range(175): slice[i] = img_data[:, :, i, 0] slice[i] = exposure.rescale_intensity(slice[i], out_range=(0, 256)) img = color.gray2rgb(slice[i]) if (img.min() >= 0): labels1 = segmentation.slic(img, compactness=30, n_segments=400, multichannel=False) out1 = color.label2rgb(labels1, img, kind='avg') #g = graph.rag_mean_color(img, labels1, mode='similarity') #labels2 = graph.cut_normalized(labels1, g) #out2 = color.label2rgb(labels2, img, kind='avg') segm[i] = color.rgb2gray(out2) #segm[i] = out1 if (show_image): show_slices([slice[100], slice[110], slice[120]]) plt.suptitle("slices") for i in range(175): img_data[:, :, i, 0] = segm[i] if (show_image): # display results fig, (ax1, ax2, ax3) = plt.subplots(nrows=1, ncols=3, figsize=(8, 3), sharex=True, sharey=True) ax1.imshow(img_data[:, :, 100, 0]) ax1.axis('off') ax1.set_title('image 100', fontsize=20) ax2.imshow(img_data[:, :, 110, 0]) ax2.axis('off') ax2.set_title('image 110', fontsize=20) ax3.imshow(img_data[:, :, 120, 0]) ax3.axis('off') ax3.set_title('image 120', fontsize=20) fig.subplots_adjust(wspace=0.02, hspace=0.02, top=0.9, bottom=0.02, left=0.02, right=0.98) plt.show() save_img = nib.Nifti1Image(img_data, np.eye(4)) nib.save(save_img, save_example_ni1)
In 2D, color images are often represented in RGB---3 layers of 2D arrays, where the 3 layers represent (R)ed, (G)reen and (B)lue channels of the image. The simplest way of getting a tinted image is to set each RGB channel to the grayscale image scaled by a different multiplier for each channel. For example, multiplying the green and blue channels by 0 leaves only the red channel and produces a bright red image. Similarly, zeroing-out the blue channel leaves only the red and green channels, which combine to form yellow. """ import matplotlib.pyplot as plt from skimage import data from skimage import color from skimage import img_as_float grayscale_image = img_as_float(data.camera()[::2, ::2]) image = color.gray2rgb(grayscale_image) red_multiplier = [1, 0, 0] yellow_multiplier = [1, 1, 0] fig, (ax1, ax2) = plt.subplots(ncols=2, figsize=(8, 4), sharex=True, sharey=True) ax1.imshow(red_multiplier * image) ax2.imshow(yellow_multiplier * image) ###################################################################### # In many cases, dealing with RGB values may not be ideal. Because of that, # there are many other `color spaces`_ in which you can represent a color # image. One popular color space is called HSV, which represents hue (~the
glob.glob( '../../input/petfinder-adoption-prediction/train_images/*.jpg')) test_image_files = sorted( glob.glob( '../../input/petfinder-adoption-prediction/test_images/*.jpg')) image_files = train_image_files + test_image_files train_images = pd.DataFrame(image_files, columns=['image_filename']) train_images['PetID'] = train_images['image_filename'].apply( lambda x: x.split('/')[-1].split('-')[0]) with timer('img_basic'): features = [] for i, (file_path, pet_id) in enumerate(train_images.values): im = io.imread(file_path) if len(im.shape) == 2: im = gray2rgb(im) avg = list(avg_c(im).flatten().astype(np.float32)) color_list = ["red", "blue", "green"] whiteness_, dullness_ = [], [] for i, color in enumerate(color_list): whiteness, dullness = calc_brightness(im[:, :, i], 20, 240) whiteness_ += [whiteness] dullness_ += [dullness] blu = get_blurrness_score(im) features.append(avg + whiteness_ + dullness_ + [blu, pet_id]) color_list = ["red", "blue", "green"] cols = ["avg_" + color for color in color_list] + ["whiteness_" + color for color in color_list] + \ ["dullness_" + color for color in color_list] + ["blurrness", "PetID"] X = pd.DataFrame(features, columns=cols)
def crf(original_image, annotated_image, output_image, use_2d=True): # Converting annotated image to RGB if it is Gray scale if (len(annotated_image.shape) < 3): annotated_image = gray2rgb(annotated_image) imsave("testing5.png", annotated_image) #Converting the annotations RGB color to single 32 bit integer annotated_label = annotated_image[:, :, 0] + ( annotated_image[:, :, 1] << 8) + (annotated_image[:, :, 2] << 16) # Convert the 32bit integer color to 0,1, 2, ... labels. colors, labels = np.unique(annotated_label, return_inverse=True) #Creating a mapping back to 32 bit colors colorize = np.empty((len(colors), 3), np.uint8) colorize[:, 0] = (colors & 0x0000FF) colorize[:, 1] = (colors & 0x00FF00) >> 8 colorize[:, 2] = (colors & 0xFF0000) >> 16 #Gives no of class labels in the annotated image n_labels = len(set(labels.flat)) print("No of labels in the Image are ") print(n_labels) #Setting up the CRF model if use_2d: d = dcrf.DenseCRF2D(original_image.shape[1], original_image.shape[0], n_labels) # get unary potentials (neg log probability) U = unary_from_labels(labels, n_labels, gt_prob=0.7, zero_unsure=False) d.setUnaryEnergy(U) # This adds the color-independent term, features are the locations only. d.addPairwiseGaussian(sxy=(3, 3), compat=3, kernel=dcrf.DIAG_KERNEL, normalization=dcrf.NORMALIZE_SYMMETRIC) # This adds the color-dependent term, i.e. features are (x,y,r,g,b). d.addPairwiseBilateral(sxy=(80, 80), srgb=(13, 13, 13), rgbim=original_image, compat=10, kernel=dcrf.DIAG_KERNEL, normalization=dcrf.NORMALIZE_SYMMETRIC) #Run Inference for 5 steps Q = d.inference(5) # Find out the most probable class for each pixel. MAP = np.argmax(Q, axis=0) # Convert the MAP (labels) back to the corresponding colors and save the image. # Note that there is no "unknown" here anymore, no matter what we had at first. MAP = colorize[MAP, :] imsave(output_image, MAP.reshape(original_image.shape)) return MAP.reshape(original_image.shape)
def explain_instance(self, image, classifier_fn, labels=(1, ), hide_color=None, top_labels=5, num_features=100000, num_samples=1000, batch_size=10, segmentation_fn=None, distance_metric='cosine', model_regressor=None, random_seed=None): """Generates explanations for a prediction. First, we generate neighborhood data by randomly perturbing features from the instance (see __data_inverse). We then learn locally weighted linear models on this neighborhood data to explain each of the classes in an interpretable way (see lime_base.py). Args: image: 3 dimension RGB image. If this is only two dimensional, we will assume it's a grayscale image and call gray2rgb. classifier_fn: classifier prediction probability function, which takes a numpy array and outputs prediction probabilities. For ScikitClassifiers , this is classifier.predict_proba. labels: iterable with labels to be explained. hide_color: TODO top_labels: if not None, ignore labels and produce explanations for the K labels with highest prediction probabilities, where K is this parameter. num_features: maximum number of features present in explanation num_samples: size of the neighborhood to learn the linear model batch_size: TODO distance_metric: the distance metric to use for weights. model_regressor: sklearn regressor to use in explanation. Defaults to Ridge regression in LimeBase. Must have model_regressor.coef_ and 'sample_weight' as a parameter to model_regressor.fit() segmentation_fn: SegmentationAlgorithm, wrapped skimage segmentation function random_seed: integer used as random seed for the segmentation algorithm. If None, a random integer, between 0 and 1000, will be generated using the internal random number generator. Returns: An Explanation object (see explanation.py) with the corresponding explanations. """ if len(image.shape) == 2: image = gray2rgb(image) if random_seed is None: random_seed = self.random_state.randint(0, high=1000) if segmentation_fn is None: segmentation_fn = SegmentationAlgorithm('quickshift', kernel_size=4, max_dist=200, ratio=0.2, random_seed=random_seed) try: segments = segmentation_fn(image) except ValueError as e: raise e fudged_image = image.copy() if hide_color is None: for x in np.unique(segments): fudged_image[segments == x] = (np.mean( image[segments == x][:, 0]), np.mean( image[segments == x][:, 1]), np.mean( image[segments == x][:, 2])) else: fudged_image[:] = hide_color top = labels data, labels = self.data_labels(image, fudged_image, segments, classifier_fn, num_samples, batch_size=batch_size) distances = sklearn.metrics.pairwise_distances( data, data[0].reshape(1, -1), metric=distance_metric).ravel() ret_exp = ImageExplanation(image, segments) if top_labels: top = np.argsort(labels[0])[-top_labels:] ret_exp.top_labels = list(top) ret_exp.top_labels.reverse() for label in top: (ret_exp.intercept[label], ret_exp.local_exp[label], ret_exp.score, ret_exp.local_pred) = self.base.explain_instance_with_data( data, labels, distances, label, num_features, model_regressor=model_regressor, feature_selection=self.feature_selection) return ret_exp
print('Building ground-truth array') labels = segmentation.slic(the_img, compactness=30, n_segments=slic_segments) this_y = list() for j in np.unique(labels): this_sp_mask = np.where(labels == j) this_gt_sp = the_gt[this_sp_mask[0], this_sp_mask[1]] this_y.append(hp.value_to_class(this_gt_sp, foreground_threshold)) Yi.append(np.asarray(this_y)) Yi_flat = np.asarray([y_.ravel() for y_ in Yi]) conf_mat = metrics.confusion_matrix(Yi_flat.ravel(), Zi_flat[0].ravel()) TPR = conf_mat[0][0] / (conf_mat[0][0] + conf_mat[0][1]) FPR = conf_mat[1][0] / (conf_mat[1][0] + conf_mat[1][1]) print('TPR/FPR = ' + str(TPR) + '/' + str(FPR)) # Display prediction as an image w = gt_imgs[img_idx].shape[0] h = gt_imgs[img_idx].shape[1] predicted_im = hp.sp_label_to_img(labels, Zi[0]) img_overlay = color.label2rgb(predicted_im, the_img, alpha=0.5) img_over_conc = hp.concatenate_images(img_overlay, color.gray2rgb(gt_imgs[img_idx])) predicted_labels = hp.concatenate_images(img_overlay, color.gray2rgb(predicted_im)) fig1 = plt.figure(figsize=(10, 10)) # create a figure with the default size plt.imshow(img_over_conc, cmap='Greys_r') plt.title('im ' + str(img_idx) + ', ' + my_classifier.__class__.__name__ + 'TPR/FPR = ' + str(TPR) + '/' + str(FPR)) plt.show()
t_ellipse = time.time() result = hough_ellipse(edges, accuracy=20, threshold=250, min_size=100, max_size=120) print(f'hough_ellipse: {time.time()-t_ellipse}') print(f'Total: {time.time()-t0}') result.sort(order='accumulator') # Estimated parameters for the ellipse best = list(result[-1]) yc, xc, a, b = [int(round(x)) for x in best[1:5]] orientation = best[5] # Draw the ellipse on the original image cy, cx = ellipse_perimeter(yc, xc, a, b, orientation) image_rgb[cy, cx] = (0, 0, 255) # Draw the edge (white) and the resulting ellipse (red) edges = color.gray2rgb(img_as_ubyte(edges)) edges[cy, cx] = (250, 0, 0) fig2, (ax1, ax2) = plt.subplots(ncols=2, nrows=1, figsize=(8, 4), sharex=True, sharey=True) ax1.set_title('Original picture') ax1.imshow(image_rgb) ax2.set_title('Edge (white) and result (red)') ax2.imshow(edges) plt.show()
"""Salvar el modelo""" model_json = model.to_json() with open("model.json", "w") as json_file: json_file.write(model_json) model.save_weights("color_tensorflow_real_mode.h5") """Probar el modelo con imagenes de test""" color_me = [] for filename in os.listdir('test/'): color_me.append(img_to_array(load_img('test/'+filename))) color_me = np.array(color_me, dtype=float) color_me = 1.0/255*color_me color_me = gray2rgb(rgb2gray(color_me)) color_me_embed = create_inception_embedding(color_me) color_me = rgb2lab(color_me)[:,:,:,0] color_me = color_me.reshape(color_me.shape+(1,)) # Test model output = model.predict([color_me, color_me_embed]) output = output * 128 # Output colorizations for i in range(len(output)): cur = np.zeros((256, 256, 3)) cur[:,:,0] = color_me[i][:,:,0] cur[:,:,1:] = output[i] imsave("result/img_"+str(i)+".png", lab2rgb(cur))
def __getitem__(self, idx): image = io.imread(self.rgb_files[idx]) if len(image.shape) < 3: image = color.gray2rgb(image) focal_length = float(self.calibration_data[idx][0]) # image will be normalized to standard height, adjust focal length as well f_scale_factor = self.image_height / image.shape[0] focal_length *= f_scale_factor # pose = np.loadtxt(self.pose_files[idx]) pose = torch.from_numpy(self.pose_data[idx]).float() # for jellyfish coords are none coords = 0 if self.augment: scale_factor = random.uniform(self.aug_scale_min, self.aug_scale_max) # scale focal length focal_length *= scale_factor angle = random.uniform(-self.aug_rotation, self.aug_rotation) # get the intrinsics and lens distortion camera_intrinsics = self.calibration_data[idx][0:4] distortion_coeffs = self.calibration_data[idx][4:] # augment input image pipeline = transforms.Compose([ transforms.Lambda(lambda img: tr.unfish( img, camera_intrinsics, distortion_coeffs)), transforms.Lambda(lambda img: tr.cambridgify(img)), transforms.ToPILImage(), transforms.Resize(int(self.image_height * scale_factor)), transforms.Grayscale(), transforms.ColorJitter(brightness=self.aug_brightness, contrast=self.aug_contrast), transforms.ToTensor() ]) image = pipeline(image) # rotate image # image = tr.rotate(image, angle, 1, 'reflect') # # # rotate ground truth camera pose # angle = angle * math.pi / 180 # pose_rot = torch.eye(4) # pose_rot[0, 0] = math.cos(angle) # pose_rot[0, 1] = -math.sin(angle) # pose_rot[1, 0] = math.sin(angle) # pose_rot[1, 1] = math.cos(angle) # pose = torch.matmul(pose, pose_rot) if self.init: # rotate and scale depth maps depth = resize(depth, image.shape[1:], order=0) depth = rotate(depth, angle, order=0, mode='constant') else: pipeline = transforms.Compose([ transforms.ToPILImage(), transforms.Resize(self.image_height), transforms.Grayscale(), transforms.ToTensor() ]) image = pipeline(image) if self.init and not self.sparse: # generate initialization targets from depth map offsetX = int(Network.OUTPUT_SUBSAMPLE / 2) offsetY = int(Network.OUTPUT_SUBSAMPLE / 2) coords = torch.zeros( (3, math.ceil(image.shape[1] / Network.OUTPUT_SUBSAMPLE), math.ceil(image.shape[2] / Network.OUTPUT_SUBSAMPLE))) # subsample to network output size depth = depth[offsetY::Network.OUTPUT_SUBSAMPLE, offsetX::Network.OUTPUT_SUBSAMPLE] # construct x and y coordinates of camera coordinate xy = self.prediction_grid[:, :depth.shape[0], :depth. shape[1]].copy() # add random pixel shift xy[0] += offsetX xy[1] += offsetY # substract principal point (assume image center) xy[0] -= image.shape[2] / 2 xy[1] -= image.shape[1] / 2 # reproject xy /= focal_length xy[0] *= depth xy[1] *= depth # assemble camera coordinates trensor eye = np.ndarray((4, depth.shape[0], depth.shape[1])) eye[0:2] = xy eye[2] = depth eye[3] = 1 # eye to scene coordinates sc = np.matmul(pose.numpy(), eye.reshape(4, -1)) sc = sc.reshape(4, depth.shape[0], depth.shape[1]) # mind pixels with invalid depth sc[:, depth == 0] = 0 sc[:, depth > 1000] = 0 sc = torch.from_numpy(sc[0:3]) coords[:, :sc.shape[1], :sc.shape[2]] = sc return image, pose, coords, focal_length, self.timestamps[ idx], self.rgb_files[idx]
def make_labeled_images_from_dataframe( df, cfg, destfolder="", scale=1.0, dpi=100, keypoint="+", draw_skeleton=True, color_by="bodypart", ): """ Write labeled frames to disk from a DataFrame. Parameters ---------- df : pd.DataFrame DataFrame containing the labeled data. Typically, the DataFrame is obtained through pandas.read_csv() or pandas.read_hdf(). cfg : dict Project configuration. destfolder : string, optional Destination folder into which images will be stored. By default, same location as the labeled data. Note that the folder will be created if it does not exist. scale : float, optional Up/downscale the output dimensions. By default, outputs are of the same dimensions as the original images. dpi : int, optional Output resolution. 100 dpi by default. keypoint : str, optional Keypoint appearance. By default, keypoints are marked by a + sign. Refer to https://matplotlib.org/3.2.1/api/markers_api.html for a list of all possible options. draw_skeleton : bool, optional Whether to draw the animal skeleton as defined in *cfg*. True by default. color_by : str, optional Color scheme of the keypoints. Must be either 'bodypart' or 'individual'. By default, keypoints are colored relative to the bodypart they represent. """ bodyparts = df.columns.get_level_values("bodyparts") bodypart_names = bodyparts.unique() nbodyparts = len(bodypart_names) bodyparts = bodyparts[::2] draw_skeleton = (draw_skeleton and cfg["skeleton"]) # Only draw if a skeleton is defined if color_by == "bodypart": map_ = bodyparts.map(dict(zip(bodypart_names, range(nbodyparts)))) cmap = get_cmap(nbodyparts, cfg["colormap"]) colors = cmap(map_) elif color_by == "individual": try: individuals = df.columns.get_level_values("individuals") individual_names = individuals.unique().to_list() nindividuals = len(individual_names) individuals = individuals[::2] map_ = individuals.map( dict(zip(individual_names, range(nindividuals)))) cmap = get_cmap(nindividuals, cfg["colormap"]) colors = cmap(map_) except KeyError as e: raise Exception( "Coloring by individuals is only valid for multi-animal data" ) from e else: raise ValueError( "`color_by` must be either `bodypart` or `individual`.") bones = [] if draw_skeleton: for bp1, bp2 in cfg["skeleton"]: match1, match2 = [], [] for j, bp in enumerate(bodyparts): if bp == bp1: match1.append(j) elif bp == bp2: match2.append(j) bones.extend(zip(match1, match2)) ind_bones = tuple(zip(*bones)) sep = "/" if "/" in df.index[0] else "\\" images = cfg["project_path"] + sep + df.index if sep != os.path.sep: images = images.str.replace(sep, os.path.sep) if not destfolder: destfolder = os.path.dirname(images[0]) tmpfolder = destfolder + "_labeled" attempttomakefolder(tmpfolder) images_list = images.to_list() ic = io.imread_collection(images_list) h, w = ic[0].shape[:2] all_same_shape = True for array in ic[1:]: if array.shape[:2] != (h, w): all_same_shape = False break xy = df.values.reshape((df.shape[0], -1, 2)) segs = xy[:, ind_bones].swapaxes(1, 2) s = cfg["dotsize"] alpha = cfg["alphavalue"] if all_same_shape: # Very efficient, avoid re-drawing the whole plot fig, ax = prepare_figure_axes(w, h, scale, dpi) im = ax.imshow(np.zeros((h, w)), "gray") pts = [ ax.plot([], [], keypoint, ms=s, alpha=alpha, color=c)[0] for c in colors ] coll = LineCollection([], colors=cfg["skeleton_color"], alpha=alpha) ax.add_collection(coll) for i in trange(len(ic)): filename = ic.files[i] ind = images_list.index(filename) coords = xy[ind] img = ic[i] if img.ndim == 2 or img.shape[-1] == 1: img = color.gray2rgb(ic[i]) im.set_data(img) for pt, coord in zip(pts, coords): pt.set_data(*coord) if ind_bones: coll.set_segments(segs[ind]) imagename = os.path.basename(filename) fig.subplots_adjust(left=0, bottom=0, right=1, top=1, wspace=0, hspace=0) fig.savefig( os.path.join(tmpfolder, imagename.replace(".png", f"_{color_by}.png")), dpi=dpi, ) plt.close(fig) else: # Good old inelegant way for i in trange(len(ic)): filename = ic.files[i] ind = images_list.index(filename) coords = xy[ind] image = ic[i] h, w = image.shape[:2] fig, ax = prepare_figure_axes(w, h, scale, dpi) ax.imshow(image) for coord, c in zip(coords, colors): ax.plot(*coord, keypoint, ms=s, alpha=alpha, color=c) if ind_bones: coll = LineCollection(segs[ind], colors=cfg["skeleton_color"], alpha=alpha) ax.add_collection(coll) imagename = os.path.basename(filename) fig.subplots_adjust(left=0, bottom=0, right=1, top=1, wspace=0, hspace=0) fig.savefig( os.path.join(tmpfolder, imagename.replace(".png", f"_{color_by}.png")), dpi=dpi, ) plt.close(fig)
hough_radii = np.arange(15, 30, 2) hough_res = hough_circle(edges, hough_radii) centers = [] accums = [] radii = [] for radius, h in zip(hough_radii, hough_res): # For each radius, extract two circles peaks = peak_local_max(h, num_peaks=2) centers.extend(peaks) accums.extend(h[peaks[:, 0], peaks[:, 1]]) radii.extend([radius, radius]) # Draw the most prominent 5 circles image = color.gray2rgb(image) for idx in np.argsort(accums)[::-1][:5]: center_x, center_y = centers[idx] radius = radii[idx] cx, cy = circle_perimeter(center_y, center_x, radius) image[cy, cx] = (220, 20, 20) ax.imshow(image, cmap=plt.cm.gray) """ .. image:: PLOT2RST.current_figure Ellipse detection ================= In this second example, the aim is to detect the edge of a coffee cup. Basically, this is a projection of a circle, i.e. an ellipse.
import numpy as np import cv2 import matplotlib.pyplot as plt from PIL import Image from skimage import io, data, color # 灰度图转彩,染色后转灰 basepath = "C:\\Users\\Dudley\\Desktop\\data vis\\SegmentationClass\\" savepath = "C:\\Users\\Dudley\\Desktop\\data vis\\8bit_png\\" f_n = os.listdir(basepath) print(f_n) for n in f_n: imdir = basepath + '\\' + n img = cv2.imread(imdir) img = color.gray2rgb(img) rows, cols, chn = img.shape b = img[:, :, 0] g = np.zeros((rows, cols), dtype=img.dtype) r = np.zeros((rows, cols), dtype=img.dtype) m = cv2.merge([b, g, r]) m = cv2.cvtColor(m, cv2.COLOR_BGR2GRAY) # cv2.imshow('merge', m) # cv2.waitKey(0) # cv2.destroyAllWindows() print(m.dtype) cv2.imwrite(savepath + n, m)
def get_saliency_rbd(img_path): # Saliency map calculation based on: # Saliency Optimization from Robust Background Detection, Wangjiang Zhu, Shuang Liang, Yichen Wei and Jian Sun, IEEE Conference on Computer Vision and Pattern Recognition (CVPR), 2014 img = cv2.imread(img_path) if len(img.shape) != 3: # got a grayscale image img = gray2rgb(img) img_lab = img_as_float(rgb2lab(img)) img_rgb = img_as_float(img) img_gray = img_as_float(rgb2gray(img)) # p=SLIC(img_rgb,250,10); # segments_slic = p.iterate_times(5) segments_slic=slic(img_rgb, n_segments=250, compactness=10, sigma=1, enforce_connectivity=False) # print 'hello' num_segments = len(np.unique(segments_slic)) nrows, ncols = segments_slic.shape max_dist = math.sqrt(nrows*nrows + ncols*ncols) grid = segments_slic (vertices,edges) = make_graph(grid) gridx, gridy = np.mgrid[:grid.shape[0], :grid.shape[1]] centers = dict() colors = dict() distances = dict() boundary = dict() for v in vertices: centers[v] = [gridy[grid == v].mean(), gridx[grid == v].mean()] colors[v] = np.mean(img_lab[grid==v],axis=0) x_pix = gridx[grid == v] y_pix = gridy[grid == v] if np.any(x_pix == 0) or np.any(y_pix == 0) or np.any(x_pix == nrows - 1) or np.any(y_pix == ncols - 1): boundary[v] = 1 else: boundary[v] = 0 G = nx.Graph() #buid the graph for edge in edges: pt1 = edge[0] pt2 = edge[1] color_distance = scipy.spatial.distance.euclidean(colors[pt1],colors[pt2]) G.add_edge(pt1, pt2, weight=color_distance ) #add a new edge in graph if edges are both on boundary for v1 in vertices: if boundary[v1] == 1: for v2 in vertices: if boundary[v2] == 1: color_distance = scipy.spatial.distance.euclidean(colors[v1],colors[v2]) G.add_edge(v1,v2,weight=color_distance) geodesic = np.zeros((len(vertices),len(vertices)),dtype=float) spatial = np.zeros((len(vertices),len(vertices)),dtype=float) smoothness = np.zeros((len(vertices),len(vertices)),dtype=float) adjacency = np.zeros((len(vertices),len(vertices)),dtype=float) sigma_clr = 10.0 sigma_bndcon = 1.0 sigma_spa = 0.25 mu = 0.1 all_shortest_paths_color = nx.shortest_path(G,source=None,target=None,weight='weight') for v1 in vertices: for v2 in vertices: if v1 == v2: geodesic[v1,v2] = 0 spatial[v1,v2] = 0 smoothness[v1,v2] = 0 else: geodesic[v1,v2] = path_length(all_shortest_paths_color[v1][v2],G) spatial[v1,v2] = scipy.spatial.distance.euclidean(centers[v1],centers[v2]) / max_dist smoothness[v1,v2] = math.exp( - (geodesic[v1,v2] * geodesic[v1,v2])/(2.0*sigma_clr*sigma_clr)) + mu for edge in edges: pt1 = edge[0] pt2 = edge[1] adjacency[pt1,pt2] = 1 adjacency[pt2,pt1] = 1 for v1 in vertices: for v2 in vertices: smoothness[v1,v2] = adjacency[v1,v2] * smoothness[v1,v2] area = dict() len_bnd = dict() bnd_con = dict() w_bg = dict() ctr = dict() wCtr = dict() for v1 in vertices: area[v1] = 0 len_bnd[v1] = 0 ctr[v1] = 0 for v2 in vertices: d_app = geodesic[v1,v2] d_spa = spatial[v1,v2] w_spa = math.exp(- ((d_spa)*(d_spa))/(2.0*sigma_spa*sigma_spa)) area_i = S(v1,v2,geodesic) area[v1] += area_i len_bnd[v1] += area_i * boundary[v2] ctr[v1] += d_app * w_spa bnd_con[v1] = len_bnd[v1] / math.sqrt(area[v1]) w_bg[v1] = 1.0 - math.exp(- (bnd_con[v1]*bnd_con[v1])/(2*sigma_bndcon*sigma_bndcon)) for v1 in vertices: wCtr[v1] = 0 for v2 in vertices: d_app = geodesic[v1,v2] d_spa = spatial[v1,v2] w_spa = math.exp(- (d_spa*d_spa)/(2.0*sigma_spa*sigma_spa)) wCtr[v1] += d_app * w_spa * w_bg[v2] # normalise value for wCtr min_value = min(wCtr.values()) max_value = max(wCtr.values()) minVal = [key for key, value in wCtr.iteritems() if value == min_value] maxVal = [key for key, value in wCtr.iteritems() if value == max_value] for v in vertices: wCtr[v] = (wCtr[v] - min_value)/(max_value - min_value) img_disp1 = img_gray.copy() img_disp2 = img_gray.copy() x = compute_saliency_cost(smoothness,w_bg,wCtr) for v in vertices: img_disp1[grid == v] = x[v] img_disp2 = img_disp1.copy() sal = np.zeros((img_disp1.shape[0],img_disp1.shape[1],3)) sal = img_disp2 sal_max = np.max(sal) sal_min = np.min(sal) sal = 255 * ((sal - sal_min) / (sal_max - sal_min)) return sal
def post_processing_local_test(image_path='./test_images/', vis_path='./results/vis_crf', dir_path='./dir_map_test/', prob_path='./epoch2800_test/', out_path='./results/crf', with_prop=False, with_kernel=False): if with_prop: vis_path = vis_path + "_prop" out_path = out_path + "_prop" if with_kernel: vis_path = vis_path + "_kernel" out_path = out_path + "_kernel" for path in [vis_path, out_path]: if not os.path.exists(path): os.mkdir(path) for file in tqdm(os.listdir(image_path)): img = cv2.imread(os.path.join(image_path, file)) prob = cv2.imread(os.path.join(prob_path, file)) prob = prob / 255 prob = prob[:, :, 0] dir_color_map = cv2.imread(os.path.join(dir_path, file)) dir_feature = dir_to_features(dir_color_map) if with_prop: if with_kernel: prop_img = propagate_max_vec(img, prob, prop_num=16, prop_size=5, sigma_color=0.713060162472891, sigma_pos=20) crf_img = crf_with_dir_kernel( img, dir_feature, prop_img, iter_num=4, compat_smooth=np.array([[1.46958173, 0.8307862], [0.71450311, -0.35393216]]), compat_appearance=np.array([[-0.05790994, 1.3304376], [1.32379982, -0.00975676]]), compat_struct=np.array([[-0.40640465, 0.79798697], [0.99422088, -3.48206395]]), w_smooth=1.2092231919137424, w_appearance=1.77501181382847, w_struct=1.2417858524385004, sigma_smooth=2.315722641303978, sigma_app_color=2.043231728337113, sigma_app_pos=40, sigma_struct_pos=173.1556405794435, sigma_struct_feat=2.572299710793928) else: prop_img = propagate_max_vec(img, prob, prop_num=16, prop_size=5, sigma_color=0.6650886795908088, sigma_pos=10) crf_img = crf( img, prop_img, iter_num=8, compat_smooth=np.array([[0.24658912, 1.03629577], [0.69663063, -0.31590588]]), compat_appearance=np.array([[-0.31513215, 0.97190996], [1.04959312, -0.47501959]]), w_smooth=1.828328302038134, w_appearance=1.795302766064866, sigma_smooth=1.435892752213356, sigma_app_color=1.78496352847059, sigma_app_pos=80) else: if with_kernel: crf_img = crf_with_dir_kernel( img, dir_feature, prob, iter_num=8, compat_smooth=np.array([[0.2704138, 1.09232546], [0.80253412, -0.18487427]]), compat_appearance=np.array([[-0.37001789, 0.852498725], [1.29555175, -0.2937206]]), compat_struct=np.array([[0.1788232, 0.61148446], [0.1116445, -4.44564896]]), w_smooth=1.6814390659156584, w_appearance=1.83980578425931, w_struct=1.3154124820494024, sigma_smooth=5.692475296551731, sigma_app_color=1.5828168297951695, sigma_app_pos=40, sigma_struct_pos=264.5010753324061, sigma_struct_feat=9.132062312611474) else: crf_img = crf( img, prob, iter_num=8, compat_smooth=np.array([[0.88830681, 0.69689981], [0.54353049, -0.1542836]]), compat_appearance=np.array([[-0.49690445, 1.15925799], [1.22089288, -0.34833315]]), w_smooth=2.2810288259551967, w_appearance=1.90286829269048, sigma_smooth=8.053041617053246, sigma_app_color=1.6955329962509278, sigma_app_pos=80) cv2.imwrite(os.path.join(out_path, file), gray2rgb(np.floor(crf_img * 255))) plt.figure(figsize=(15, 5)) plt.subplot(1, 3, 1) plt.imshow(img) plt.title('Original image') plt.axis('off') plt.subplot(1, 3, 2) plt.imshow(gray2rgb(prob)) plt.title('PSP prob') plt.axis('off') plt.subplot(1, 3, 3) plt.imshow(gray2rgb(crf_img)) plt.title('CRF prob') plt.axis('off') plt.savefig(os.path.join(vis_path, file), dpi=300) plt.close()
def post_processing(image_path='./test_images/', vis_path='./results/vis_crf', dir_path='./dir_map_test/', prob_path='./epoch3000_test/', out_path='./results/crf', with_prop=False, with_kernel=False): if with_prop: vis_path = vis_path + "_prop" out_path = out_path + "_prop" if with_kernel: vis_path = vis_path + "_kernel" out_path = out_path + "_kernel" for path in [vis_path, out_path]: if not os.path.exists(path): os.mkdir(path) for file in tqdm(os.listdir(image_path)): img = cv2.imread(os.path.join(image_path, file)) prob = cv2.imread(os.path.join(prob_path, file)) prob = prob / 255 prob = prob[:, :, 0] dir_color_map = cv2.imread(os.path.join(dir_path, file)) dir_feature = dir_to_features(dir_color_map) if with_prop: if with_kernel: """ # 3000 prop_img = propagate_max_vec(img,prob,prop_num=16, prop_size=5, sigma_color=1.6935536588645517, sigma_pos=20) crf_img = crf_with_dir_kernel(img, dir_feature, prop_img, iter_num=8, compat_smooth=np.array([[0.57842984, 0.95404739],[0.52004501, 0.40422152]]), compat_appearance=np.array([[-0.31741694, 1.18494974],[1.12442186, 0.24456809]]), compat_struct=np.array([[-0.30595129, 0.7728254],[0.12691528, -0.85317293]]), w_smooth=1.6516782004079098, w_appearance=1.529408010866331, w_struct=4.938469494095774, sigma_smooth=4.883141908483301, sigma_app_color=1.5137269619243123, sigma_app_pos=40, sigma_struct_pos=475.9932426424997, sigma_struct_feat=5.37248405378539) """ prop_img = propagate_max_vec(img, prob, prop_num=32, prop_size=11, sigma_color=0.9558387244727098, sigma_pos=10) crf_img = crf_with_dir_kernel( img, dir_feature, prop_img, iter_num=8, compat_smooth=np.array([[0.63928644, 0.7558237], [1.24894913, 0.12096477]]), compat_appearance=np.array([[-0.21364454, 0.86426312], [0.81418789, 0.16731218]]), compat_struct=np.array([[-0.43469763, 0.9034787], [0.55590722, -0.09176918]]), w_smooth=1.0807095786024483, w_appearance=1.1901483892457647, w_struct=2.942751798533171, sigma_smooth=5.899771615934947, sigma_app_color=2.4522940567843348, sigma_app_pos=80, sigma_struct_pos=187.99058882436577, sigma_struct_feat=14.469581394524806) else: prop_img = propagate_max_vec(img, prob, prop_num=32, prop_size=5, sigma_color=0.8648530839870829, sigma_pos=10) crf_img = crf( img, prop_img, iter_num=8, compat_smooth=np.array([[-0.27312429, 1.44344053], [0.94756049, -0.05987938]]), compat_appearance=np.array([[0.30314967, 0.93767007], [1.37851458, 0.14069547]]), w_smooth=2.622061363416207, w_appearance=1.5669198261263837, sigma_smooth=7.2716251558091365, sigma_app_color=2.0988905139201224, sigma_app_pos=10) else: if with_kernel: crf_img = crf_with_dir_kernel( img, dir_feature, prob, iter_num=8, compat_smooth=np.array([[1.42628086, 0.57972011], [1.36363985, 0.21731918]]), compat_appearance=np.array([[-0.43596408, 1.04737195], [1.39793517, 0.07055685]]), compat_struct=np.array([[-0.32739809, 0.45224483], [0.7109288, -0.35132453]]), w_smooth=2.1670176734442625, w_appearance=1.699234108202612, w_struct=4.818667137623257, sigma_smooth=4.835584315772047, sigma_app_color=2.0611781853345224, sigma_app_pos=10, sigma_struct_pos=174.76741408433867, sigma_struct_feat=48.092921007782984) else: crf_img = crf( img, prob, iter_num=8, compat_smooth=np.array([[-0.43588055, 1.49743748], [1.08083823, 0.48265268]]), compat_appearance=np.array([[-0.30344363, 0.62247936], [0.9058573, 0.26622347]]), w_smooth=3.8508739815607615, w_appearance=1.8400845974535944, sigma_smooth=4.980895096538283, sigma_app_color=1.150807401052397, sigma_app_pos=80) cv2.imwrite(os.path.join(out_path, file), gray2rgb(np.floor(crf_img * 255))) plt.figure(figsize=(15, 5)) plt.subplot(1, 3, 1) plt.imshow(img) plt.title('Original image') plt.axis('off') plt.subplot(1, 3, 2) plt.imshow(gray2rgb(prob)) plt.title('PSP prob') plt.axis('off') plt.subplot(1, 3, 3) plt.imshow(gray2rgb(crf_img)) plt.title('CRF prob') plt.axis('off') plt.savefig(os.path.join(vis_path, file), dpi=300) plt.close()
train_Y.append([1, 0]) train_Y.append([1, 0]) test_Y = [] for row in range(0, len(test_csv)): if test_csv.iloc[row]["pathology"] == "MALIGNANT": test_Y.append([0, 1]) else: test_Y.append([1, 0]) train_X_gray = [] for row in range(0, len(train_csv)): picture = exposure.equalize_hist( io.imread(train_dir + (train_csv.iloc[row]["image file path"]).replace("\\", "/"))) picture = color.gray2rgb(picture) train_X_gray.append(picture) train_X_gray.append(np.fliplr(picture)) #insert horizontal flip train_X_gray.append(np.flipud(picture)) #insert vertical flip #train_X_gray.append(skimage.transform.rotate(picture, random.randint(45,90), resize = False,mode= "symmetric")) #rotate between 45 and 90 #train_X_gray.append(skimage.transform.rotate(picture, random.randint(135,180), resize = False,mode= "symmetric")) #rotate between 135 and 180 train_X_gray.append(np.rot90(picture, 1)) train_X_gray.append(np.rot90(picture, 2)) train_X_gray.append(np.rot90(picture, 3)) test_X_gray = [] for row in range(0, len(test_csv)): picture = exposure.equalize_hist( io.imread(test_dir + (test_csv.iloc[row]["image file path"]).replace("\\", "/")) ) #Equalise the hist
def cleanAndReshapeImage(imageArray): # Resize the image into 28x28 img = transform.resize(imageArray, (28, 28)) # Convert the image's RGB to grayscale array grays = color.rgb2gray(img).flatten() # Training the SVM model clf = svm.SVC() clf.fit([[np.min(grays), np.min(grays)], [np.max(grays), np.max(grays)]], [1, 0]) # Converting the grayscale array to a 2d ndarray grays_2d = np.zeros((len(grays), 2)) for i in range(len(grays)): grays_2d[i][0] = grays[i] grays_2d[i][1] = grays[i] # Execute the SVM model prediction = clf.predict(grays_2d) predDF = pd.DataFrame({'pred': prediction}) writeprintIndexes = predDF[predDF['pred'] == 1].index processedImg = np.zeros(len(grays), dtype=int) for i in range(len(writeprintIndexes)): processedImg[writeprintIndexes[i]] = 1 # Reshape the image processedImg = np.reshape(processedImg, (-1, np.sqrt(len(grays)).astype(int))) rowsSum = np.sum(processedImg, axis=1) cropedImg = np.array([], dtype=int) for i, sum in np.ndenumerate(rowsSum): if sum > 0: cropedImg = np.append(cropedImg, processedImg[i]) cropedImg = np.reshape(cropedImg, (-1, 28)) nrow = len(cropedImg) processedImgCols = np.rot90(cropedImg) colsSum = np.sum(processedImgCols, axis=1) cropedImg = np.array([], dtype=int) for i, sum in np.ndenumerate(colsSum): if sum > 0: cropedImg = np.append(cropedImg, processedImgCols[i]) cropedImg = np.reshape(cropedImg, (-1, nrow)) cropedImg = np.rot90(cropedImg, k=3) cropedImg = transform.resize(color.gray2rgb(cropedImg), (28, 28)) grays = color.rgb2gray(cropedImg) * 1e19 resizedImg = np.zeros(grays.shape, int) for index, g in np.ndenumerate(grays): if g > 1: resizedImg[index] = 1 return resizedImg
import numpy as np from numpy.testing import run_module_suite, assert_raises, assert_equal from skimage import restoration, data, color, img_as_float np.random.seed(1234) astro = img_as_float(data.astronaut()[:128, :128]) astro_gray = color.rgb2gray(astro) checkerboard_gray = img_as_float(data.checkerboard()) checkerboard = color.gray2rgb(checkerboard_gray) def test_denoise_tv_chambolle_2d(): # astronaut image img = astro_gray.copy() # add noise to astronaut img += 0.5 * img.std() * np.random.rand(*img.shape) # clip noise so that it does not exceed allowed range for float images. img = np.clip(img, 0, 1) # denoise denoised_astro = restoration.denoise_tv_chambolle(img, weight=60.0) # which dtype? assert denoised_astro.dtype in [np.float, np.float32, np.float64] from scipy import ndimage grad = ndimage.morphological_gradient(img, size=((3, 3))) grad_denoised = ndimage.morphological_gradient(denoised_astro, size=((3, 3))) # test if the total variation has decreased assert grad_denoised.dtype == np.float assert (np.sqrt((grad_denoised**2).sum()) < np.sqrt((grad**2).sum()) / 2)
# def superimpose_delhi_map(): def get_lat_long_image_coordinates(xmin, ymin): df = gpd.read_file('Municipal_Spatial_Data/Delhi/Delhi_Boundary.geojson') long_array = list(df.loc[0, 'geometry'].exterior.coords.xy[0]) long_array = list(map(lambda x : round(x, 2), long_array)) lat_array = list(df.loc[0, 'geometry'].exterior.coords.xy[1]) lat_array = list(map(lambda x : round(x, 2), lat_array)) long_img_coordinates = list(map(lambda x : int(100*round(x - xmin, 2)), long_array)) lat_img_coordinates = list(map(lambda x : int(100*round(ymax - x, 2)), lat_array)) return lat_img_coordinates, long_img_coordinates files = read_data() ymin = np.min(scipy.io.loadmat('weekly_bc_2015/'+files[0][1])['cc'][:, 0]) ymax = np.max(scipy.io.loadmat('weekly_bc_2015/'+files[0][1])['cc'][:, 0]) xmin = np.min(scipy.io.loadmat('weekly_bc_2015/'+files[0][1])['cc'][:, 1]) xmax = np.max(scipy.io.loadmat('weekly_bc_2015/'+files[0][1])['cc'][:, 1]) lat_img_coordinates, long_img_coordinates = get_lat_long_image_coordinates(xmin, ymin) max_pol, min_pol = get_max_min_poll_values(files) for file in files: # image_gray = rgb2gray(poll_arr) image_gray, poll_arr = create_grayscale_image(file, max_pol, min_pol) print(image_gray.shape) blobs_list = create_blobs(image_gray) plot_blobs(image_gray, poll_arr, blobs_list, lat_img_coordinates, long_img_coordinates, file, xmin, xmax, ymin, ymax) plt.imshow(gray2rgb(image_gray))
model = generate_mobilenet_model() model.load_weights(_MODEL_WEIGHTS_NAME) def get_images_names(path): images = os.listdir(path) final = [] for image in images: final.append(image.rstrip()) return images images_names = get_images_names(_IMAGE_PATH) for img in images_names: x_b = img_to_array(load_img(_IMAGE_PATH+img, target_size= (224,224))) x_b = np.expand_dims(x_b, axis=0) x_b = x_b/255 #conversion to a range of -1 to 1. Explanation saved. grayscaled_rgb = gray2rgb(rgb2gray(x_b)) # convert to 3 channeled grayscale image X_lab = rgb2lab(grayscaled_rgb)[:, :, :, 0] X_lab = X_lab.reshape(X_lab.shape + (1,)) X_lab = 2 * X_lab / 100 - 1. predictions = model.predict([X_lab,grayscaled_rgb], steps=1, verbose = 1) postprocess_output(X_lab , predictions, img)