-
Notifications
You must be signed in to change notification settings - Fork 0
/
imageAlgebra.py
187 lines (161 loc) · 6.89 KB
/
imageAlgebra.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
#%%
import cv2
import matplotlib.pyplot as plt
import numpy as np
#%%
class Algebra:
def getXor(self,image1, image2):
new_img = np.zeros(image1.shape)
n = image1.shape[0]
m = image2.shape[1]
for i in range(n):
for j in range(m):
b = image1[i,j][0] ^ image2[i,j][0]
g = image1[i,j][1] ^ image2[i,j][1]
r = image1[i,j][2] ^ image2[i,j][2]
new_img[i,j] = [b,g,r]
return new_img
def getAnd(self,image1, image2):
new_img = np.zeros(image1.shape)
n = image1.shape[0]
m = image2.shape[1]
for i in range(n):
for j in range(m):
b = image1[i,j][0] & image2[i,j][0]
g = image1[i,j][1] & image2[i,j][1]
r = image1[i,j][2] & image2[i,j][2]
new_img[i,j] = [b,g,r]
return new_img
def getOr(self,image1, image2):
new_img = np.zeros(image1.shape)
n = image1.shape[0]
m = image2.shape[1]
for i in range(n):
for j in range(m):
b = image1[i,j][0] | image2[i,j][0]
g = image1[i,j][1] | image2[i,j][1]
r = image1[i,j][2] | image2[i,j][2]
new_img[i,j] = [b,g,r]
return new_img
def getAddn(self, image1, image2):
new_img = np.zeros(image1.shape)
n = image1.shape[0]
m = image2.shape[1]
for i in range(n):
for j in range(m):
new_img[i,j][0] = image1[i,j][0] + image2[i,j][0]
new_img[i,j][1] = image1[i,j][1] + image2[i,j][1]
new_img[i,j][2] = image1[i,j][2] + image2[i,j][2]
return new_img
def getSubtract(self, image1, image2):
new_img = np.zeros(image1.shape)
n = image1.shape[0]
m = image2.shape[1]
for i in range(n):
for j in range(m):
new_img[i,j][0] = ((image1[i,j][0] - image2[i,j][0]))
new_img[i,j][1] = ((image1[i,j][1] - image2[i,j][1]))
new_img[i,j][2] = ((image1[i,j][2] - image2[i,j][2]))
return new_img
def getMultiplyImages(self, image1, image2):
new_img = np.zeros(image1.shape)
n = image1.shape[0]
m = image2.shape[1]
for i in range(n):
for j in range(m):
new_img[i,j][0] = ((image1[i,j][0] * image2[i,j][0]))
new_img[i,j][1] = ((image1[i,j][1] * image2[i,j][1]))
new_img[i,j][2] = ((image1[i,j][2] * image2[i,j][2]))
return new_img
def getMultiply(self, image1, k):
new_img = np.zeros(image1.shape)
n = image1.shape[0]
m = image1.shape[1]
for i in range(n):
for j in range(m):
new_img[i,j][0] = ((image1[i,j][0] * k))
new_img[i,j][1] = ((image1[i,j][1] * k))
new_img[i,j][2] = ((image1[i,j][2] * k))
return new_img
def getDivisionImages(self, image1, image2):
new_img = np.zeros(image1.shape)
n = image1.shape[0]
m = image2.shape[1]
for i in range(n):
for j in range(m):
new_img[i,j][0] = ((image1[i,j][0] // image2[i,j][0])%256 + 256) % 256
new_img[i,j][1] = ((image1[i,j][1] // image2[i,j][0])%256 + 256) % 256
new_img[i,j][2] = ((image1[i,j][2] // image2[i,j][0])%256 + 256) % 256
return new_img
def getDivision(self, image1, k):
new_img = np.zeros(image1.shape)
n = image1.shape[0]
m = image1.shape[1]
for i in range(n):
for j in range(m):
new_img[i,j][0] = ((image1[i,j][0] // k)%256 + 256) % 256
new_img[i,j][1] = ((image1[i,j][1] // k)%256 + 256) % 256
new_img[i,j][2] = ((image1[i,j][2] // k)%256 + 256) % 256
return new_img
def printimage(name, image):
print(f"{name} :")
plt.imshow(image)
plt.show()
def main():
im_name1 = input('enter the image 1 :')
image1 = cv2.resize(cv2.imread(im_name1),(300,300))
printimage('Original Image 1', image1)
cv2.imshow('Original Image 1', image1)
cv2.waitKey(0)
cv2.destroyAllWindows()
im_name2 = input('enter the image 2 :')
image2 = cv2.resize(cv2.imread(im_name2),(300,300))
printimage('Original Image 2', image2)
cv2.imshow('Original Image 2', image2)
cv2.waitKey(0)
cv2.destroyAllWindows()
alg_obj = Algebra()
image_addn1, image_addn2 = image1.copy(),image2.copy()
image_sub1, image_sub2 = image1.copy(),image2.copy()
image_multi1, image_multi2 = image1.copy(),image2.copy()
image_div1, image_div2 = image1.copy(),image2.copy()
image_divScale1, image_MultiScale = image1.copy(),image1.copy()
printimage('And of Images',alg_obj.getAnd(image1.copy(),image2.copy()))
cv2.imshow('And of Images',alg_obj.getAnd(image1.copy(),image2.copy()))
cv2.waitKey(0)
cv2.destroyAllWindows()
printimage('Or of Images',alg_obj.getOr(image1.copy(),image2.copy()))
cv2.imshow('Or of Images',alg_obj.getOr(image1.copy(),image2.copy()))
cv2.waitKey(0)
cv2.destroyAllWindows()
printimage('Xor of Images',alg_obj.getXor(image1.copy(),image2.copy()))
cv2.imshow('Xor of Images',alg_obj.getXor(image1.copy(),image2.copy()))
cv2.waitKey(0)
cv2.destroyAllWindows()
printimage('Addition of images',alg_obj.getAddn(image_addn1.copy(),image_addn2.copy()))
cv2.imshow('Addition of images',alg_obj.getAddn(image_addn1,image_addn2))
cv2.waitKey(0)
cv2.destroyAllWindows()
printimage('Subtraction of Images',alg_obj.getSubtract(image_sub1.copy(),image_sub2.copy()))
cv2.imshow('Subtraction of Images',alg_obj.getSubtract(image_sub1,image_sub2))
cv2.waitKey(0)
cv2.destroyAllWindows()
printimage('Multiplication with scalar',alg_obj.getMultiply(image_MultiScale.copy(),10))
cv2.imshow('Multiplication with scalar',alg_obj.getMultiply(image_MultiScale,10))
cv2.waitKey(0)
cv2.destroyAllWindows()
printimage('Multiplication of Images',alg_obj.getMultiplyImages(image_multi1.copy(),image_multi2.copy()))
cv2.imshow('Multiplication of Images',alg_obj.getMultiplyImages(image_multi1,image_multi2))
cv2.waitKey(0)
cv2.destroyAllWindows()
printimage('Division with scalar',alg_obj.getDivision(image_divScale1.copy(),10))
cv2.imshow('Division with scalar',alg_obj.getDivision(image_divScale1,10))
cv2.waitKey(0)
cv2.destroyAllWindows()
printimage('Division of images',alg_obj.getDivisionImages(image_div1.copy(), image_div2.copy()))
cv2.imshow('Division of images',alg_obj.getDivisionImages(image_div1, image_div2))
cv2.waitKey(0)
cv2.destroyAllWindows()
#%%
if __name__ == '__main__':
main()