def test_deprecation(): selem = disk(3) image = img_as_ubyte(data.camera()[:50, :50]) with expected_warnings(['rank.tophat is deprecated.']): rank.tophat(image, selem) with expected_warnings(['rank.bottomhat is deprecated.']): rank.bottomhat(image, selem)
def detectField(input): #radiation field detection and contouring kernal = cv2.getStructuringElement(cv2.MORPH_RECT, (5, 5)) output = bottomhat(input, kernal) # disk(30)) blur = cv2.GaussianBlur(output, (5, 5), 0) _, threshold = cv2.threshold(blur, 230, 255, 0) # field threshold = threshold.astype('uint8') contours, hierarchy = cv2.findContours(threshold, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) for cnt in contours: for i in tqdm(np.arange(len(contours))): # Shortlisting the regions based on there area. if cv2.contourArea(cnt) > 2000: print(cv2.contourArea(cnt)) approx = cv2.approxPolyDP(cnt, 0.009 * cv2.arcLength(cnt, True), True) # Checking if the no. of sides of the selected region is 4. cv2.drawContours(threshold, [approx], -1, (78, 55, -128), 1) M = cv2.moments(cnt) time.sleep(0.1) i = i + 1 cX = int(M["m10"] / M["m00"]) cY = int(M["m01"] / M["m00"]) print (cX, cY) cv2.circle(threshold, (cX, cY), 2, (78, 55, -128), 1) cv2.putText(threshold, "center", (cX - 20, cY - 20), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 255), 1) cv2.imshow("Image", threshold) cv2.waitKey(0)
def detectField(input): #radiation field detection and contouring input = cv2.blur(input, (10, 10)) kernel = np.ones((5, 5), np.float32) / 50 input = cv2.filter2D(input, -1, kernel) output = bottomhat(input, square(3)) # disk(30)) #blur = cv2.GaussianBlur(output, (5, 5), 0) _, threshold = cv2.threshold(output, 254, 255, 0) # field threshold = threshold.astype('uint8') contours, hierarchy = cv2.findContours(threshold, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) i = 0 for cnt in contours: for i in tqdm(np.arange(len(contours))): cv2.drawContours(threshold, [cnt], -1, (78, 55, -128), 1) M = cv2.moments(cnt) time.sleep(0.1) i = i + 1 cX = int(M["m10"] / M["m00"]) cY = int(M["m01"] / M["m00"]) cv2.circle(threshold, (cX, cY), 2, (78, 55, -128), 1) cv2.putText(threshold, "Radiation Field Iso-center", (cX - 20, cY - 20), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 255), 1) cv2.imshow("Field", threshold) cv2.moveWindow("Field", 300, 30) print("X-Coordinate: " + str(cX)) print("Y-Coordinate: " + str(cY)) cv2.destroyWindow("Centroid") cv2.waitKey(0) return threshold, cX, cY
def detectField(input): #radiation field detection and contouring kernal = cv2.getStructuringElement(cv2.MORPH_RECT, (5, 5)) output = bottomhat(input, kernal) # disk(30)) blur = cv2.GaussianBlur(output, (5, 5), 0) _, threshold = cv2.threshold(blur, 230, 255, 0) # field threshold = threshold.astype('uint8') contours, hierarchy = cv2.findContours(threshold, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) i = 0 for cnt in contours: for i in tqdm(np.arange(len(contours))): cv2.drawContours(threshold, [cnt], -1, (78, 55, -128), 1) M = cv2.moments(cnt) time.sleep(0.1) i = i + 1 cX = int(M["m10"] / M["m00"]) cY = int(M["m01"] / M["m00"]) cv2.circle(threshold, (cX, cY), 2, (78, 55, -128), 1) cv2.putText(threshold, "center", (cX - 20, cY - 20), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 255), 1) cv2.imshow("Field", threshold) cv2.moveWindow("Field", 300, 30) print("X-Coordinate: " + str(cX)) print("Y-Coordinate: " + str(cY)) cv2.destroyWindow("Centroid") cv2.waitKey(0) return threshold, cX, cY
def segmentlocal(img, filter_size=5, level=0.2, selem_size=3, rescale_low=(0, 50), rescale_high=(50, 100)): """ Segment image using gradients calculated locally. Apply a Wiener filter to remove salt-and-pepper noise, then calculate gradients over local neighborhoods using the specified structuring element. Threshold the image constructed my multiplying the rescaled original image with its rescaled derivative (this helps define the edges). args: img (ndarray): input image kwargs: filter_size (int): neighborhood size of Wiener filter selem_size (int): size of the disk structuring element level (float): threshold level, specified as a fraction of the calculated Otsu threshold; must by in the range [0, 1] rescale_low (tuple): (low, high) values used to rescale original image rescale_high (tuple): (low, high) values used to rescale edges image returns: ndarray: thresholded binary image (dtype = bool) """ img2 = wiener(img, (filter_size, filter_size)) img3 = median_filter(img2, filter_size) img4 = complement(rescale(img3, rescale_low)) img5 = bottomhat(img_as_uint12(img3), disk(selem_size)) img6 = fillholes(img5) img7 = rescale(img6, rescale_high) img8 = img4 * img7 return threshold(img8, level=level)
def img_loader2(path): image = cv2.imread(path, cv2.IMREAD_COLOR) image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) image =sfr.bottomhat(image, disk(2)) #半径为2的圆形滤波器 R = image G = image B = image image = cv2.merge([B, G, R]) return image
def check_all(): selem = morphology.disk(1) refs = np.load( os.path.join(skimage.data_dir, "rank_filter_tests.npz")) assert_equal(refs["autolevel"], rank.autolevel(self.image, selem)) assert_equal(refs["autolevel_percentile"], rank.autolevel_percentile(self.image, selem)) assert_equal(refs["bottomhat"], rank.bottomhat(self.image, selem)) assert_equal(refs["equalize"], rank.equalize(self.image, selem)) assert_equal(refs["gradient"], rank.gradient(self.image, selem)) assert_equal(refs["gradient_percentile"], rank.gradient_percentile(self.image, selem)) assert_equal(refs["maximum"], rank.maximum(self.image, selem)) assert_equal(refs["mean"], rank.mean(self.image, selem)) assert_equal(refs["geometric_mean"], rank.geometric_mean(self.image, selem)), assert_equal(refs["mean_percentile"], rank.mean_percentile(self.image, selem)) assert_equal(refs["mean_bilateral"], rank.mean_bilateral(self.image, selem)) assert_equal(refs["subtract_mean"], rank.subtract_mean(self.image, selem)) assert_equal(refs["subtract_mean_percentile"], rank.subtract_mean_percentile(self.image, selem)) assert_equal(refs["median"], rank.median(self.image, selem)) assert_equal(refs["minimum"], rank.minimum(self.image, selem)) assert_equal(refs["modal"], rank.modal(self.image, selem)) assert_equal(refs["enhance_contrast"], rank.enhance_contrast(self.image, selem)) assert_equal(refs["enhance_contrast_percentile"], rank.enhance_contrast_percentile(self.image, selem)) assert_equal(refs["pop"], rank.pop(self.image, selem)) assert_equal(refs["pop_percentile"], rank.pop_percentile(self.image, selem)) assert_equal(refs["pop_bilateral"], rank.pop_bilateral(self.image, selem)) assert_equal(refs["sum"], rank.sum(self.image, selem)) assert_equal(refs["sum_bilateral"], rank.sum_bilateral(self.image, selem)) assert_equal(refs["sum_percentile"], rank.sum_percentile(self.image, selem)) assert_equal(refs["threshold"], rank.threshold(self.image, selem)) assert_equal(refs["threshold_percentile"], rank.threshold_percentile(self.image, selem)) assert_equal(refs["tophat"], rank.tophat(self.image, selem)) assert_equal(refs["noise_filter"], rank.noise_filter(self.image, selem)) assert_equal(refs["entropy"], rank.entropy(self.image, selem)) assert_equal(refs["otsu"], rank.otsu(self.image, selem)) assert_equal(refs["percentile"], rank.percentile(self.image, selem)) assert_equal(refs["windowed_histogram"], rank.windowed_histogram(self.image, selem))
def check_all(): np.random.seed(0) image = np.random.rand(25, 25) selem = morphology.disk(1) refs = np.load(os.path.join(skimage.data_dir, "rank_filter_tests.npz")) assert_equal(refs["autolevel"], rank.autolevel(image, selem)) assert_equal(refs["autolevel_percentile"], rank.autolevel_percentile(image, selem)) assert_equal(refs["bottomhat"], rank.bottomhat(image, selem)) assert_equal(refs["equalize"], rank.equalize(image, selem)) assert_equal(refs["gradient"], rank.gradient(image, selem)) assert_equal(refs["gradient_percentile"], rank.gradient_percentile(image, selem)) assert_equal(refs["maximum"], rank.maximum(image, selem)) assert_equal(refs["mean"], rank.mean(image, selem)) assert_equal(refs["mean_percentile"], rank.mean_percentile(image, selem)) assert_equal(refs["mean_bilateral"], rank.mean_bilateral(image, selem)) assert_equal(refs["subtract_mean"], rank.subtract_mean(image, selem)) assert_equal(refs["subtract_mean_percentile"], rank.subtract_mean_percentile(image, selem)) assert_equal(refs["median"], rank.median(image, selem)) assert_equal(refs["minimum"], rank.minimum(image, selem)) assert_equal(refs["modal"], rank.modal(image, selem)) assert_equal(refs["enhance_contrast"], rank.enhance_contrast(image, selem)) assert_equal(refs["enhance_contrast_percentile"], rank.enhance_contrast_percentile(image, selem)) assert_equal(refs["pop"], rank.pop(image, selem)) assert_equal(refs["pop_percentile"], rank.pop_percentile(image, selem)) assert_equal(refs["pop_bilateral"], rank.pop_bilateral(image, selem)) assert_equal(refs["sum"], rank.sum(image, selem)) assert_equal(refs["sum_bilateral"], rank.sum_bilateral(image, selem)) assert_equal(refs["sum_percentile"], rank.sum_percentile(image, selem)) assert_equal(refs["threshold"], rank.threshold(image, selem)) assert_equal(refs["threshold_percentile"], rank.threshold_percentile(image, selem)) assert_equal(refs["tophat"], rank.tophat(image, selem)) assert_equal(refs["noise_filter"], rank.noise_filter(image, selem)) assert_equal(refs["entropy"], rank.entropy(image, selem)) assert_equal(refs["otsu"], rank.otsu(image, selem)) assert_equal(refs["percentile"], rank.percentile(image, selem)) assert_equal(refs["windowed_histogram"], rank.windowed_histogram(image, selem))
from skimage import data, color import matplotlib.pyplot as plt from skimage.morphology import disk import skimage.filters.rank as sfr img = color.rgb2gray(data.camera()) bh = sfr.bottomhat(img, disk(5)) # 半径为5的圆形滤波器 th = sfr.tophat(img, disk(5)) fig, axes = plt.subplots(nrows=2, ncols=2, figsize=(8, 8)) axes = axes.ravel() ax0, ax1, ax2, ax3 = axes ax0.imshow(img, cmap=plt.cm.gray, interpolation='nearest') ax0.set_title("Original") ax2.imshow(bh, cmap=plt.cm.gray, interpolation='nearest') ax2.set_title("Bottomhat") ax3.imshow(th, cmap=plt.cm.gray, interpolation='nearest') ax3.set_title("Tophat") for ax in axes: ax.axis('off') fig.tight_layout() plt.show()
print("Masking") med_img = median(img2, disk(50*mask_scale)) mask = np.zeros(img2.shape) mask[med_img>np.mean(med_img)] = 1 mask_c = closing(mask, disk(100*mask_scale)) # plot_comparison(mask, mask_c, 'closing') # plot_comparison(mask_c, mean(mask_c, disk(20)), 'mean') masked = np.copy(img) # masked = img * resize(mask_c, img.shape) # plot_comparison(img, masked, "masked") print("bottomhatting") streets_2 = bottomhat(masked, disk(20*scale_factor)) # streets_3 = bottomhat(masked, disk(6)) # plot_comparison(streets_2, streets_3, "bottomhat") global_thresh = threshold_otsu(streets_2) binary_global = streets_2 > global_thresh block_size = 71 binary_adaptive = threshold_adaptive(streets_2, block_size) plot_comparison(binary_global, binary_adaptive, "bi") combined= binary_global*binary_adaptive closed = closing(opening(combined, square(2*scale_factor)), square(2*scale_factor)) plot_comparison(combined, closed, "opening")
print("Masking") med_img = median(img2, disk(50 * mask_scale)) mask = np.zeros(img2.shape) mask[med_img > np.mean(med_img)] = 1 mask_c = closing(mask, disk(100 * mask_scale)) # plot_comparison(mask, mask_c, 'closing') # plot_comparison(mask_c, mean(mask_c, disk(20)), 'mean') masked = np.copy(img) # masked = img * resize(mask_c, img.shape) # plot_comparison(img, masked, "masked") print("bottomhatting") streets_2 = bottomhat(masked, disk(20 * scale_factor)) # streets_3 = bottomhat(masked, disk(6)) # plot_comparison(streets_2, streets_3, "bottomhat") global_thresh = threshold_otsu(streets_2) binary_global = streets_2 > global_thresh block_size = 71 binary_adaptive = threshold_adaptive(streets_2, block_size) plot_comparison(binary_global, binary_adaptive, "bi") combined = binary_global * binary_adaptive closed = closing(opening(combined, square(2 * scale_factor)), square(2 * scale_factor))
from skimage import data, color import matplotlib.pyplot as plt from skimage.morphology import disk import skimage.filters.rank as sfr original_image = color.rgb2gray(data.camera()) plt.imshow(original_image, cmap=plt.cm.gray) filtered_images = [] filtered_images.append(sfr.autolevel(original_image, disk(5))) filtered_images.append(sfr.bottomhat(original_image, disk(5))) filtered_images.append(sfr.tophat(original_image, disk(5))) filtered_images.append(sfr.enhance_contrast(original_image, disk(5))) filtered_images.append(sfr.entropy(original_image, disk(5))) filtered_images.append(sfr.equalize(original_image, disk(5))) filtered_images.append(sfr.gradient(original_image, disk(5))) filtered_images.append(sfr.maximum(original_image, disk(5))) filtered_images.append(sfr.minimum(original_image, disk(5))) filtered_images.append(sfr.mean(original_image, disk(5))) filtered_images.append(sfr.median(original_image, disk(5))) filtered_images.append(sfr.modal(original_image, disk(5))) filtered_images.append(sfr.otsu(original_image, disk(5))) filtered_images.append(sfr.threshold(original_image, disk(5))) filtered_images.append(sfr.subtract_mean(original_image, disk(5))) filtered_images.append(sfr.sum(original_image, disk(5))) name_list = [ 'autolevel', 'bottomhat', 'tophat', 'enhance_contrast', 'entropy', 'equalize', 'gradient', 'maximum', 'minimum', 'mean', 'median', 'modal', 'otsu', 'threshold', 'subtract_mean', 'sum' ]
# ============================================================================= print('''十二、高级滤波''') # ============================================================================= img =color.rgb2gray(data.astronaut()) '''1、autolevel''' auto =rank.autolevel(img, disk(5)) #半径为5的圆形滤波器 plt.figure('filters1',figsize=(8,8)) plt.subplot(121) plt.title('origin image') plt.imshow(img,plt.cm.gray) plt.subplot(122) plt.title('filted image:autolevel') plt.imshow(auto,plt.cm.gray) plt.show() '''2、bottomhat 与 tophat''' auto =rank.bottomhat(img, disk(5)) #半径为5的圆形滤波器 plt.figure('filters2',figsize=(8,8)) plt.subplot(121) plt.title('origin image') plt.imshow(img,plt.cm.gray) plt.subplot(122) plt.title('filted image:bottomhat && tophat') plt.imshow(auto,plt.cm.gray) plt.show() '''3、enhance_contrast''' auto =rank.enhance_contrast(img, disk(5)) #半径为5的圆形滤波器 plt.figure('filters3',figsize=(8,8)) plt.subplot(121) plt.title('origin image') plt.imshow(img,plt.cm.gray) plt.subplot(122)