diff 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
line wrap: on
line diff
--- a/auto_threshold.py	Wed Mar 13 06:15:54 2024 +0000
+++ b/auto_threshold.py	Thu Apr 04 15:23:18 2024 +0000
@@ -27,23 +27,28 @@
 }
 
 
-def do_thresholding(in_fn, out_fn, th_method, block_size=5, threshold=0, invert_output=False):
+def do_thresholding(in_fn, out_fn, th_method, block_size, offset, threshold, invert_output=False):
     img = skimage.io.imread(in_fn)
-    th = th_methods[th_method](img_raw=img, bz=block_size, thres=threshold)
+    img = np.squeeze(img)
+    assert img.ndim == 2
+
+    th = offset + th_methods[th_method](img_raw=img, bz=block_size, thres=threshold)
     res = img > th
     if invert_output:
         res = np.logical_not(res)
+
     tifffile.imwrite(out_fn, skimage.util.img_as_ubyte(res))
 
 
 if __name__ == "__main__":
-    parser = argparse.ArgumentParser(description='Automatic Image Thresholding')
+    parser = argparse.ArgumentParser(description='Automatic image thresholding')
     parser.add_argument('im_in', help='Path to the input image')
-    parser.add_argument('im_out', help='Path to the output image (TIFF)')
+    parser.add_argument('im_out', help='Path to the output image (uint8)')
     parser.add_argument('th_method', choices=th_methods.keys(), help='Thresholding method')
     parser.add_argument('block_size', type=int, default=5, help='Odd size of pixel neighborhood for calculating the threshold')
-    parser.add_argument('threshold', type=float, default=0, help='Manual thresholding value')
+    parser.add_argument('offset', type=float, default=0, help='Offset of automatically determined threshold value')
+    parser.add_argument('threshold', type=float, default=0, help='Manual threshold value')
     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')
     args = parser.parse_args()
 
-    do_thresholding(args.im_in, args.im_out, args.th_method, args.block_size, args.threshold, args.invert_output)
+    do_thresholding(args.im_in, args.im_out, args.th_method, args.block_size, args.offset, args.threshold, args.invert_output)