-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathImage_processing.py
More file actions
166 lines (139 loc) · 4.11 KB
/
Image_processing.py
File metadata and controls
166 lines (139 loc) · 4.11 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
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
# -*- coding: utf-8 -*-
"""
Created on Mon Jul 25 20:57:19 2022
@author: Juan Pablo
"""
import math
import numpy as np
import cv2 as cv
import matplotlib.pyplot as plt
#Import the cameraman image
cameraman = cv.imread('standard_test_images/cameraman.tif')
cameraman_g = cv.cvtColor(cameraman, cv.COLOR_BGR2GRAY)
#Import the house image
house = cv.imread('standard_test_images/house.tif')
house_g = cv.cvtColor(house, cv.COLOR_BGR2GRAY)
#Import the livingroom image
liv = cv.imread('standard_test_images/livingroom.tif')
liv_g = cv.cvtColor(liv, cv.COLOR_BGR2GRAY)
#Show the image
cv.imshow('House',house_g)
cv.waitKey(0)
#Camera man
#Histogram visualization
gray_hist = cv.calcHist([cameraman_g],[0],None,[256],[0,256])
#plt.hist(cameraman.ravel(),256,[0,256]); plt.show()
#Histogram equalization
gray_eq = cv.equalizeHist(cameraman_g)
equ = cv.equalizeHist(cameraman_g)
'''hist,bins = np.histogram(equ.flatten(),256,[0,256])
cdf = hist.cumsum()
cdf_normalized = cdf * float(hist.max()) / cdf.max()
plt.plot(cdf_normalized, color = 'b')
plt.hist(equ.flatten(),256,[0,256], color = 'r')
plt.xlim([0,256])
plt.legend(('cdf','histogram'), loc = 'upper left')
plt.show()
'''
#Gaussian blur 5x5
blur = cv.GaussianBlur(cameraman, (5,5),2)
#cv.imshow('Gaussian',blur)
#cv.waitKey(0)
#Display camera man results
'''
plt.figure()
plt.title('Histogram visualization')
plt.xlabel('Bins')
plt.ylabel('No. Pixels')
plt.plot(gray_hist)
plt.xlim([0,256])
plt.show()
'''
'''
plt.figure()
plt.title('Original')
plt.subplot(1, 2, 1)
plt.plot(cameraman)
plt.title('Gaussian blur 5x5')
plt.subplot(1, 2, 2)
plt.plot(blur)
plt.show()
'''
#House
#Sobel edge detection
house_blur = cv.GaussianBlur(house_g,(5,5),2)
#sobelx = cv.Sobel(src=house, ddepth=cv.CV_64F, dx=1, dy=0, ksize=5) # Sobel Edge
#cv.imshow('Sobel X Y using Sobel() function', sobelx)
#cv.waitKey(0)
'''
def sobel(src):
scale = 1
delta = 0
ddepth = cv.CV_16S
gray = cv.cvtColor(src, cv.COLOR_BGR2GRAY)
grad_x = cv.Sobel(gray, ddepth, 1, 0, ksize=3, scale=scale, delta=delta, borderType=cv.BORDER_DEFAULT)
# Gradient-Y
# grad_y = cv.Scharr(gray,ddepth,0,1)
grad_y = cv.Sobel(gray, ddepth, 0, 1, ksize=3, scale=scale, delta=delta, borderType=cv.BORDER_DEFAULT)
abs_grad_x = cv.convertScaleAbs(grad_x)
abs_grad_y = cv.convertScaleAbs(grad_y)
grad = cv.addWeighted(abs_grad_x, 0.5, abs_grad_y, 0.5, 0)
return grad
grad = sobel(house)
cv.imshow('Sobel edge detection', grad)
cv.waitKey(0)
'''
#Harris corner detector
'''
img = house
gray = np.float32(house_g)
dst = cv.cornerHarris(gray,2,3,0.04)
#result is dilated for marking the corners, not important
dst = cv.dilate(dst,None)
# Threshold for an optimal value, it may vary depending on the image.
img[dst>0.01*dst.max()]=[0,0,255]
cv.imshow('dst',img)
if cv.waitKey(0) & 0xff == 27:
cv.destroyAllWindows()
'''
#Canny edge detector
'''
img = house_g
edges = cv.Canny(img,100,200)
plt.subplot(121),plt.imshow(img,cmap = 'gray')
plt.title('Original Image'), plt.xticks([]), plt.yticks([])
plt.subplot(122),plt.imshow(edges,cmap = 'gray')
plt.title('Edge Image'), plt.xticks([]), plt.yticks([])
plt.show()
cv.imshow('Canny',edges)
cv.waitKey(0)
'''
#Hough transform (straight lines)
'''
image1 = house
gray=cv.cvtColor(image1,cv.COLOR_BGR2GRAY)
dst = cv.Canny(gray, 50, 200)
lines= cv.HoughLines(dst, 1, math.pi/180.0, 100, np.array([]), 0, 0)
a,b,c = lines.shape
for i in range(a):
rho = lines[i][0][0]
theta = lines[i][0][1]
a = math.cos(theta)
b = math.sin(theta)
x0, y0 = a*rho, b*rho
pt1 = ( int(x0+1000*(-b)), int(y0+1000*(a)) )
pt2 = ( int(x0-1000*(-b)), int(y0-1000*(a)) )
cv.line(image1, pt1, pt2, (0, 0, 255), 2, cv.LINE_AA)
cv.imshow('image1',image1)
cv.waitKey(0)
cv.destoryAllWindows(0)
'''
#Living room
#SIFT feature detection
img = liv
gray= cv.cvtColor(img,cv.COLOR_BGR2GRAY)
sift = cv.SIFT_create()
kp = sift.detect(gray,None)
img=cv.drawKeypoints(gray,kp,img)
cv.imshow('SIFT',img)
cv.waitKey(0)