-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgray_level_transformations.py
82 lines (82 loc) · 3.01 KB
/
gray_level_transformations.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import cv2 as cv
import numpy as np
from matplotlib import pyplot as plt
import time as t
in_img = input("Enter the exact location path of the input image on which transformation is to be performed:")
img = cv.imread(in_img)
while True:
print("-----------------------------------------------------------------------------------------------------------------------------------------------------------------------")
print("Grey level transformations\n")
t.sleep(0.2)
print("Select any one transformation to be applied on the given image:")
print("1.Negative transformation")
print("2.log transformation")
print("3.power law transformation")
print("4.exit");
ch = int(input("choose any number corresponding to the above listed transformations: "))
if ch == 1:
o_dir = input("please enter a valid directory location to save:")
img_g = cv.imread(in_img,0)
img = cv.cvtColor(img, cv.COLOR_BGR2RGB)
c_negative = abs(255-img)
g_negative = abs(255-img_g)
plt.imshow(img)
plt.title("Inputted positive image")
plt.savefig(o_dir + 'negative_input_image.png')
plt.show()
plt.imshow(c_negative)
plt.title("Output negative image")
plt.savefig(o_dir + 'negative_output_image.png')
plt.show()
plt.imshow(img_g, cmap='gray')
plt.title("gray scale image of the inputted image")
plt.savefig(o_dir + 'negative_input_gray_scale.png')
plt.show()
plt.imshow(g_negative, cmap='gray')
plt.title("negative of gray scale image ")
plt.savefig(o_dir + 'negative_output_gray_scale.png')
plt.show()
print("Negative image created successfully")
t.sleep(1)
print("All the input and processed output images are saved to the provided output directory successfully")
t.sleep(1)
elif ch == 2:
o_dir = input("please enter a valid directory location to save:")
c = 255 / np.log(1 + np.max(img))
log_image = c * (np.log(img + 1))
log_image = np.array(log_image, dtype = np.uint8)
plt.imshow(img)
plt.title("inputted image")
plt.savefig(o_dir + "log_in_img.png")
plt.show()
plt.imshow(log_image)
plt.title("image after log transformation")
plt.savefig(o_dir + 'log_t.png')
plt.show();
print("log transformation applied successfully")
t.sleep(1)
print("All the input and processed output images are saved to the provided output directory successfully")
t.sleep(1)
elif ch == 3:
o_dir = input("please enter a valid directory location to save:")
gamma = float(input("enter gamma value:"))
im_res = np.array(255*(img/255)**gamma, dtype = np.uint8)
plt.imshow(img)
plt.title("Input image")
plt.savefig(o_dir + 'input_gamma_img.png')
plt.show()
plt.imshow(im_res)
plt.title("Image after gamma transform")
plt.savefig(o_dir + 'out_gamma_img.png')
plt.show()
print("power law transformation with the given gamma value="+ str(gamma) +" successful")
t.sleep(1)
print("All the input and processed output images are saved to the provided output directory successfully")
t.sleep(1)
elif ch == 4:
t.sleep(0.2)
print("see you again!!!")
t.sleep(0.2)
quit()
else:
print("bad response... Try again\n")