-
Notifications
You must be signed in to change notification settings - Fork 1
/
dasar.py
92 lines (70 loc) · 2.36 KB
/
dasar.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
import cv2
def baca_img():
im = cv2.imread('dd.jpg');
h,w = im.shape[:2]
new_h, new_w = int(h/4), int(w/4)
res = cv2.resize(im, (new_w,new_h))
print(im.shape) #resolusi gambar
cv2.imshow('res', res)
cv2.waitKey(0)
def croping():
img = cv2.imread('dd.jpg');
crop = img[440:520, 730:870]
cv2.imshow('Resizing', crop)
cv2.waitKey(0)
def rotating():
img = cv2.imread('dd.jpg')
h,w = img.shape[:2]
center = (w/2, h/2)
rotate = cv2.getRotationMatrix2D(center, 180, 1)
rotImg = cv2.warpAffine(img, rotate, (w,h))
cv2.imshow('Rotating', rotImg)
cv2.waitKey(0)
def binary():
img = cv2.imread('images.jpg', 0) #dd.jpg dalam format grayscale(parameter kedua)
a,thresh = cv2.threshold(img, 125, 255, cv2.THRESH_BINARY)
b,thresh_inv = cv2.threshold(img, 125, 255, cv2.THRESH_BINARY_INV)
c,thresh_trunch = cv2.threshold(img, 125, 255, cv2.THRESH_TRUNC)
d,thresh_tozero = cv2.threshold(img, 125, 255, cv2.THRESH_TOZERO)
e,thresh_tozero_inv = cv2.threshold(img, 125, 255, cv2.THRESH_TOZERO_INV)
cv2.imshow('threshold', thresh)
cv2.imshow('thresh_inv', thresh_inv)
cv2.imshow('thresh_trunch', thresh_trunch)
cv2.imshow('thresh_tozero', thresh_tozero)
cv2.imshow('thresh_tozero_inv', thresh_tozero_inv)
cv2.imshow('original', img)
cv2.waitKey(0)
cv2.destroyAllWindow()
def video():
#cap = cv2.VideoCapture(0) #webcam
cap = cv2.VideoCapture('tes.mp4') #play dari file
while(True):
ret, frame = cap.read()
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) #RGB diubah ke GrayScale
#cv2.imwrite('tangkapRGB.png', frame) #img disimpan
#cv2.imwrite('tangkapGRAY.png', gray)
#cv2.imshow("Hasil capture", gray)
cv2.imshow("Hasil capture", frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindow()
def video_save_webcam():
cap = cv2.VideoCapture(0)
fourcc = cv2.cv.CV_FOURCC(*'DIVX') #cv2.VideoWriter(['namafile', [nilai fourcc], [nilai fps], [nilai lebar,panjang]])
out = cv2.VideoWriter('output.avi', fourcc, 5, (640,480))
raw_input('Tekan enter untuk mulai esc untuk saving dan keluar')
while(cap.isOpened()):
ret, frame = cap.read()
if ret == True:
frame = cv2.flip(frame, 180) #mengatur drajat
out.write(frame)
cv2.imshow("Hasil capture", frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
else:
break
cap.release()
cv2.destroyAllWindow()
if __name__ == '__main__':
video_save_webcam()