我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用skimage.measure.label()。
def get_n_masks(im,z): coords = [] binary = im < 604 cleared = clear_border(binary) label_image = label(cleared) areas = [r.area for r in regionprops(label_image)] areas.sort() if len(areas) > 2: for region in regionprops(label_image): if region.area < 6 and region.area > 3: # print(region.centroid) # print(region.area) centroid = region.centroid coord = [int(centroid[0]),int(centroid[1]),z] # plot_im(im,centroid) coords.append(coord) return coords
def remove_conn_components(pred_mask, num_cc): labels = label(pred_mask) if num_cc == 1: maxArea = 0 for region in regionprops(labels): if region.area > maxArea: maxArea = region.area print(maxArea) mask = remove_small_objects(labels, maxArea - 1) else: mask = remove_small_objects(labels, 3000, connectivity=2) return mask
def keep_largest_connected_components(mask): ''' Keeps only the largest connected components of each label for a segmentation mask. ''' out_img = np.zeros(mask.shape, dtype=np.uint8) for struc_id in [1, 2, 3]: binary_img = mask == struc_id blobs = measure.label(binary_img, connectivity=1) props = measure.regionprops(blobs) if not props: continue area = [ele.area for ele in props] largest_blob_ind = np.argmax(area) largest_blob_label = props[largest_blob_ind].label out_img[blobs == largest_blob_label] = struc_id return out_img
def plot_regions(self, fill=True, bgimage=None, alpha=0.5): import pylab as pl ax = pl.gca() assert isinstance(ax, pl.Axes) colors = i12.JET_12 self._plot_background(bgimage) for label in self.regions: color = colors[i12.LABELS.index(label)] / 255. for region in self.regions[label]: t = region['top'] l = self.facade_left + region['left'] b = region['bottom'] r = self.facade_left + region['right'] patch = pl.Rectangle((l, t), r - l, b - t, color=color, fill=fill, alpha=alpha) ax.add_patch(patch)
def convert_new(fname, target_size): print('Processing image: %s' % fname) img = Image.open(fname) blurred = img.filter(ImageFilter.BLUR) ba = np.array(blurred) ba_gray = rgb2gray(ba) val = filters.threshold_otsu(ba_gray) # foreground = (ba_gray > val).astype(np.uint8) foreground = closing(ba_gray > val, square(3)) # kernel = morphology.rectangle(5, 5) # foreground = morphology.binary_dilation(foreground, kernel) labels = measure.label(foreground) properties = measure.regionprops(labels) properties = sorted(properties, key=lambda p: p.area, reverse=True) # draw_top_regions(properties, 3) # return ba bbox = properties[0].bbox bbox = (bbox[1], bbox[0], bbox[3], bbox[2]) cropped = img.crop(bbox) resized = cropped.resize([target_size, target_size]) return np.array(resized)
def convert_new_regions(fname, target_size): print('Processing image: %s' % fname) img = Image.open(fname) blurred = img.filter(ImageFilter.BLUR) ba = np.array(blurred) ba_gray = rgb2gray(ba) val = filters.threshold_otsu(ba_gray) # foreground = (ba_gray > val).astype(np.uint8) foreground = closing(ba_gray > val, square(3)) # kernel = morphology.rectangle(5, 5) # foreground = morphology.binary_dilation(foreground, kernel) labels = measure.label(foreground) properties = measure.regionprops(labels) properties = sorted(properties, key=lambda p: p.area, reverse=True) draw_top_regions(properties, 3) return ba
def convert(fname, target_size): # print('Processing image: %s' % fname) img = Image.open(fname) blurred = img.filter(ImageFilter.BLUR) ba = np.array(blurred) ba_gray = rgb2gray(ba) val = filters.threshold_otsu(ba_gray) # foreground = (ba_gray > val).astype(np.uint8) foreground = closing(ba_gray > val, square(3)) # kernel = morphology.rectangle(5, 5) # foreground = morphology.binary_dilation(foreground, kernel) labels = measure.label(foreground) properties = measure.regionprops(labels) properties = sorted(properties, key=lambda p: p.area, reverse=True) # draw_top_regions(properties, 3) # return ba bbox = properties[0].bbox bbox = (bbox[1], bbox[0], bbox[3], bbox[2]) cropped = img.crop(bbox) resized = cropped.resize([target_size, target_size]) return resized
def createMask(originalMap , alpha): height,width = originalMap.shape; threshold = alpha * np.amax(originalMap) # im2bw bw = np.zeros((height,width)) idx = originalMap > threshold bw[idx] = 1 location = np.argmax(originalMap) [row,col] = np.unravel_index(location,(height,width)) blobs = bw==1 ConnectedComponent = measure.label(blobs) CompLabel = ConnectedComponent[row,col] PixelList = ConnectedComponent==CompLabel mask = np.ones((height,width)); mask[PixelList] = 0; return mask
def _floodfill(self, img): back = Back._scharr(img) # Binary thresholding. back = back > 0.05 # Thin all edges to be 1-pixel wide. back = skm.skeletonize(back) # Edges are not detected on the borders, make artificial ones. back[0, :] = back[-1, :] = True back[:, 0] = back[:, -1] = True # Label adjacent pixels of the same color. labels = label(back, background=-1, connectivity=1) # Count as background all pixels labeled like one of the corners. corners = [(1, 1), (-2, 1), (1, -2), (-2, -2)] for l in (labels[i, j] for i, j in corners): back[labels == l] = True # Remove remaining inner edges. return skm.opening(back)
def create_mask(im_arr, erode=0): if im_arr.shape[2] == 3: im_arr = rgb2gray(im_arr) thresh = 0.05 inv_bin = np.invert(im_arr > thresh) all_labels = measure.label(inv_bin) # Select largest object and invert seg_arr = all_labels == 0 if erode > 0: strel = selem.disk(erode, dtype=np.bool) seg_arr = binary_erosion(seg_arr, selem=strel) elif erode < 0: strel = selem.disk(abs(erode), dtype=np.bool) seg_arr = binary_dilation(seg_arr, selem=strel) return seg_arr.astype(np.bool)
def generate_markers(image): #Creation of the internal Marker marker_internal = image < -400 marker_internal = segmentation.clear_border(marker_internal) marker_internal_labels = measure.label(marker_internal) areas = [r.area for r in measure.regionprops(marker_internal_labels)] areas.sort() if len(areas) > 2: for region in measure.regionprops(marker_internal_labels): if region.area < areas[-2]: for coordinates in region.coords: marker_internal_labels[coordinates[0], coordinates[1]] = 0 marker_internal = marker_internal_labels > 0 #Creation of the external Marker external_a = ndimage.binary_dilation(marker_internal, iterations=10) external_b = ndimage.binary_dilation(marker_internal, iterations=55) marker_external = external_b ^ external_a #Creation of the Watershed Marker matrix marker_watershed = np.zeros(image.shape, dtype=np.int) marker_watershed += marker_internal * 255 marker_watershed += marker_external * 128 return marker_internal, marker_external, marker_watershed
def findMaximaOnFG(self, param): self.defineFG(param) #self.smooth_corr() self.coorExtract = [0, 0] xmin, ymin = self.coorExtract img=self.candi img [self.FG ==0] =0 im = img_as_float(img) image_max = ndimage.maximum_filter(im, size=10, mode='constant') coordinates = peak_local_max(im, min_distance=10) tep=np.zeros(self.candi.shape) for i,ide in enumerate(coordinates): tep[ide[0],ide[1]] = self.candi[ide[0],ide[1]] lbl = ndimage.label(tep)[0] centerc = np.round(ndimage.measurements.center_of_mass(tep, lbl, range(1,np.max(lbl)+1))) if centerc.size > 0: self.centersX = centerc[:,0].astype(int) self.centersY = centerc[:,1].astype(int) self.nComponents = len(self.centersX)
def generate_markers_3d(image): #Creation of the internal Marker marker_internal = image < -400 marker_internal_labels = np.zeros(image.shape).astype(np.int16) for i in range(marker_internal.shape[0]): marker_internal[i] = segmentation.clear_border(marker_internal[i]) marker_internal_labels[i] = measure.label(marker_internal[i]) #areas = [r.area for r in measure.regionprops(marker_internal_labels)] areas = [r.area for i in range(marker_internal.shape[0]) for r in measure.regionprops(marker_internal_labels[i])] for i in range(marker_internal.shape[0]): areas = [r.area for r in measure.regionprops(marker_internal_labels[i])] areas.sort() if len(areas) > 2: for region in measure.regionprops(marker_internal_labels[i]): if region.area < areas[-2]: for coordinates in region.coords: marker_internal_labels[i, coordinates[0], coordinates[1]] = 0 marker_internal = marker_internal_labels > 0 #Creation of the external Marker # 3x3 structuring element with connectivity 1, used by default struct1 = ndimage.generate_binary_structure(2, 1) struct1 = struct1[np.newaxis,:,:] # expand by z axis . external_a = ndimage.binary_dilation(marker_internal, structure=struct1, iterations=10) external_b = ndimage.binary_dilation(marker_internal, structure=struct1, iterations=55) marker_external = external_b ^ external_a #Creation of the Watershed Marker matrix #marker_watershed = np.zeros((512, 512), dtype=np.int) # origi marker_watershed = np.zeros((marker_external.shape), dtype=np.int) marker_watershed += marker_internal * 255 marker_watershed += marker_external * 128 return marker_internal, marker_external, marker_watershed
def clean_contour(in_contour, is_prob=False): if is_prob: pred = (in_contour >= 0.5).astype(np.float32) else: pred = in_contour labels = measure.label(pred) area = [] for l in range(1, np.amax(labels) + 1): area.append(np.sum(labels == l)) out_contour = in_contour out_contour[np.logical_and(labels > 0, labels != np.argmax(area) + 1)] = 0 return out_contour
def img_recover(data, label, imgsize=(200, 200), px_over=5): """Recover the image after classification. Inputs ====== data: np.ndarray the splitted samples data of the observation label: np.ndarray the estimated labels imgsize: tuple shape of the image px_over: integer the overlapped pixels Output ====== img: np.ndarray the recovered image """ # Init img = np.zeros(imgsize, dtype=bool) # Get params numsamples, boxsize = data.shape boxsize = int(np.sqrt(boxsize)) # Number of boxes px_diff = boxsize - px_over box_rows = int(np.round((imgsize[0] - boxsize - 1) / px_diff)) + 1 box_cols = int(np.round((imgsize[1] - boxsize - 1) / px_diff)) + 1 # recover for i in range(box_rows): for j in range(box_cols): if label[i*box_rows+j] == 1: label_temp = True else: label_temp = False img[i * px_diff:i * px_diff + boxsize, j * px_diff:j * px_diff + boxsize] += label_temp return img.astype(int)
def clean_contour(prob, c_input): # Smaller areas with lower prob are very likely to be false positives wt_mor = binary_dilation((c_input > 0).astype(np.float32), iterations=10) labels = measure.label(wt_mor) w_area = [] for l in range(1, np.amax(labels) + 1): w_area.append(np.sum(prob[labels == l])) if len(w_area) > 0: max_area = np.amax(w_area) for l in range(len(w_area)): if w_area[l] < max_area / 2.0: c_input[labels == l + 1] = 0 return c_input
def get_segmented_lungs(im, plot=False): # Step 1: Convert into a binary image. binary = im < -400 # Step 2: Remove the blobs connected to the border of the image. cleared = clear_border(binary) # Step 3: Label the image. label_image = label(cleared) # Step 4: Keep the labels with 2 largest areas. areas = [r.area for r in regionprops(label_image)] areas.sort() if len(areas) > 2: for region in regionprops(label_image): if region.area < areas[-2]: for coordinates in region.coords: label_image[coordinates[0], coordinates[1]] = 0 binary = label_image > 0 # Step 5: Erosion operation with a disk of radius 2. This operation is seperate the lung nodules attached to the blood vessels. selem = disk(2) binary = binary_erosion(binary, selem) # Step 6: Closure operation with a disk of radius 10. This operation is to keep nodules attached to the lung wall. selem = disk(10) # CHANGE BACK TO 10 binary = binary_closing(binary, selem) # Step 7: Fill in the small holes inside the binary mask of lungs. edges = roberts(binary) binary = ndi.binary_fill_holes(edges) # Step 8: Superimpose the binary mask on the input image. get_high_vals = binary == 0 im[get_high_vals] = -2000 return im, binary
def extract_boxes_as_dictionaries(image, threshold=0.5, se=disk(3)): mask = image > threshold mask = binary_opening(mask, selem=se) try: props = regionprops(label(mask)) def _tag(tlbr): t, l, b, r = tlbr return dict(top=int(t), left=int(l), bottom=int(b), right=int(r)) result = [_tag(r.bbox) for r in props] except (ValueError, TypeError) as e: result = [] return result
def get_mask(image, uid): mask = np.array(image > -320, dtype=np.int8) # Set the edges to zeros. This is to connect the air regions, which # may appear separated in some scans mask[:, 0] = 0 mask[:, -1] = 0 mask[:, :, 0] = 0 mask[:, :, -1] = 0 labels = measure.label(mask, connectivity=1, background=-1) vals, counts = np.unique(labels, return_counts=True) inds = np.argsort(counts) # Assume that the lungs make up the third largest region lung_val = vals[inds][-3] if mask[labels == lung_val].sum() != 0: print('Warning: could not get mask for %s' % uid) mask[:] = 1 return mask mask[labels == lung_val] = 1 mask[labels != lung_val] = 0 fill_mask(mask) left_center = mask[mask.shape[0] // 2, mask.shape[1] // 2, mask.shape[2] // 4] right_center = mask[mask.shape[0] // 2, mask.shape[1] // 2, mask.shape[2] * 3 // 4] if (left_center == 0) or (right_center == 0): print('Warning: could not get mask for %s' % uid) mask[:] = 1 return mask mask = ndimage.morphology.binary_dilation(mask, iterations=settings.mask_dilation) return mask
def fill_mask_slice(slc): labels = measure.label(slc, connectivity=1, background=-1) vals, counts = np.unique(labels, return_counts=True) inds = np.argsort(counts) max_val = vals[inds][-1] if len(vals) > 1: next_val = vals[inds][-2] labels[labels == next_val] = max_val slc[labels != max_val] = 1
def segment_lung_mask(image, fill_lung_structures=True): # not actually binary, but 1 and 2. # 0 is treated as background, which we do not want binary_image = np.array(image > -320, dtype=np.int8)+1 labels = measure.label(binary_image) # Pick the pixel in the very corner to determine which label is air. # Improvement: Pick multiple background labels from around the patient # More resistant to "trays" on which the patient lays cutting the air # around the person in half background_label = labels[0,0,0] #Fill the air around the person binary_image[background_label == labels] = 2 # Method of filling the lung structures (that is superior to something like # morphological closing) if fill_lung_structures: # For every slice we determine the largest solid structure for i, axial_slice in enumerate(binary_image): axial_slice = axial_slice - 1 labeling = measure.label(axial_slice) l_max = largest_label_volume(labeling, bg=0) if l_max is not None: #This slice contains some lung binary_image[i][labeling != l_max] = 1 binary_image -= 1 #Make the image actual binary binary_image = 1-binary_image # Invert it, lungs are now 1 # Remove other air pockets insided body labels = measure.label(binary_image, background=0) l_max = largest_label_volume(labels, bg=0) if l_max is not None: # There are air pockets binary_image[labels != l_max] = 0 return binary_image
def extract_voxels(images,pred_1,truths=None): eroded = morphology.erosion(pred_1,np.ones([3,3,3])) dilation = morphology.dilation(eroded,np.ones([3,3,3])) labels = measure.label(dilation) # Different labels are displayed in different colors label_vals = np.unique(labels) regions = measure.regionprops(labels) kept = 0 removed = 0 data = [] for idx in range(len(regions)): b = regions[idx].bbox if regions[idx].area < 50: removed += 1 continue kept += 1 print "before->",b b = get_bounding_box(b,images.shape) print "after->",b image_voxel = images[b[0]:b[3],b[1]:b[4],b[2]:b[5]] label = 0 if not truths is None: print "finding region in truths" truth_voxel = truths[b[0]:b[3],b[1]:b[4],b[2]:b[5]] nonzeros = np.count_nonzero(truth_voxel) if nonzeros > 0: label = 1 assert(image_voxel.size==(VOXEL_DEPTH*VOXEL_DEPTH*VOXEL_DEPTH)) print "Appending voxel with label ",label data.append((image_voxel,label,b)) print "kept",kept,"removed",removed sys.stdout.flush() return data
def image_filter(img): img2 = img.copy(); img2[img2 < 30] = 100; img2 = exp.smooth_image(img2, sigma = 1.0); #plt.figure(6); plt.clf(); #plt.imshow(img2); # threshold image and take zero smaller components.. th = img2 < 92; th2 = morph.binary_closing(th, morph.diamond(1)) label = meas.label(th2, background=0) #plt.imshow(mask) bs = meas.regionprops(label+1); area = np.array([prop.area for prop in bs]); if len(area) > 0: mask = np.logical_and(label > -1, label != np.argsort(area)[-1]); img2[mask] = 100; img2[:2,:] = 100; img2[-2:,:] = 100; img2[:,:2] = 100; img2[:,-2:] = 100; #plt.figure(6); plt.clf(); #plt.subplot(1,2,1); #plt.imshow(img2, vmin = 84, vmax = 92, cmap = plt.cm.gray) #plt.subplot(1,2,2); #plt.imshow(img2); return img2;
def localization(x, y): """Simple post-processing and get IVDs positons. Return: positons: calculated by `ndimage.measurements.center_of_mass` y: after fill holes and remove small objects. """ labels, nums = label(y, return_num=True) areas = np.array([prop.filled_area for prop in regionprops(labels)]) assert nums >= 7, 'Fail in this test, should detect at least seven regions.' # Segment a joint region which should be separate (if any). while np.max(areas) > 10000: y = ndimage.binary_opening(y, structure=np.ones((3, 3, 3))) areas = np.array([prop.filled_area for prop in regionprops(label(y))]) # Remove small objects. threshold = sorted(areas, reverse=True)[7] y = morphology.remove_small_objects(y, threshold + 1) # Fill holes. y = ndimage.binary_closing(y, structure=np.ones((3, 3, 3))) y = morphology.remove_small_holes(y, min_size=512, connectivity=3) positions = ndimage.measurements.center_of_mass(x, label(y), range(1, 8)) return np.array(positions), y
def analyze(tgt, idx): print "%d :==========================" % idx tgt = binarize(tgt) if should_invert(tgt): tgt = invert(tgt) regions = regionprops(label(tgt)) for region in regions: print "-----" print "area:%s" % region.area # ??????????? print "centroid:" + str(region.centroid) # ???? print "perimeter:%s" % region.perimeter # ?? print "euler:%s" % region.euler_number print "circularity:%s" % (region.area / region.perimeter**2) print "complexity:%s" % (region.perimeter**2 / region.area)
def layout_seg(image, page_text): if page_text.endswith(u'\r\n'): separator = u'\r\n' else: separator = u'\n' texts = page_text.rstrip(separator).split(separator) bw = binarisation(image) image_height, image_width = bw.shape bw = (1 - bw).astype('ubyte') label_image = label(bw, connectivity=2) line_region_lst = get_line_region_lst(label_image) region_lst = [] line_idx = 0 text_len = len(texts) page_bar_no = texts[0].strip() for i in range(1, text_len): text = texts[i].rstrip() if text: region_seg(image, bw, image_height, page_bar_no, i, line_region_lst[line_idx], text, region_lst) line_idx = line_idx + 1 else: left = line_region_lst[line_idx].right right = line_region_lst[line_idx-1].left region = { u'text': text, u'left': left, u'right': right, u'top': 0, u'bottom': image_height, u'line_no': i, u'region_no': 1, u'page_bar_no': page_bar_no, } region_lst.append(region) return region_lst
def connected_components2bboxes(labels): """Returns a dictionary of bounding boxes (upper left c., lower right c.) for each label. >>> labels = [[0, 0, 1, 1], [2, 0, 0, 1], [2, 0, 0, 0], [0, 0, 3, 3]] >>> bboxes = connected_components2bboxes(labels) >>> bboxes[0] [0, 0, 4, 4] >>> bboxes[1] [0, 2, 2, 4] >>> bboxes[2] [1, 0, 3, 1] >>> bboxes[3] [3, 2, 4, 4] :param labels: The output of cv2.connectedComponents(). :returns: A dict indexed by labels. The values are quadruplets (xmin, ymin, xmax, ymax) so that the component with the given label lies exactly within labels[xmin:xmax, ymin:ymax]. """ bboxes = {} for x, row in enumerate(labels): for y, l in enumerate(row): if l not in bboxes: bboxes[l] = [x, y, x+1, y+1] else: box = bboxes[l] if x < box[0]: box[0] = x elif x + 1 > box[2]: box[2] = x + 1 if y < box[1]: box[1] = y elif y + 1 > box[3]: box[3] = y + 1 return bboxes
def compute_connected_components(image): labels = label(image, background=0) cc = int(labels.max()) bboxes = connected_components2bboxes(labels) return cc, labels, bboxes
def _get_lineSeg_Disp_all(self): # draw mask on img_arr with foreground painted in its "color_fill" (only display cur_line_type2 lineSeg) ## always draw on original_sized arr img_arr = self.ori_img.copy() # self.final_BI = np.zeros(img_arr.shape, np.uint8) if self.final_BI is None else self.final_BI bi_t = np.zeros(self.final_BI.shape, np.uint8) # print "img_arr.shape = {}".format(img_arr.shape) # display all types in one img for lineType in self.line_types.keys(): if "label" not in self.line_types[lineType].keys(): continue # print ">>>>>>>>>>>>> ", lineType label = self.line_types[lineType]["label"] color_fill = self.line_types[lineType]["color_fill"][0] bi_t[:] = 0 bi_t[self.final_BI == label] = 255 ### 1) visualize line type if np.count_nonzero(bi_t) > 0: # Image.fromarray(bi_t).show() bi_t_img = Image.fromarray(bi_t) img = Image.fromarray(img_arr) draw = ImageDraw.Draw(img, mode='RGB') draw.bitmap((0, 0), bi_t_img, fill=tuple(color_fill)) img_arr = np.array(img) lineSeg_Disp = img_arr # Image.fromarray(lineSeg_Disp).show() return lineSeg_Disp
def set_seg_config(self, ref_pic, label_type, seg_method): """ This function is used for initializing the segmentation part according to the label_type and seg_method """ self.set_reference_pic(ref_pic) self.label_type = label_type self.load_label_info(label_type) # self.load_line_info() self.seg_method = seg_method self.init_seg() # dont use label_type; init seg_arr with ignored label self.update_disp() # dont use label_type self.update() # dont use label_type # def update(self): # super(SegPic, self).update()
def selectBlob(self, mouseXY): # Detect blobs on binary tmp_BI (0, 255) tmp_BI = np.zeros(self.final_BI.shape, np.uint8) tmp_BI[self.final_BI == self.cur_line_label] = 255 all_labels = measure.label(tmp_BI, background=0) props = measure.regionprops(all_labels, tmp_BI) coord_RC = [] coord_XY = [] rect = [] # [min_r, max_r, min_c, max_c] for prop in props: ## bbox: tuple (min_r, min_c, max_r, max_c) min_r, min_c, max_r, max_c = prop.bbox if mouseXY[0] >= min_c and mouseXY[0] <= max_c and mouseXY[1] >= min_r and mouseXY[1] <= max_r: print "got one blob !!!!" coord_RC = prop.coords.tolist() rect = [min_r, max_r, min_c, max_c] break ## [row, col] to [x, y] for RC in coord_RC: coord_XY.append([RC[1], RC[0]]) # print coord_XY return coord_XY, rect
def update_segvalue(self, selected_index, label): """ 1) mark selected_index with current label 1) update img_arr according to seg_disp and label 2) mark_boundaries on img_arr 3) reset collect_points """ self.seg_arr.ravel()[ selected_index] = label # 1) mark selected_index with current label tmp = self.seg_disp.reshape((-1, 3)) # 3 cols: R, G, B # different color represents diff labels tmp[selected_index, :] = self.color_map[label] self.seg_disp = tmp.reshape(self.seg_disp.shape) self.img_arr = np.array(self.ref_pic.img_arr * (1 - self.alpha) + self.alpha * self.seg_disp, dtype=np.uint8) self.img_arr = np.array( mark_boundaries( self.img_arr, self.seg_index) * 255, dtype=np.uint8) self.collect_points = [] # reset collect_points[] self.update()
def _ID2label_dict_add(self, ID, label): """ add pair ID - label """ print "@@@@@@@@@@@ [_ID2label_dict_add]>> ID: {}; label: {}".format(ID, label) print "1 self.line_ID2label: ", self.line_ID2label unique, counts = np.unique(self.final_ID, return_counts=True) unique_label, counts_label = np.unique(self.final_BI, return_counts=True) final_ID_cnt = dict(zip(unique, counts)) final_BI_cnt = dict(zip(unique_label, counts_label)) print "final_ID_cnt>>>>>>>: ", final_ID_cnt print "final_BI_cnt>>>>>>>: ", final_BI_cnt if ID not in final_ID_cnt.keys(): print ">>> try to add pair, but ID: {} not in final_ID".format(ID) return if label not in final_BI_cnt.keys(): print ">>> try to add pair, but label: {} not in final_BI".format(label) return if ID not in self.line_ID2label.keys(): if ID not in final_ID_cnt.keys(): print ">>> try to add pair, but ID: {} not in final_ID".format(ID) else: self.line_ID2label[ID] = [label] elif label not in self.line_ID2label[ID]: if label not in final_BI_cnt.keys(): print ">>> try to add pair, but label: {} not in final_BI".format(label) else: self.line_ID2label[ID].append(label) print "2 >>>>>>>>>>>>>>>>>>>: ", self.line_ID2label
def do_segmentation(self): # Apply segmentation and update the display """ 1) Do pre-segmentation (2 methods available) 2) seg_index -- init with pre-segmentation result 3) seg_disp -- each pixel is colored according to its class label 4.1) img_arr -- on ori_img, color each pixel according to seg_disp 4.2) img_arr -- mark boundaries of segmentations according to seg_index """ ori_img = self.ref_pic.img_arr sp = self.seg_params if self.seg_method == 'slic': n_segments, compactness, sigma = np.int( sp[0]), np.int(sp[1]), sp[2] self.seg_index = slic( ori_img, n_segments=n_segments, compactness=compactness, sigma=sigma) elif self.seg_method == 'felzenszwalb': scale, min_size, sigma = np.int(sp[0]), np.int(sp[1]), sp[2] self.seg_index = felzenszwalb( ori_img, scale=scale, min_size=min_size, sigma=sigma) r, c = self.seg_arr.shape # color_map -- one row; color_map[2] -- color of class label_2 # seg_disp -- 3D (r*c*3); seg_disp[r, c] -- color of pixel (r, c) on # img according to its label self.seg_disp = self.color_map[self.seg_arr.ravel()].reshape((r, c, 3)) # img_arr -- color the ori_img with the result of pixelwise labeling self.img_arr = np.array( ori_img * (1 - self.alpha) + self.alpha * self.seg_disp, dtype=np.uint8) self.img_arr = np.array( mark_boundaries( self.img_arr, self.seg_index) * 255, dtype=np.uint8) self.update()
def connected_component_image(otsu_image): """ apply Connected Component Analysis to otsu_image it is because of detect tissue choose the label that has largest spces in the image otsu_image = input image that applied otsu thresholding max_label = maximum label of components cnt_label = the number of pix which in certin lebel result_label = the label which indicate tissue return tissue image """ image_labels = measure.label(otsu_image) max_label = np.max(image_labels) cnt_label = 0 result_label = 1 for i in range(1,max_label): temp = (image_labels == i) temp = temp.astype(float) cnt_nonzero = np.count_nonzero(temp) if cnt_nonzero > cnt_label: cnt_label = cnt_nonzero result_label = i tissue_image = (image_labels == result_label) tissue_image = tissue_image.astype(float) return tissue_image
def measure_voxels(labs, ims): #print("Befpre measure.regionprops, labs & intensity shapes: ", labs.shape, ims.shape) regprop = measure.regionprops(labs, intensity_image=ims) # probkem here on 20170327 voxel_volume = np.product(RESIZE_SPACING) areas = [rp.area for rp in regprop] # this is in cubic mm now (i.e. should really be called volume) volumes = [rp.area * voxel_volume for rp in regprop] diameters = [2 * (3* volume / (4 * np.pi ))**0.3333 for volume in volumes] labs_ids = [rp.label for rp in regprop] #ls = [rp.label for rp in regprop] max_val = np.max(areas) max_index = areas.index(max_val) max_label = regprop[max_index].label bboxes = [r.bbox for r in regprop] #max_ls = ls[max_index] idl = labs == regprop[max_index].label # 400 nodules_pixels = ims[idl] nodules_hu = pix_to_hu(nodules_pixels) run_UNNEEDED_code = False if run_UNNEEDED_code: nodules_hu_reg = [] for rp in regprop: idl = labs == rp.label nodules_pixels = ims[idl] nodules_hu = pix_to_hu(nodules_pixels) nodules_hu_reg.append(nodules_hu) # NOTE some are out of interest, i.e. are equal all (or near all) to MAX_BOUND (400) dfn = pd.DataFrame( { #"zcenter": zcenters, #"ycenter": ycenters, #"xcenter": xcenters, "area": areas, "diameter": diameters, #"irreg_vol": irreg_vol, #"irreg_shape": irreg_shape, #"nodules_hu": nodules_hu_reg, "bbox": bboxes }, index=labs_ids) return dfn
def gen_sample(img, img_mark, rate=0.5, boxsize=10, px_over=5): """ Generate samples by splitting the pixel classified image with provided boxsize Input ----- img: np.ndarray The 2D raw image img_mark: np.ndarray The 2D marked image rate: float The rate of cavity pixels in the box, belongs to (0,1), default as 0.5 boxsize: integer Size of the box, default as 10 px_over: integer Overlapped pixels, default as 5 Output ------ data: np.ndarray The matrix holding samples, each column represents one sample label: np.ndarray Labels with respect to samples, could be 0, 1, and 2. """ # Init rows, cols = img.shape px_diff = boxsize - px_over # Number of boxes box_rows = int(np.round((rows - boxsize - 1) / px_diff)) + 1 box_cols = int(np.round((cols - boxsize - 1) / px_diff)) + 1 # init data and label data = np.zeros((box_rows * box_cols, boxsize**2)) label = np.zeros((box_rows * box_cols, 1)) # Split for i in range(box_rows): for j in range(box_cols): sample = img[i * px_diff:i * px_diff + boxsize, j * px_diff:j * px_diff + boxsize] label_mat = img_mark[i * px_diff:i * px_diff + boxsize, j * px_diff:j * px_diff + boxsize] data[i * box_rows + j, :] = sample.reshape((boxsize**2, )) rate_box = len(np.where(label_mat.reshape((boxsize**2,)) == 1)[0]) # label[i*box_rows+j,0] = np.where(hist==hist.max())[0][0] rate_box = rate_box / boxsize**2 if rate_box >= rate: label[i * box_rows + j, 0] = 1 else: label[i * box_rows + j, 0] = 0 return data, label
def gen_sample_multi(img, img_mark, rate=0.2, boxsize=10, px_over=5): """ Generate samples by splitting the pixel classified image with provided boxsize Input ----- img: np.ndarray The 2D raw image img_mark: np.ndarray The 2D marked image rate: float The rate of cavity pixels in the box, belongs to (0,1), default as 0.5 boxsize: integer Size of the box, default as 10 px_over: integer Overlapped pixels, default as 5 Output ------ data: np.ndarray The matrix holding samples, each column represents one sample label: np.ndarray Labels with respect to samples, could be 0, 1, and 2. """ # Init rows, cols = img.shape px_diff = boxsize - px_over # Number of boxes box_rows = int(np.round((rows - boxsize - 1) / px_diff)) + 1 box_cols = int(np.round((cols - boxsize - 1) / px_diff)) + 1 # init data and label data = np.zeros((box_rows * box_cols, boxsize**2)) label = np.zeros((box_rows * box_cols, 1)) # Split for i in range(box_rows): for j in range(box_cols): sample = img[i * px_diff:i * px_diff + boxsize, j * px_diff:j * px_diff + boxsize] label_mat = img_mark[i * px_diff:i * px_diff + boxsize, j * px_diff:j * px_diff + boxsize] data[i * box_rows + j, :] = sample.reshape((boxsize**2, )) # get label (modified: 2017/02/22) mask = label_mat.reshape((boxsize**2,)) mask0 = len(np.where(mask == 0)[0]) mask1 = len(np.where(mask == 127)[0]) mask2 = len(np.where(mask == 255)[0]) try: r = mask1 / (len(mask)) except ZeroDivisionError: r = 1 if r >= rate: label[i * box_rows + j, 0] = 1 else: mask_mat = np.array([mask0, mask1, mask2]) l = np.where(mask_mat == mask_mat.max())[0][0] # label[i*box_rows+j,0] = np.where(hist==hist.max())[0][0] label[i * box_rows + j, 0] = l return data, label
def segment_lung_mask(image, speedup=4): def largest_label_volume(im, bg=-1): vals, counts = np.unique(im, return_counts=True) counts = counts[vals != bg] vals = vals[vals != bg] if len(counts) > 0: return vals[np.argmax(counts)] else: return None if speedup>1: smallImage = transform.downscale_local_mean(image,(1,speedup,speedup)); else: smallImage = image; # not actually binary, but 1 and 2. # 0 is treated as background, which we do not want binary_image = np.array((smallImage < -320) & (smallImage>-1400), dtype=np.int8) #return binary_image; for i, axial_slice in enumerate(binary_image): axial_slice = 1-axial_slice labeling = measure.label(axial_slice) l_max = largest_label_volume(labeling, bg=0) if l_max is not None: #This slice contains some lung binary_image[i][(labeling!=l_max)] = 1 # Remove other air pockets insided body labels = measure.label(binary_image, background=0) m = labels.shape[0]//2; check_layers = labels[m-12:m+20:4,:,:]; l_max = largest_label_volume(check_layers, bg=0) while l_max is not None: # There are air pockets idx = np.where(check_layers==l_max); ii = np.vstack(idx[1:]).flatten(); if np.max(ii)>labels.shape[1]-24/speedup or np.min(ii)<24/speedup: binary_image[labels==l_max] = 0; labels = measure.label(binary_image, background=0) m = labels.shape[0]//2; check_layers = labels[m-12:m+20:4,:,:]; l_max = largest_label_volume(check_layers, bg=0) else: binary_image[labels != l_max] = 0 break if speedup<=1: return binary_image else: res = np.zeros(image.shape,dtype=np.uint8); for i,x in enumerate(binary_image): orig = np.copy(x); x = binary_dilation(x,disk(5)) x = binary_erosion(x,disk(5)) x = np.logical_or(x,orig) y = transform.resize(x*1.0,image.shape[1:3]); res[i][y>0.5]=1 return res;
def iter_blob_extremes(image, n=5): original_shape = image.shape[::-1] if max(original_shape) < 2000: size = (500, 500) y_scale = original_shape[0] / 500 x_scale = original_shape[1] / 500 else: size = (1000, 1000) y_scale = original_shape[0] / 1000 x_scale = original_shape[1] / 1000 img = resize(image, size) bimg = gaussian_filter(img, sigma=1.0) bimg = threshold_adaptive(bimg, 20, offset=2/255) bimg = -bimg bimg = ndi.binary_fill_holes(bimg) label_image = label(bimg, background=False) label_image += 1 regions = regionprops(label_image) regions.sort(key=attrgetter('area'), reverse=True) iter_n = 0 for region in regions: try: iter_n += 1 if iter_n > n: break # Skip small images if region.area < int(np.prod(size) * 0.05): continue coords = get_contours(add_border(label_image == region.label, size=label_image.shape, border_size=1, background_value=False))[0] coords = np.fliplr(coords) top_left = sorted(coords, key=lambda x: np.linalg.norm(np.array(x)))[0] top_right = sorted(coords, key=lambda x: np.linalg.norm(np.array(x) - [img.shape[1], 0]))[0] bottom_left = sorted(coords, key=lambda x: np.linalg.norm(np.array(x) - [0, img.shape[0]]))[0] bottom_right = sorted(coords, key=lambda x: np.linalg.norm(np.array(x) - [img.shape[1], img.shape[0]]))[0] scaled_extremes = [(int(x[0] * y_scale), int(x[1]*x_scale)) for x in (top_left, top_right, bottom_left, bottom_right)] yield scaled_extremes except Exception: pass raise SudokuExtractError("No suitable blob could be found.")
def prob_heatmap_features(phm, cutoff, k=1, nb_cls=3): fea_list = [] if phm is None: # deal with missing view. for _ in xrange(nb_cls - 1): # phms depending on the # of cls. fea = {'nb_regions': np.nan, 'total_area': np.nan, 'global_max_intensity': np.nan} for j in xrange(k): reg_fea = { 'area': np.nan, 'area_ratio': np.nan, 'area_ratio2': np.nan, 'eccentricity': np.nan, 'eig1': np.nan, 'eig2': np.nan, 'equivalent_diameter': np.nan, 'euler_number': np.nan, 'extent': np.nan, 'major_axis_length': np.nan, 'max_intensity': np.nan, 'mean_intensity': np.nan, 'minor_axis_length': np.nan, 'orientation': np.nan, 'perimeter': np.nan, 'solidity': np.nan, } for key in reg_fea.keys(): new_key = 'top' + str(j+1) + '_' + key reg_fea[new_key] = reg_fea.pop(key) fea.update(reg_fea) fea_list.append(fea) return fea_list for i in xrange(1, nb_cls): phm_ = phm[:,:,i] hm_bin = np.zeros_like(phm_, dtype='uint8') hm_bin[phm_ >= cutoff] = 255 hm_label = label(hm_bin) props = regionprops(hm_label, phm_) fea = { 'nb_regions':len(props), 'total_area':total_area(props), 'global_max_intensity':global_max_intensity(props), } nb_reg = min(k, len(props)) idx = topK_region_idx(props, k) for j,x in enumerate(idx): reg_fea = region_features(props[x]) for key in reg_fea.keys(): new_key = 'top' + str(j+1) + '_' + key reg_fea[new_key] = reg_fea.pop(key) fea.update(reg_fea) for j in xrange(nb_reg, k): reg_fea = region_features() for key in reg_fea.keys(): new_key = 'top' + str(j+1) + '_' + key reg_fea[new_key] = reg_fea.pop(key) fea.update(reg_fea) fea_list.append(fea) return fea_list
def get_masks(im): ''' Step 1: Convert into a binary image. ''' print('step1') binary = im < 604 # plt.imshow(binary,cmap=plt.cm.gray) # plt.show() ''' Step 2: Remove the blobs connected to the border of the image. ''' print('step2') cleared = clear_border(binary) # plt.imshow(cleared,cmap=plt.cm.gray) # plt.show() ''' Step 3: Label the image. ''' print('step3') label_image = label(cleared) # plt.imshow(label_image,cmap=plt.cm.gray) # plt.show() ''' Step 4: Keep the labels with 2 largest areas. ''' print('step4') areas = [r.area for r in regionprops(label_image)] areas.sort() if len(areas) > 2: for region in regionprops(label_image): if region.area < 10 and region.area > 3: print(region.centroid,region.area) # print(region.area) centroid = region.centroid plot_im(im,centroid) # label_image[int(centroid[0]),int(centroid[1])] = 1000 # for coordinates in region.coords: # label_image[coordinates[0], coordinates[1]] = 0 # binary = label_image > 999 # plt.imshow(binary,cmap=plt.cm.gray) # plt.show() ''' Step 5: Erosion operation with a disk of radius 2. This operation is seperate the lung nodules attached to the blood vessels. ''' # print('step5') # selem = disk(2) # binary = binary_erosion(binary, selem) # plt.imshow(binary,cmap=plt.cm.gray) # plt.show()