comparison auto_threshold.py @ 7:e5c8e7e72373 draft

planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_auto_threshold/ commit c045f067a57e8308308cf6329060c7ccd3fc372f
author imgteam
date Thu, 04 Apr 2024 15:23:18 +0000
parents 8bccb36e055a
children 699a5e9146b3
comparison
equal deleted inserted replaced
6:8bccb36e055a 7:e5c8e7e72373
25 'loc_median': lambda img_raw, bz, **kwargs: skimage.filters.threshold_local(img_raw, bz, method='median'), 25 'loc_median': lambda img_raw, bz, **kwargs: skimage.filters.threshold_local(img_raw, bz, method='median'),
26 'loc_mean': lambda img_raw, bz, **kwargs: skimage.filters.threshold_local(img_raw, bz, method='mean') 26 'loc_mean': lambda img_raw, bz, **kwargs: skimage.filters.threshold_local(img_raw, bz, method='mean')
27 } 27 }
28 28
29 29
30 def do_thresholding(in_fn, out_fn, th_method, block_size=5, threshold=0, invert_output=False): 30 def do_thresholding(in_fn, out_fn, th_method, block_size, offset, threshold, invert_output=False):
31 img = skimage.io.imread(in_fn) 31 img = skimage.io.imread(in_fn)
32 th = th_methods[th_method](img_raw=img, bz=block_size, thres=threshold) 32 img = np.squeeze(img)
33 assert img.ndim == 2
34
35 th = offset + th_methods[th_method](img_raw=img, bz=block_size, thres=threshold)
33 res = img > th 36 res = img > th
34 if invert_output: 37 if invert_output:
35 res = np.logical_not(res) 38 res = np.logical_not(res)
39
36 tifffile.imwrite(out_fn, skimage.util.img_as_ubyte(res)) 40 tifffile.imwrite(out_fn, skimage.util.img_as_ubyte(res))
37 41
38 42
39 if __name__ == "__main__": 43 if __name__ == "__main__":
40 parser = argparse.ArgumentParser(description='Automatic Image Thresholding') 44 parser = argparse.ArgumentParser(description='Automatic image thresholding')
41 parser.add_argument('im_in', help='Path to the input image') 45 parser.add_argument('im_in', help='Path to the input image')
42 parser.add_argument('im_out', help='Path to the output image (TIFF)') 46 parser.add_argument('im_out', help='Path to the output image (uint8)')
43 parser.add_argument('th_method', choices=th_methods.keys(), help='Thresholding method') 47 parser.add_argument('th_method', choices=th_methods.keys(), help='Thresholding method')
44 parser.add_argument('block_size', type=int, default=5, help='Odd size of pixel neighborhood for calculating the threshold') 48 parser.add_argument('block_size', type=int, default=5, help='Odd size of pixel neighborhood for calculating the threshold')
45 parser.add_argument('threshold', type=float, default=0, help='Manual thresholding value') 49 parser.add_argument('offset', type=float, default=0, help='Offset of automatically determined threshold value')
50 parser.add_argument('threshold', type=float, default=0, help='Manual threshold value')
46 parser.add_argument('--invert_output', default=False, action='store_true', help='Values below/above the threshold are labeled with 0/255 by default, and with 255/0 if this argument is used') 51 parser.add_argument('--invert_output', default=False, action='store_true', help='Values below/above the threshold are labeled with 0/255 by default, and with 255/0 if this argument is used')
47 args = parser.parse_args() 52 args = parser.parse_args()
48 53
49 do_thresholding(args.im_in, args.im_out, args.th_method, args.block_size, args.threshold, args.invert_output) 54 do_thresholding(args.im_in, args.im_out, args.th_method, args.block_size, args.offset, args.threshold, args.invert_output)