Python skimage.morphology.remove_small_objects() Examples
The following are 22
code examples of skimage.morphology.remove_small_objects().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
skimage.morphology
, or try the search function
.
Example #1
Source File: MorphologyModule.py From HistoQC with BSD 3-Clause Clear License | 6 votes |
def removeSmallObjects(s, params): logging.info(f"{s['filename']} - \tremoveSmallObjects") min_size = int(params.get("min_size", 64)) img_reduced = morphology.remove_small_objects(s["img_mask_use"], min_size=min_size) img_small = np.invert(img_reduced) & s["img_mask_use"] io.imsave(s["outdir"] + os.sep + s["filename"] + "_small_remove.png", img_as_ubyte(img_small)) s["img_mask_small_filled"] = (img_small * 255) > 0 prev_mask = s["img_mask_use"] s["img_mask_use"] = img_reduced s.addToPrintList("percent_small_tissue_removed", printMaskHelper(params.get("mask_statistics", s["mask_statistics"]), prev_mask, s["img_mask_use"])) if len(s["img_mask_use"].nonzero()[0]) == 0: # add warning in case the final tissue is empty logging.warning(f"{s['filename']} - After MorphologyModule.removeSmallObjects: NO tissue " f"remains detectable! Downstream modules likely to be incorrect/fail") s["warnings"].append(f"After MorphologyModule.removeSmallObjects: NO tissue remains " f"detectable! Downstream modules likely to be incorrect/fail") return
Example #2
Source File: BasicModule.py From HistoQC with BSD 3-Clause Clear License | 6 votes |
def finalProcessingArea(s, params): logging.info(f"{s['filename']} - \tfinalProcessingArea") area_thresh = int(params.get("area_threshold", "1000")) mask = s["img_mask_use"] mask_opened = remove_small_objects(mask, min_size=area_thresh) mask_removed_area = ~mask_opened & mask io.imsave(s["outdir"] + os.sep + s["filename"] + "_areathresh.png", img_as_ubyte(mask_removed_area)) prev_mask = s["img_mask_use"] s["img_mask_use"] = mask_opened > 0 s.addToPrintList("areaThresh", printMaskHelper(params.get("mask_statistics", s["mask_statistics"]), prev_mask, s["img_mask_use"])) if len(s["img_mask_use"].nonzero()[0]) == 0: # add warning in case the final tissue is empty logging.warning( f"{s['filename']} - After BasicModule.finalProcessingArea NO tissue remains detectable! Downstream modules likely to be incorrect/fail") s["warnings"].append( f"After BasicModule.finalProcessingArea NO tissue remains detectable! Downstream modules likely to be incorrect/fail")
Example #3
Source File: android.py From MillionHeroAssistant with MIT License | 6 votes |
def auto_find_crop_area(source_file): """ 1. convert to gray picture 2. find pixel > 200 (white) and connect 3. if > image/4 4. find edge of question and answer :param source_file: :return: """ image = Image.open(source_file) width, height = image.size[0], image.size[1] array_img = np.array(image) ot_img = (array_img > 200) obj_dtec_img = morphology.remove_small_objects(ot_img, min_size=width * height / 4, connectivity=1) if np.sum(obj_dtec_img) < 1000: return [] return [ np.where(obj_dtec_img * 1.0 > 0)[1].min() + 20, np.where(obj_dtec_img * 1.0 > 0)[0].min(), np.where(obj_dtec_img * 1.0 > 0)[1].max(), np.where(obj_dtec_img * 1.0 > 0)[0].max()]
Example #4
Source File: test.py From MillionHeroAssistant with MIT License | 6 votes |
def test_autocrop(self): from PIL import Image import numpy as np from skimage import morphology image = Image.open("screenshots/screenshot.png") width, height = image.size[0], image.size[1] array_img = np.array(image) ot_img = (array_img > 200) obj_dtec_img = morphology.remove_small_objects(ot_img, min_size=width * height / 4, connectivity=1) if np.sum(obj_dtec_img) < 1000: print("can't find question") print([ np.where(obj_dtec_img * 1.0 > 0)[1].min() + 20, np.where(obj_dtec_img * 1.0 > 0)[0].min(), np.where(obj_dtec_img * 1.0 > 0)[1].max(), np.where(obj_dtec_img * 1.0 > 0)[0].max()])
Example #5
Source File: run_create_annotation.py From pyImSegm with BSD 3-Clause "New" or "Revised" License | 5 votes |
def load_correct_segm(path_img): """ load segmentation and correct it with simple morphological operations :param str path_img: :return (ndarray, ndarray): """ assert os.path.isfile(path_img), 'missing: %s' % path_img logging.debug('loading image: %s', path_img) img = tl_data.io_imread(path_img) seg = (img > 0) seg = morphology.binary_opening(seg, selem=morphology.disk(25)) seg = morphology.remove_small_objects(seg) seg_lb = measure.label(seg) seg_lb[seg == 0] = 0 return seg, seg_lb
Example #6
Source File: BubbleRegionByRegion.py From HistoQC with BSD 3-Clause Clear License | 5 votes |
def detectSmoothness(s, params): logging.info(f"{s['filename']} - \tBubbleRegionByRegion.detectSmoothness") thresh = float(params.get("threshold", ".01" )) kernel_size = int(params.get("kernel_size", "10")) min_object_size = int(params.get("min_object_size", "100")) img = s.getImgThumb(s["image_work_size"]) img = color.rgb2gray(img) avg = np.ones((kernel_size, kernel_size)) / (kernel_size**2) imf = scipy.signal.convolve2d(img, avg, mode="same") mask_flat = abs(imf - img) < thresh mask_flat = remove_small_objects(mask_flat, min_size=min_object_size) mask_flat = ~remove_small_objects(~mask_flat, min_size=min_object_size) prev_mask = s["img_mask_use"] s["img_mask_flat"] = mask_flat io.imsave(s["outdir"] + os.sep + s["filename"] + "_flat.png", img_as_ubyte(mask_flat & prev_mask)) s["img_mask_use"] = s["img_mask_use"] & ~s["img_mask_flat"] s.addToPrintList("flat_areas", printMaskHelper(params.get("mask_statistics", s["mask_statistics"]), prev_mask, s["img_mask_use"])) if len(s["img_mask_use"].nonzero()[0]) == 0: # add warning in case the final tissue is empty logging.warning(f"{s['filename']} - After BubbleRegionByRegion.detectSmoothness: NO tissue " f"remains detectable! Downstream modules likely to be incorrect/fail") s["warnings"].append(f"After BubbleRegionByRegion.detectSmoothness: NO tissue remains " f"detectable! Downstream modules likely to be incorrect/fail") return
Example #7
Source File: fill.py From plantcv with MIT License | 5 votes |
def fill(bin_img, size): """Identifies objects and fills objects that are less than size. Inputs: bin_img = Binary image data size = minimum object area size in pixels (integer) Returns: filtered_img = image with objects filled :param bin_img: numpy.ndarray :param size: int :return filtered_img: numpy.ndarray """ params.device += 1 # Make sure the image is binary if len(np.shape(bin_img)) != 2 or len(np.unique(bin_img)) != 2: fatal_error("Image is not binary") # Cast binary image to boolean bool_img = bin_img.astype(bool) # Find and fill contours bool_img = remove_small_objects(bool_img, size) # Cast boolean image to binary and make a copy of the binary image for returning filtered_img = np.copy(bool_img.astype(np.uint8) * 255) if params.debug == 'print': print_image(filtered_img, os.path.join(params.debug_outdir, str(params.device) + '_fill' + str(size) + '.png')) elif params.debug == 'plot': plot_image(filtered_img, cmap='gray') return filtered_img
Example #8
Source File: android.py From MillionHeroAssistant with MIT License | 5 votes |
def parse_answer_area(source_file, text_area_file, compress_level, crop_area): """ crop the answer area :return: """ image = Image.open(source_file) width, height = image.size[0], image.size[1] if not crop_area: image = image.convert("L") array_img = np.array(image) ot_img = (array_img > 225) obj_dtec_img = morphology.remove_small_objects(ot_img, min_size=width * height / 4, connectivity=1) if np.sum(obj_dtec_img) < 1000: return False region = image.crop(( np.where(obj_dtec_img * 1.0 > 0)[1].min() + 20, np.where(obj_dtec_img * 1.0 > 0)[0].min() + 215, np.where(obj_dtec_img * 1.0 > 0)[1].max(), np.where(obj_dtec_img * 1.0 > 0)[0].max())) else: if compress_level == 1: image = image.convert("L") elif compress_level == 2: image = image.convert("1") region = image.crop((width * crop_area[0], height * crop_area[1], width * crop_area[2], height * crop_area[3])) if enable_scale: region = region.resize((int(1080 / 3), int(1920 / 5)), Image.BILINEAR) region.save(text_area_file) return True
Example #9
Source File: hover.py From hover_net with MIT License | 5 votes |
def proc_np_dist(pred): """ Process Nuclei Prediction with Distance Map Args: pred: prediction output, assuming channel 0 contain probability map of nuclei channel 1 containing the regressed distance map """ blb_raw = pred[...,0] dst_raw = pred[...,1] blb = np.copy(blb_raw) blb[blb > 0.5] = 1 blb[blb <= 0.5] = 0 blb = measurements.label(blb)[0] blb = remove_small_objects(blb, min_size=10) blb[blb > 0] = 1 dst_raw[dst_raw < 0] = 0 dst = np.copy(dst_raw) dst = dst * blb dst[dst > 0.5] = 1 dst[dst <= 0.5] = 0 marker = dst.copy() marker = binary_fill_holes(marker) marker = measurements.label(marker)[0] marker = remove_small_objects(marker, min_size=10) proced_pred = watershed(-dst_raw, marker, mask=blb) return proced_pred ####
Example #10
Source File: evaluate.py From Global_Convolutional_Network with MIT License | 5 votes |
def remove_small_regions(img, size): """Morphologically removes small (less than size) connected regions of 0s or 1s.""" img = morphology.remove_small_objects(img, size) img = morphology.remove_small_holes(img, size) return img
Example #11
Source File: postprocessing.py From open-solution-data-science-bowl-2018 with MIT License | 5 votes |
def drop_small(img, min_size): img = morph.remove_small_objects(img, min_size=min_size) return relabel(img)
Example #12
Source File: postprocessing.py From open-solution-data-science-bowl-2018 with MIT License | 5 votes |
def drop_small_unlabeled(img, min_size): img = morph.remove_small_objects(img.astype(np.bool), min_size=min_size) return img.astype(np.uint8)
Example #13
Source File: locate_tissue.py From tissueloc with MIT License | 5 votes |
def remove_small_tissue(bw_img, min_size=10000): """ Remove small holes in tissue image Parameters ---------- bw_img : np.array 2D binary image. min_size: int Minimum tissue area. Returns ------- bw_remove: np.array Binary image with small tissue regions removed """ bw_remove = remove_small_objects(bw_img, min_size=min_size, connectivity=8) return bw_remove
Example #14
Source File: segmentation_test.py From DRFNS with MIT License | 5 votes |
def get_rough_detection(self, img, bigsize=40.0, smallsize=4.0, thresh = 0): diff = self.difference_of_gaussian(-img, bigsize, smallsize) diff[diff>thresh] = 1 se = morphology.square(4) ero = morphology.erosion(diff, se) labimage = label(ero) #rec = morphology.reconstruction(ero, img, method='dilation').astype(np.dtype('uint8')) # connectivity=1 corresponds to 4-connectivity. morphology.remove_small_objects(labimage, min_size=600, connectivity=1, in_place=True) #res = np.zeros(img.shape) ero[labimage==0] = 0 ero = 1 - ero labimage = label(ero) morphology.remove_small_objects(labimage, min_size=400, connectivity=1, in_place=True) ero[labimage==0] = 0 res = 1 - ero res[res>0] = 255 #temp = 255 - temp #temp = morphology.remove_small_objects(temp, min_size=400, connectivity=1, in_place=True) #res = 255 - temp return res
Example #15
Source File: preprocessing.py From bird-species-classification with MIT License | 5 votes |
def compute_binary_mask_lasseck(spectrogram, threshold): # normalize to [0, 1) norm_spectrogram = normalize(spectrogram) # median clipping binary_image = median_clipping(norm_spectrogram, threshold) # closing binary image (dilation followed by erosion) binary_image = morphology.binary_closing(binary_image, selem=np.ones((4, 4))) # dialate binary image binary_image = morphology.binary_dilation(binary_image, selem=np.ones((4, 4))) # apply median filter binary_image = filters.median(binary_image, selem=np.ones((2, 2))) # remove small objects binary_image = morphology.remove_small_objects(binary_image, min_size=32, connectivity=1) mask = np.array([np.max(col) for col in binary_image.T]) mask = smooth_mask(mask) return mask # TODO: This method needs some real testing
Example #16
Source File: BloodVessels.py From Diabetic-Retinopathy-Feature-Extraction-using-Fundus-Images with GNU General Public License v3.0 | 5 votes |
def cleanSmallObjects(self): cleanImg = morphology.remove_small_objects(self.curImg, min_size=130, connectivity=100) self.curImg = cleanImg #cv2.imwrite('Final123.jpg',threshImg)
Example #17
Source File: inferences.py From Global_Convolutional_Network with MIT License | 4 votes |
def remove_small_regions(img, size): """Morphologically removes small (less than size) connected regions of 0s or 1s.""" img = morphology.remove_small_objects(img, size) img = morphology.remove_small_holes(img, size) return img
Example #18
Source File: SDS_preprocess.py From CoastSat with GNU General Public License v3.0 | 4 votes |
def create_cloud_mask(im_QA, satname, cloud_mask_issue): """ Creates a cloud mask using the information contained in the QA band. KV WRL 2018 Arguments: ----------- im_QA: np.array Image containing the QA band satname: string short name for the satellite: ```'L5', 'L7', 'L8' or 'S2'``` cloud_mask_issue: boolean True if there is an issue with the cloud mask and sand pixels are being erroneously masked on the images Returns: ----------- cloud_mask : np.array boolean array with True if a pixel is cloudy and False otherwise """ # convert QA bits (the bits allocated to cloud cover vary depending on the satellite mission) if satname == 'L8': cloud_values = [2800, 2804, 2808, 2812, 6896, 6900, 6904, 6908] elif satname == 'L7' or satname == 'L5' or satname == 'L4': cloud_values = [752, 756, 760, 764] elif satname == 'S2': cloud_values = [1024, 2048] # 1024 = dense cloud, 2048 = cirrus clouds # find which pixels have bits corresponding to cloud values cloud_mask = np.isin(im_QA, cloud_values) # remove cloud pixels that form very thin features. These are beach or swash pixels that are # erroneously identified as clouds by the CFMASK algorithm applied to the images by the USGS. if sum(sum(cloud_mask)) > 0 and sum(sum(~cloud_mask)) > 0: morphology.remove_small_objects(cloud_mask, min_size=10, connectivity=1, in_place=True) if cloud_mask_issue: elem = morphology.square(3) # use a square of width 3 pixels cloud_mask = morphology.binary_opening(cloud_mask,elem) # perform image opening # remove objects with less than 25 connected pixels morphology.remove_small_objects(cloud_mask, min_size=25, connectivity=1, in_place=True) return cloud_mask
Example #19
Source File: BubbleRegionByRegion.py From HistoQC with BSD 3-Clause Clear License | 4 votes |
def roiWise(s, params): name = params.get("name", "classTask") print("\tpixelWise:\t", name, end="") level = int(params.get("level", 1)) win_size = int(params.get("win_size", 2048)) #the size of the ROI which will be iteratively considered osh = s["os_handle"] dim_base = osh.level_dimensions[0] dims = osh.level_dimensions[level] ratio_x = dim_base[0] / dims[0] #figure out the difference between desi ratio_y = dim_base[1] / dims[1] frangi_scale_range = (1, 6) frangi_scale_step = 2 frangi_beta1 = .5 frangi_beta2 = 100 frangi_black_ridges = True mask = [] for x in range(0, dim_base[0], round(win_size * ratio_x)): row_piece = [] print('.', end='', flush=True) for y in range(0, dim_base[1], round(win_size * ratio_y)): region = np.asarray(osh.read_region((x, y), 1, (win_size, win_size))) region = region[:, :, 0:3] # remove alpha channel g = rgb2gray(region) feat = frangi(g, frangi_scale_range, frangi_scale_step, frangi_beta1, frangi_beta2, frangi_black_ridges) feat = feat / 8.875854409275627e-08 region_mask = np.bitwise_and(g < .3, feat > 5) region_mask = remove_small_objects(region_mask, min_size=100, in_place=True) # region_std = region.std(axis=2) # region_gray = rgb2gray(region) # region_mask = np.bitwise_and(region_std < 20, region_gray < 100/255) # region_mask = scipy.ndimage.morphology.binary_dilation(region_mask, iterations=1) # region_mask = resize(region_mask , (region_mask.shape[0] / 2, region_mask.shape[1] / 2)) row_piece.append(region_mask) row_piece = np.concatenate(row_piece, axis=0) mask.append(row_piece) mask = np.concatenate(mask, axis=1) if params.get("area_threshold", "") != "": mask = remove_small_objects(mask, min_size=int(params.get("area_threshold", "")), in_place=True) s.addToPrintList(name, str(mask.mean())) #TODO, migrate to printMaskHelper, but currently don't see how this output affects final mask #s.addToPrintList(name, # printMaskHelper(params.get("mask_statistics", s["mask_statistics"]), prev_mask, s["img_mask_use"])) io.imsave(s["outdir"] + os.sep + s["filename"] + "_BubbleBounds.png", img_as_ubyte(mask)) #.astype(np.uint8) * 255) return
Example #20
Source File: submit.py From dsb2018_topcoders with MIT License | 3 votes |
def wsh(mask_img, threshold, border_img, seeds): img_copy = np.copy(mask_img) m = seeds * border_img# * dt img_copy[m <= threshold + 0.35] = 0 img_copy[m > threshold + 0.35] = 1 img_copy = img_copy.astype(np.bool) img_copy = remove_small_objects(img_copy, 10).astype(np.uint8) mask_img[mask_img <= threshold] = 0 mask_img[mask_img > threshold] = 1 mask_img = mask_img.astype(np.bool) mask_img = remove_small_holes(mask_img, 1000) mask_img = remove_small_objects(mask_img, 8).astype(np.uint8) # cv2.imwrite('t.png', (mask_img * 255).astype(np.uint8)) # cv2.imwrite('t2.png', (img_copy * 255).astype(np.uint8)) labeled_array = my_watershed(mask_img, mask_img, img_copy) return labeled_array
Example #21
Source File: inference.py From lung-segmentation-2d with MIT License | 2 votes |
def remove_small_regions(img, size): """Morphologically removes small (less than size) connected regions of 0s or 1s.""" img = morphology.remove_small_objects(img, size) img = morphology.remove_small_holes(img, size) return img
Example #22
Source File: demo.py From lung-segmentation-2d with MIT License | 1 votes |
def remove_small_regions(img, size): """Morphologically removes small (less than size) connected regions of 0s or 1s.""" img = morphology.remove_small_objects(img, size) img = morphology.remove_small_holes(img, size) return img