Parcourir la source

First scripts of project added

Jérôme BUISINE il y a 5 ans
Parent
commit
7c4b7a1ad2

+ 3 - 0
.gitignore

@@ -58,3 +58,6 @@ docs/_build/
 # PyBuilder
 target/
 
+
+# others
+generated/*

+ 24 - 0
generate_all.sh

@@ -0,0 +1,24 @@
+for noise in {"cauchy","gaussian","laplace","log_normal","mut_white","white"}; do
+
+    for identical in {"0","1"}; do
+
+        if [ ${identical} == "1" ]; then
+            python noise_computation.py --noise ${noise} --image images/calibration.png --n 999 --identical ${identical} --output ${noise}.png --all 1
+        else
+            python noise_computation.py --noise ${noise} --image images/calibration.png --n 999 --identical ${identical} --output ${noise}_color.png --all 1
+        fi
+
+    done
+done
+
+
+# specifig for salt and pepper noise
+for identical in {"0","1"}; do
+    if [ ${identical} == "1" ]; then
+        python noise_computation.py --noise salt_pepper --image images/calibration.png --n 999 --identical ${identical} --output ${noise}_B.png --all 1 --p 0.1
+        python noise_computation.py --noise salt_pepper --image images/calibration.png --n 999 --identical ${identical} --output ${noise}_A.png --all 1 --p 0.01
+    else
+        python noise_computation.py --noise salt_pepper --image images/calibration.png --n 999 --identical ${identical} --output ${noise}_A_color.png --all 1 --p 0.01
+        python noise_computation.py --noise salt_pepper --image images/calibration.png --n 999 --identical ${identical} --output ${noise}_B_color.png --all 1 --p 0.1
+    fi
+done

BIN
image_test.png


BIN
images/calibration.png


+ 0 - 0
modules/__init__.py


+ 36 - 0
modules/noise.py

@@ -0,0 +1,36 @@
+from ipfml.filters import noise as nf
+
+
+def get_noise_result(_image, _n, _noise_choice, _identical=False, _p=None):
+    """Return image with applied noise using choice
+
+    Args:
+        image: image to convert
+        _n: importance of noise expected [1, 999]
+        _noise_choice: choise of noise filter to apply
+        _identical: specify if the distribution is the same or not for each chanel
+        _p: optional parameter for salt_pepper noise
+
+    Returns:
+        Noisy image with noise filter applied
+
+    """
+
+    noise_method = None
+    function_name = _noise_choice + '_noise'
+
+    try:
+        noise_method = getattr(nf, function_name)
+    except AttributeError:
+        raise NotImplementedError("Noise filter `{}` not implement `{}`".format(nf.__name__, function_name))
+
+
+    if _p:
+
+        if _noise_choice != 'salt_pepper':
+            raise ValueError("p parameter is only used for salt pepper noise...")
+
+        return noise_method(_image, _n, identical=_identical, p=_p)
+    else:
+        return noise_method(_image, _n, identical=_identical)
+

+ 0 - 0
modules/utils/__init__.py


+ 3 - 0
modules/utils/config.py

@@ -0,0 +1,3 @@
+
+image_kinds     = ['RGB', 'Grey']
+noise_labels    = ['cauchy', 'gaussian', 'laplace', 'log_normal', 'mut_white', 'salt_papper', 'white']

+ 89 - 0
noise_computation.py

@@ -0,0 +1,89 @@
+from ipfml.filters import noise as nf
+import sys, os, getopt
+from PIL import Image
+
+from modules.utils import config as cfg
+from modules import noise
+
+noise_list       = cfg.noise_labels
+filename_ext     = 'png'
+
+generated_folder = 'generated'
+
+def generate_noisy_image(p_image, p_n, p_noise, p_identical, p_output, p_param):
+
+    noisy_image = noise.get_noise_result(p_image, p_n, _noise_choice=p_noise, _identical=p_identical, _p=p_param)
+    noisy_image = Image.fromarray(noisy_image)
+
+    output_path = os.path.join(generated_folder, p_noise)
+
+    if not os.path.exists(output_path):
+        os.makedirs(output_path)
+
+    output_image_path = os.path.join(output_path, p_output)
+
+    if not filename_ext in output_image_path:
+        output_image_path = output_image_path + filename_ext
+
+    noisy_image.save(output_image_path)
+
+    print("Image saved at... '%s'" % output_image_path)
+
+
+def main():
+
+    # by default..
+    p_param = None
+    p_all = False
+
+    if len(sys.argv) < 1:
+        print('python noise_computation.py --noise xxxx --image path/to/image.png --n 100 --identical 0 --output image_name --all 1 --p 0.1')
+        sys.exit(2)
+    try:
+        opts, args = getopt.getopt(sys.argv[1:], "h:n:i:n:i:o:a:p", ["help=", "noise=", "image=", "n=", "identical=", "output=", "all=", "p="])
+    except getopt.GetoptError:
+        # print help information and exit:
+        print('python noise_computation.py --noise xxxx --image path/to/image.png --n 100 --identical 0 --output image_name --all 1 --p 0.1')
+        sys.exit(2)
+    for o, a in opts:
+        if o == "-h":
+            print('python noise_computation.py --noise xxxx --image path/to/image.png --n 100 --identical 0 --output image_name --all 1 --p 0.1')
+            sys.exit()
+        elif o in ("-n", "--noise"):
+            p_noise = a
+
+            if not p_noise in noise_list:
+                assert False, "Unknow noise parameter %s " % (noise_list)
+
+        elif o in ("-i", "--image"):
+            p_image_path = a
+        elif o in ("-n", "--n"):
+            p_n = int(a)
+
+        elif o in ("-i", "--identical"):
+            p_identical = int(a)
+        elif o in ("-o", "--output"):
+            p_output = a
+        elif o in ("-a", "--all"):
+            p_all = int(a)
+        elif o in ("-p", "--p"):
+            p_param = float(a)
+        else:
+            assert False, "unhandled option"
+
+    img = Image.open(p_image_path)
+
+    if p_all:
+
+        split_output = p_output.split('.')
+
+        for i in range(1, p_n):
+            p_filename = split_output[0] + "_" + str(i) + "." + filename_ext
+
+            generate_noisy_image(img, i, p_noise, p_identical, p_filename, p_param)
+
+    else:
+        generate_noisy_image(img, p_n, p_noise, p_identical, p_output, p_param)
+
+if __name__== "__main__":
+    main()

+ 3 - 0
requirements.txt

@@ -0,0 +1,3 @@
+ipfml
+matplotlib
+numpy