diff options
author | Sudipto Mallick <smlckz@termux-alpine> | 2024-02-10 12:49:55 +0000 |
---|---|---|
committer | Sudipto Mallick <smlckz@termux-alpine> | 2024-02-10 12:49:55 +0000 |
commit | 4182a108a01df3aa28009716472f0ef291704866 (patch) | |
tree | 8fabeb2d6ddff80b930c02b3d535b5447bbd41dc /opencv/code/a5.py | |
parent | 02884d29e4f5aea71364a203dcaecd53600d8aa4 (diff) | |
download | zadania-main.tar.gz |
Complete DIP assignments main
Diffstat (limited to 'opencv/code/a5.py')
-rw-r--r-- | opencv/code/a5.py | 42 |
1 files changed, 42 insertions, 0 deletions
diff --git a/opencv/code/a5.py b/opencv/code/a5.py new file mode 100644 index 0000000..52e56c5 --- /dev/null +++ b/opencv/code/a5.py @@ -0,0 +1,42 @@ +import cv2 +import numpy as np + +image = cv2.imread("4i.jpg", cv2.IMREAD_GRAYSCALE) + +# Image Negative +negative_image = 255 - image +cv2.imwrite("5.negative.jpg", negative_image) + +# Log Transformation +c = 255 / np.log(1 + np.max(image)) +log_transformed = c * np.log1p(1.0 + image) +log_transformed = np.uint8(log_transformed) +cv2.imwrite("5.log.jpg", log_transformed) + +# Power Law Transform +gamma = 0.5 +power_law_transformed = np.power(image, gamma) +power_law_transformed = cv2.normalize( + power_law_transformed, None, 0, 255, cv2.NORM_MINMAX +) +power_law_transformed = np.uint8(power_law_transformed) +cv2.imwrite("5.power_law.jpg", power_law_transformed) + + +# Piecewise Linear Transform +def piecewise_linear(x): + return np.piecewise( + x, + [x < 50, (x >= 50) & (x < 100), (x >= 100) & (x < 150), x >= 150], + [ + lambda x: 0, + lambda x: 255 * ((x - 50) / (100 - 50)), + lambda x: 255, + lambda x: 255 * ((255 - x) / (255 - 150)), + ], + ) + + +piecewise_transformed = piecewise_linear(image) +piecewise_transformed = np.uint8(piecewise_transformed) +cv2.imwrite("5.piecewise.jpg", piecewise_transformed) |