Skip to content

Commit 7a4688e

Browse files
committed
add opencv basic tutorial
0 parents  commit 7a4688e

10 files changed

+183
-0
lines changed

‎.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
output.avi
2+
.vscode/

‎.vscode/settings.json

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"python.pythonPath": "opencv\\Scripts\\python.exe"
3+
}

‎add_text_to_videos.py

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import cv2
2+
import datetime
3+
cap = cv2.VideoCapture(0)
4+
5+
print(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
6+
print(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
7+
8+
# 3: width, 4: height
9+
# you can only able to some fix height and width like 1280x720
10+
# If you set 700x700 then it will take 640x480
11+
# If you set 3000x3000 then it will take 1280x720
12+
13+
cap.set(cv2.CAP_PROP_FRAME_WIDTH , 1208)
14+
cap.set(cv2.CAP_PROP_FRAME_HEIGHT , 720)
15+
16+
print(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
17+
print(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
18+
19+
while(cap.isOpened()):
20+
ret , frame = cap.read()
21+
if ret == True:
22+
font = cv2.FONT_HERSHEY_SIMPLEX
23+
text = 'Width: '+str(cap.get(3)) +' Height: '+str(cap.get(4))
24+
date = 'Date: '+ str(datetime.datetime.now())
25+
26+
cv2.putText(frame , text ,(10,50) , font , 1 ,(0,255,255) , 2 , lineType=cv2.LINE_AA)
27+
28+
cv2.putText(frame , date ,(10,90) , font , 1 ,(0,255,255) , 2 , lineType=cv2.LINE_AA)
29+
30+
cv2.imshow('image' , frame)
31+
32+
if cv2.waitKey(1) & 0xFF == ord('q'):
33+
break
34+
else:
35+
break
36+
37+
cap.release()
38+
cv2.destroyAllWindows()

‎basic_operations_opencv.py

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import numpy as np
2+
import cv2
3+
4+
img = cv2.imread('messi5.jpg')
5+
6+
print(img.shape) # row , column , channles
7+
print(img.size) # total number of pixels
8+
print(img.dtype) # image datatype
9+
10+
b,g,r = cv2.split(img)
11+
12+
img = cv2.merge((b,g,r))
13+
14+
cv2.imshow('image' , img)
15+
cv2.waitKey(0)
16+
cv2.destroyAllWindows()

‎draw_shapes.py

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import cv2
2+
import numpy as np
3+
# img = cv2.imread('lena.jpg', 1)
4+
5+
# img with numpy
6+
img = np.zeros([600,600,3] , np.uint8 )
7+
8+
## Draw Line
9+
img = cv2.line(img, (0,0),(255,255),(255,0,0),thickness=3)
10+
11+
## Arrowed line
12+
img = cv2.arrowedLine(img, (0,255),(255,255),(0,255,0),thickness=3)
13+
14+
## Rectangle
15+
img = cv2.rectangle(img , (384,0),(450,330),(0,0,255),thickness=5)
16+
# if thickness = -1 then it will filled with given color
17+
18+
## Circle
19+
img = cv2.circle(img, (447,63),radius=63,color = (255,34,135),thickness=-1)
20+
21+
## Put Text
22+
font = cv2.FONT_HERSHEY_SIMPLEX
23+
img = cv2.putText(img,'OpenCv',(10,450) ,font , 4 , (255,255,255), 10, cv2.LINE_AA)
24+
25+
cv2.imshow('image',img)
26+
cv2.waitKey(0)
27+
cv2.destroyAllWindows()

‎first_tut.py

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import cv2
2+
3+
# img = cv2.imread('lena.jpg',-1)
4+
5+
# cv2.imshow('image',img)
6+
7+
# k = cv2.waitKey(0)
8+
9+
# if k == 27:
10+
# cv2.destroyAllWindows()
11+
# elif k == ord('s'):
12+
# cv2.imwrite('lena_copy_2.png' , img)
13+
# cv2.destroyAllWindows()
14+
15+
16+
########## Video Capture ###########
17+
18+
## If we want to load our video file we can do that by giving
19+
# name of file in video capture.
20+
21+
cap = cv2.VideoCapture(0)
22+
23+
fourcc = cv2.VideoWriter_fourcc(*'XVID') # need to save video
24+
25+
out = cv2.VideoWriter('output.avi',fourcc , 20 , (640,480)) # save live video
26+
27+
28+
## isOpened is useful for to check if cap capture something.
29+
while(cap.isOpened()):
30+
ret , frame = cap.read()
31+
if ret == True:
32+
gray = cv2.cvtColor(frame , cv2.COLOR_BGR2GRAY)
33+
cv2.imshow('manthan' , gray)
34+
35+
out.write(frame)
36+
37+
print(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)) # Frame height
38+
print(cap.get(cv2.CAP_PROP_FRAME_WIDTH)) # Frame width
39+
40+
if cv2.waitKey(1) & 0xFF == ord('q'):
41+
break
42+
else:
43+
break
44+
45+
cap.release()
46+
out.release()
47+
cv2.destroyAllWindows()
48+
49+

‎lena.jpg

89.7 KB
Loading

‎messi5.jpg

71.2 KB
Loading

‎mouse_event_opencv_python.py

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import cv2
2+
import numpy as np
3+
4+
def event_click(event , x , y , flags , param):
5+
# if event == cv2.EVENT_LBUTTONDOWN:
6+
# print(x ,' ', y)
7+
# strxy = str(x) + ', ' + str(y)
8+
# font = cv2.FONT_HERSHEY_SIMPLEX
9+
# cv2.putText(img ,strxy, (x,y) , font , 0.5 , (255,255,0), 2)
10+
# cv2.imshow('image' , img)
11+
# if event == cv2.EVENT_RBUTTONDOWN:
12+
# blue = img[y,x,0] # BLUE
13+
# green = img[y,x,1] # green
14+
# red = img[y,x,2] # red
15+
# strBGR = str(blue) + ', ' + str(green) +', ' + str(red)
16+
# font = cv2.FONT_HERSHEY_SIMPLEX
17+
# cv2.putText(img ,strBGR, (x,y) , font , 0.5 , (0,0,0), 2)
18+
# cv2.imshow('image' , img)
19+
20+
####### Advanced Events ########
21+
# if event == cv2.EVENT_LBUTTONDOWN:
22+
# cv2.circle(img , (x,y) , 3 ,(0,0,255) , -1)
23+
# points.append((x,y))
24+
# if len(points) >= 2:
25+
# cv2.line(img , points[-1] , points[-2] ,(0,255,0) ,2 , lineType=cv2.LINE_AA)
26+
# cv2.imshow('image' , img)
27+
if event == cv2.EVENT_LBUTTONDOWN:
28+
blue = img[x,y,0] # BLUE
29+
green = img[x,y,1] # green
30+
red = img[x,y,2] # red
31+
cv2.circle(img , (x,y) , 3 ,(0,0,255) , -1)
32+
mycolorimage = np.zeros([512,512,3] , np.uint8)
33+
mycolorimage[:] = [blue,green,red]
34+
cv2.imshow('color image' , mycolorimage)
35+
36+
37+
38+
39+
40+
# img = np.zeros([512,512,3] , np.uint8)
41+
img = cv2.imread('lena.jpg')
42+
cv2.imshow('image' , img)
43+
points = []
44+
# window name must be same
45+
cv2.setMouseCallback('image' , event_click)
46+
47+
cv2.waitKey(0)
48+
cv2.destroyAllWindows()

‎output.avi

1.29 MB
Binary file not shown.

0 commit comments

Comments
 (0)