-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathassignment3-final.py
More file actions
66 lines (52 loc) · 1.88 KB
/
Copy pathassignment3-final.py
File metadata and controls
66 lines (52 loc) · 1.88 KB
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
import cv2
import numpy as np
import matplotlib.pyplot as plt
import math
import util as utl
#================== Equalize ========================
def cdfNormalization(hist):
cdf = hist.cumsum()
cdf_m = np.ma.masked_equal(cdf, 0)
cdf_m = ((cdf_m - cdf_m.min()) / (cdf_m.max() - cdf_m.min()))*255
cdf_m = np.ma.filled(cdf_m, 0).astype('uint8')
return cdf_m
def equalize(img, extra):
hist = utl.calHist(img)
cdf_m = cdfNormalization(hist)
out = cdf_m[img]
return out
#============== Edge Detection =========================
def edge_operator_meth (img, k):
f = img.copy().astype(np.float32)
out = np.zeros_like(img, dtype= 'float32')
mask_gx = np.array([[-1,0,1], [-k,0,k], [-1,0,1]], dtype='float32')
mask_gy = np.array([[-1,-k,-1], [0,0,0], [1,k,1]], dtype='float32')
sz,sz = mask_gx.shape
bd = sz // 2
(m,n) = img.shape
for i in range (bd, m-bd):
for j in range (bd, n-bd):
gx, gy = 0., 0.
sub_f = f[i-bd:bd+i+1, j-bd:bd+j+1]
gx = np.multiply(sub_f, mask_gx).sum()
gy = np.multiply(sub_f, mask_gy).sum()
out[i,j] = np.sqrt(gx **2 + gy**2)
out[out>255.0] = 255.0
return out.astype(np.uint8)
def main():
out = []
histList = []
histTitleList = ["Original Histogram", "Gamma Image Histogram", "Equalize Histogram"]
funcs = [utl.gammaTrans, equalize, edge_operator_meth]
arguments = [2, None,1]
currentImg = cv2.imread("./images/pic3.png", 0)
out.append(currentImg)
for i in range (len(funcs)):
histList.append(utl.calHist(out[-1]))
currentImg = funcs[i](currentImg, arguments[i])
out.append(currentImg)
utl.plotMultipleHist(histList, histTitleList, "./outs/assignment3/mulHistograms.png")
utl.showMultipleImg(out,"./outs/assignment3/mulImages.png" ,"Original vs Power Gamma vs Equalize Image vs Edge Image")
utl.cv_show(out[-1], "Edge Image")
return
if __name__ == "__main__": main()