-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
226 lines (181 loc) · 7.71 KB
/
main.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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
import cv2
import numpy as np
import datetime
#Read an Image~~~~
# images = cv2.imread('Images/smarties.png', -1) #reads File, also accepts arguments (Flags) (1, 0, -1) 0 == Grayscale Version, 1 is Colored -1 includes alpha channel (load image as it is)
#
# print(images) # prints img Matrix value, also checks if image path is correct
#
# cv2.imshow('ImageWindow', images)# displays Image, arguments("Window Name", "Image Variable") displays only 1 millisecond
#
#
#
# k = cv2.waitKey(0)#keyboard binding function to wait for it to close, in milisecond, 0 for it to permanently be open unless closed
#
# if k == 27: # 27 == escape key will close
# cv2.destroyAllWindows()#destroy a window or close
#
# elif k == ord('j'): #ord is a built-in function that accepts an argument that we will click
# cv2.imwrite('Candy.png', images)#use to write an image in the form of a file ("Image Name", variable)
# cv2.destroyAllWindows()
# #Video ~~~
# cap = cv2.VideoCapture(0)# arguments accept filename or cam index 0 ("video.png") or(0)
# fourcc = cv2.VideoWriter_fourcc(*'XVID')
# out = cv2.VideoWriter('output.mp4', fourcc, 30,(640,480))#saves the video
# while(cap.isOpened()):# will return false if cap is not opened or not found
#
# ret,frame = cap.read()#read returns true if frame is available to Frame and true to ret
# if ret == True:# checks if ret is true then proceeds down
# print(cap.get(cv2.CAP_PROP_FRAME_WIDTH)) #print both width and heigh of frames
# print(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
#
# out.write(frame)#instantiate video writer for saving
#
# gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) # grayscale the image
# cv2.imshow('Frame', gray)#hows the fraeme to window
#
# if cv2.waitKey(1) & 0xFF == ord('q'):
# break
# else:
# break
# cap.release() # important since we have to release after reading
# out.release() #release resources for video writer...
# cv2.destroyAllWindows()
# # Drawing Geometric Shapes on Images
# # img = cv2.imread('Images/lena.jpg',1)
# img = np.zeros([512,512,3],np.uint8) # using numpy as image with black background
#
# img = cv2.line(img,(0,0),(255,255),(0,255,0),5)# create a line in the image, argumen pt1 == start pt2 == end
# img = cv2.arrowedLine(img,(0,255),(255,255),(0,0,255),5)
# img = cv2.rectangle(img,(384,0),(510,128),(255,0,0),2) #arguments pt1 == top left coordinates && pt2 == lower right , -1 will fill shape with color
# img = cv2.circle(img,(447,63),63,(0,255,0),-1)
# font = cv2.FONT_ITALIC
# img = cv2.putText(img, 'OpenCV',(10,500),font,4,(255,255,100),2,cv2.LINE_AA)# Puts text on an Image , 3rd argument is the coordinates
# cv2.imshow('Lena Picture', img)
#
# cv2.waitKey(0)
# cv2.destroyAllWindows()
# # setting camera parameters
# cap = cv2.VideoCapture(0)
# print(cap.get(cv2.CAP_PROP_FRAME_WIDTH)) # width of frame
# print(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)) # height of frame
#
# cap.set(3, 3000)# arguments (number of property , value)
# cap.set(4, 3000)
# # Note this sets the resolution only available for the camera
# # will only return MAX resolution of cam
# print(cap.get(3))
# print(cap.get(4))
# while(cap.isOpened()):
# ret, frame = cap.read() # returns true if there are frames
# if ret == True:
# gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
# cv2.imshow('setting cam parameters', gray)# display
#
# if cv2.waitKey(1) & 0xFF == ord('q'):
# break
#
# else:
# break
#
# cap.release()
# cv2.destroyAllWindows()
# Showing Date time on videos
# cap = cv2.VideoCapture(0)
#
# cap.get(cv2.CAP_PROP_FRAME_WIDTH)
# cap.get(cv2.CAP_PROP_FRAME_HEIGHT)
#
# cap.set(3,3000)
# cap.set(4,3000)
#
# print(cap.get(3))
# print(cap.get(4))
#
# while(cap.isOpened()):
# ret,frame= cap.read()
#
# if ret == True:
#
# font = cv2.FONT_HERSHEY_SIMPLEX # in using text , instantiate font first
# text = "Width: " + str(cap.get(3)) + " Height: " + str(cap.get(4))
# date = str(datetime.datetime.now())
# frame = cv2.putText(frame,text,(10,50), font, 1, (0,255,0), 2, cv2.LINE_AA )
# frame = cv2.putText(frame, date, (700, 50), font, 1, (255, 255, 0), 2, cv2.LINE_AA)# adding date text in video screen
# cv2.imshow("Showing Date Time", frame )
#
# if cv2.waitKey(1) & 0xFF == ord ("q"):
# break
#
# else:
# break
#
# cap.release()
# cv2.destroyAllWindows()
# Mouse events on OpenCV
# events = [i for i in dir(cv2) if 'EVENT'in i]#shows all the built in methods in CV2
# print(events)
#
# def click_event (event, x,y, flags, param): # specific format for mouse event functions
# if event == cv2.EVENT_LBUTTONDOWN:# if left mouse button is clicked
# print(x,', ' ,y) # this prints the x and y Axis in console
# font = cv2.FONT_HERSHEY_SIMPLEX
# str1= str(x) + ', ' + str(y) # container for x and y values
# cv2.putText(img, str1, (x, y), font, 1, (0, 255, 255), 2)
# cv2.imshow('LeftClick', img)
# if event == cv2.EVENT_RBUTTONDOWN: #onlcick that will check what color
# blue = img[y, x, 0]
# green = img[y, x, 1]
# red = img[y, x, 2]
# font = cv2.FONT_HERSHEY_SIMPLEX
# bgr = str(blue) + ', ' + str(green) + ',' + str(red) # container for x and y values
# cv2.putText(img, bgr, (x, y), font, 1, (255, 255, 0), 2)
# cv2.imshow('LeftClick', img)
#
# # img = np.zeros((512,512,3), np.uint8)
# img = cv2.imread('Images/lena.jpg')
# cv2.imshow('LeftClick', img)
# cv2.setMouseCallback('LeftClick', click_event)#used to call the click_event function
# cv2.waitKey(0)
# cv2.destroyAllWindows()
#More Mouse Events (Lines and Points Specifically)
#
# def click_event (event, x,y, flags, param): # specific format for mouse event functions
# if event == cv2.EVENT_LBUTTONDOWN:# if left mouse button is clicked
# cv2.circle(img, (x, y), 3, (0,255,0), -1) # circle coordinates, remember -1 fills the color
# points.append((x, y))# saving the point on which the mouse is clicked in the form of an array
# if len(points) >= 2:
# cv2.line(img,points[-1],points[-2],(255, 0, 255), 5 )# last 2 elements in the array
# cv2.imshow('LeftClick', img)
# print(points)
#
# # img = np.zeros((512,512,3), np.uint8).
# img = cv2.imread('Images/lena.jpg')
# cv2.imshow('LeftClick', img)
# points = []#empty array
# print(points)
# cv2.setMouseCallback('LeftClick', click_event)#used to call the click_event function
# cv2.waitKey(0)
# cv2.destroyAllWindows()
# Built in functions cv.split, cv.merge, cv.resice, cv.add
img = cv2.imread("Images/messi5.jpg")
img2 = cv2.imread("Images/lena.jpg")# using this image to add to messi
print(img.shape)# Shape Attribute will return a tuple of rows, columns and channels
print(img.size)# return a total number of pixels of the image
print(img.dtype)# return the image data type
b, g, r = cv2.split(img)# splits the image into 3 channels
img = cv2.merge((b,g,r))# to merge the split image.
#ROI: Region of Interest
ball = img[280:340, 330:390] #getting the lips location and putting in an numpy indexing,(upper left, buttom right)
img[273:333, 100:160] = ball #placing the lips on the desired coordinates on the pic
img = cv2.resize(img, (512, 512))# resize the image
im2 = cv2.resize(img2, (512,512)) #Param - (Source - size in tuple)
# dst = cv2.add(img,img2) #pass two array (Imgs) other parameters are set by default check cv documentation
#PS: error if size of input is not matching , array
dst = cv2.addWeighted(img, .3, img2, .7, 0) # add weighted will still combine the two images
#weight is how the picture shows or like the contrast of the image
cv2.imshow("Messi", img)
cv2.imshow("Lenna", img2)
cv2.imshow("Added", dst)
cv2.waitKey(0)
cv2.destroyAllWindows()