Skip to content

Commit

Permalink
feat: add example codes for OpenCV
Browse files Browse the repository at this point in the history
  • Loading branch information
YeonwooSung committed Jun 28, 2023
1 parent bf79f9c commit ad4fa3b
Show file tree
Hide file tree
Showing 370 changed files with 325,834 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CV/OpenCV/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.dist
.DS_Store
5 changes: 5 additions & 0 deletions CV/OpenCV/Interface/blank_img.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import cv2
import numpy as np

img = np.full((500,500,3), 255, dtype=np.uint8)
cv2.imwrite('./img/blank_500.jpg', img)
30 changes: 30 additions & 0 deletions CV/OpenCV/Interface/draw_circle.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import cv2

img = cv2.imread('../img/blank_500.jpg')


cv2.circle(img, (150, 150), 100, (255,0,0))
cv2.circle(img, (300, 150), 70, (0,255,0), 5)
cv2.circle(img, (400, 150), 50, (0,0,255), -1)


cv2.ellipse(img, (50, 300), (50, 50), 0, 0, 360, (0,0,255))
cv2.ellipse(img, (150, 300), (50, 50), 0, 0, 180, (255,0,0))
cv2.ellipse(img, (200, 300), (50, 50), 0, 181, 360, (0,0,255))


cv2.ellipse(img, (325, 300), (75, 50), 0, 0, 360, (0,255,0))
cv2.ellipse(img, (450, 300), (50, 75), 0, 0, 360, (255,0,255))


cv2.ellipse(img, (50, 425), (50, 75), 15, 0, 360, (0,0,0))
cv2.ellipse(img, (200, 425), (50, 75), 45, 0, 360, (0,0,0))


cv2.ellipse(img, (350, 425), (50, 75), 45, 0, 180, (0,0,255))
cv2.ellipse(img, (400, 425), (50, 75), 45, 181, 360, (255,0,0))


cv2.imshow('circle', img)
cv2.waitKey(0)
cv2.destroyAllWindows()
24 changes: 24 additions & 0 deletions CV/OpenCV/Interface/draw_line.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import cv2

img = cv2.imread('../img/blank_500.jpg')

cv2.line(img, (50, 50), (150, 50), (255,0,0))
cv2.line(img, (200, 50), (300, 50), (0,255,0))
cv2.line(img, (350, 50), (450, 50), (0,0,255))


cv2.line(img, (100, 100), (400, 100), (255,255,0), 10)
cv2.line(img, (100, 150), (400, 150), (255,0,255), 10)
cv2.line(img, (100, 200), (400, 200), (0,255,255), 10)
cv2.line(img, (100, 250), (400, 250), (200,200,200), 10)
cv2.line(img, (100, 300), (400, 300), (0,0,0), 10)


cv2.line(img, (100, 350), (400, 400), (0,0,255), 20, cv2.LINE_4)
cv2.line(img, (100, 400), (400, 450), (0,0,255), 20, cv2.LINE_8)
cv2.line(img, (100, 450), (400, 500), (0,0,255), 20, cv2.LINE_AA)
cv2.line(img, (0,0), (500,500), (0,0,255))

cv2.imshow('lines', img)
cv2.waitKey(0)
cv2.destroyAllWindows()
21 changes: 21 additions & 0 deletions CV/OpenCV/Interface/draw_poly.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import cv2
import numpy as np # 좌표 표현을 위한 numpy 모듈 ---①

img = cv2.imread('../img/blank_500.jpg')



pts1 = np.array([[50,50], [150,150], [100,140],[200,240]], dtype=np.int32)
pts2 = np.array([[350,50], [250,200], [450,200]], dtype=np.int32)
pts3 = np.array([[150,300], [50,450], [250,450]], dtype=np.int32)
pts4 = np.array([[350,250], [450,350], [400,450], [300,450], [250,350]], dtype=np.int32)


cv2.polylines(img, [pts1], False, (255,0,0))
cv2.polylines(img, [pts2], False, (0,0,0), 10)
cv2.polylines(img, [pts3], True, (0,0,255), 10)
cv2.polylines(img, [pts4], True, (0,0,0))

cv2.imshow('polyline', img)
cv2.waitKey(0)
cv2.destroyAllWindows()
11 changes: 11 additions & 0 deletions CV/OpenCV/Interface/draw_rect.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import cv2

img = cv2.imread('../img/blank_500.jpg')

cv2.rectangle(img, (50, 50), (150, 150), (255,0,0) )
cv2.rectangle(img, (300, 300), (100, 100), (0,255,0), 10 )
cv2.rectangle(img, (450, 200), (200, 450), (0,0,255), -1 )

cv2.imshow('rectangle', img)
cv2.waitKey(0)
cv2.destroyAllWindows()
40 changes: 40 additions & 0 deletions CV/OpenCV/Interface/draw_text.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import cv2

img = cv2.imread('../img/blank_500.jpg')

# sans-serif small
cv2.putText(img, "Plain", (50, 30), cv2.FONT_HERSHEY_PLAIN, 1, (0, 0,0))
# sans-serif normal
cv2.putText(img, "Simplex", (50, 70), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0,0))
# sans-serif bold
cv2.putText(img, "Duplex", (50, 110), cv2.FONT_HERSHEY_DUPLEX, 1, (0, 0,0))
# sans-serif normall X2 ---①
cv2.putText(img, "Simplex", (200, 110), cv2.FONT_HERSHEY_SIMPLEX, 2, (0,0,250))

# serif small
cv2.putText(img, "Complex Small", (50, 180), cv2.FONT_HERSHEY_COMPLEX_SMALL, \
1, (0, 0,0))
# serif normal
cv2.putText(img, "Complex", (50, 220), cv2.FONT_HERSHEY_COMPLEX, 1, (0, 0,0))
# serif bold
cv2.putText(img, "Triplex", (50, 260), cv2.FONT_HERSHEY_TRIPLEX, 1, (0, 0,0))
# serif normal X2 ---②
cv2.putText(img, "Complex", (200, 260), cv2.FONT_HERSHEY_TRIPLEX, 2, (0,0,255))

# hand-wringing sans-serif
cv2.putText(img, "Script Simplex", (50, 330), cv2.FONT_HERSHEY_SCRIPT_SIMPLEX, \
1, (0, 0,0))
# hand-wringing serif
cv2.putText(img, "Script Complex", (50, 370), cv2.FONT_HERSHEY_SCRIPT_COMPLEX, \
1, (0, 0,0))

# sans-serif + italic ---③
cv2.putText(img, "Plain Italic", (50, 430), \
cv2.FONT_HERSHEY_PLAIN | cv2.FONT_ITALIC, 1, (0, 0,0))
# sarif + italic
cv2.putText(img, "Complex Italic", (50, 470), \
cv2.FONT_HERSHEY_COMPLEX | cv2.FONT_ITALIC, 1, (0, 0,0))

cv2.imshow('draw text', img)
cv2.waitKey()
cv2.destroyAllWindows()
27 changes: 27 additions & 0 deletions CV/OpenCV/Interface/event_key.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import cv2

img_file = "../img/girl.jpg"
img = cv2.imread(img_file)
title = 'IMG'
x, y = 100, 100

while True:
cv2.imshow(title, img)
cv2.moveWindow(title, x, y)

key = cv2.waitKey(0) & 0xFF
print(key, chr(key))

if key == ord('h'):
x -= 10
elif key == ord('j'):
y += 10
elif key == ord('k'):
y -= 10
elif key == ord('l'):
x += 10
elif key == ord('q') or key == 27:
break
cv2.destroyAllWindows()
cv2.moveWindow(title, x, y )

21 changes: 21 additions & 0 deletions CV/OpenCV/Interface/event_mouse_circle.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import cv2

title = 'mouse event'
img = cv2.imread('../img/blank_500.jpg')
cv2.imshow(title, img)


def onMouse(event, x, y, flags, param):
print(event, x, y, )

if event == cv2.EVENT_LBUTTONDOWN:
cv2.circle(img, (x,y), 30, (0,0,0), -1)
cv2.imshow(title, img)


cv2.setMouseCallback(title, onMouse)

while True:
if cv2.waitKey(0) & 0xFF == 27:
break
cv2.destroyAllWindows()
38 changes: 38 additions & 0 deletions CV/OpenCV/Interface/event_mouse_circle_flag.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import cv2


title = 'mouse event'
img = cv2.imread('../img/blank_500.jpg')
cv2.imshow(title, img)


colors = {'black':(0,0,0),
'red' : (0,0,255),
'blue':(255,0,0),
'green': (0,255,0) }


def onMouse(event, x, y, flags, param):
print(event, x, y, flags)
color = colors['black']

if event == cv2.EVENT_LBUTTONDOWN:
if flags & cv2.EVENT_FLAG_CTRLKEY and flags & cv2.EVENT_FLAG_SHIFTKEY:
color = colors['green']

elif flags & cv2.EVENT_FLAG_SHIFTKEY:
color = colors['blue']

elif flags & cv2.EVENT_FLAG_CTRLKEY:
color = colors['red']

cv2.circle(img, (x,y), 30, color, -1)
cv2.imshow(title, img)


cv2.setMouseCallback(title, onMouse)

while True:
if cv2.waitKey(0) & 0xFF == 27:
break
cv2.destroyAllWindows()
29 changes: 29 additions & 0 deletions CV/OpenCV/Interface/event_trackbar.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import cv2
import numpy as np

win_name = 'Trackbar'

img = cv2.imread('../img/blank_500.jpg')
cv2.imshow(win_name,img)


def onChange(x):
print(x)

r = cv2.getTrackbarPos('R',win_name)
g = cv2.getTrackbarPos('G',win_name)
b = cv2.getTrackbarPos('B',win_name)
print(r, g, b)
img[:] = [b,g,r]
cv2.imshow(win_name, img)


cv2.createTrackbar('R', win_name, 255, 255, onChange)
cv2.createTrackbar('G', win_name, 255, 255, onChange)
cv2.createTrackbar('B', win_name, 255, 255, onChange)

while True:
if cv2.waitKey(1) & 0xFF == 27:
break

cv2.destroyAllWindows()
13 changes: 13 additions & 0 deletions CV/OpenCV/Interface/img_show.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import cv2


img_file = "../img/girl.jpg"
img = cv2.imread(img_file)


if img is not None:
cv2.imshow('IMG', img)
cv2.waitKey()
cv2.destroyAllWindows()
else:
print('No image file.')
13 changes: 13 additions & 0 deletions CV/OpenCV/Interface/img_show_gray.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import cv2

img_file = "../img/girl.jpg"

# read image with gray scaling
img = cv2.imread(img_file, cv2.IMREAD_GRAYSCALE)

if img is not None:
cv2.imshow('IMG', img)
cv2.waitKey()
cv2.destroyAllWindows()
else:
print('No image file.')
11 changes: 11 additions & 0 deletions CV/OpenCV/Interface/img_write.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import cv2

img_file = '../img/girl.jpg'
save_file = '../img/girl_gray.jpg'


img = cv2.imread(img_file, cv2.IMREAD_GRAYSCALE)
cv2.imshow(img_file, img)
cv2.imwrite(save_file, img)
cv2.waitKey()
cv2.destroyAllWindows()
22 changes: 22 additions & 0 deletions CV/OpenCV/Interface/video_cam.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import cv2

cap = cv2.VideoCapture(0)

if cap.isOpened():
while True:
ret, img = cap.read()

if ret:
cv2.imshow('camera', img)
if cv2.waitKey(1) != -1:
break

else:
print('no frame')
break
else:
print("can't open camera.")


cap.release()
cv2.destroyAllWindows()
32 changes: 32 additions & 0 deletions CV/OpenCV/Interface/video_cam_rec.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import cv2

cap = cv2.VideoCapture(0)

if cap.isOpened:
file_path = './record.avi'
fps = 30.0
fourcc = cv2.VideoWriter_fourcc(*'DIVX')
width = cap.get(cv2.CAP_PROP_FRAME_WIDTH)
height = cap.get(cv2.CAP_PROP_FRAME_HEIGHT)
size = (int(width), int(height))
out = cv2.VideoWriter(file_path, fourcc, fps, size)


while True:
ret, frame = cap.read()

if ret:
cv2.imshow('camera-recording',frame)
out.write(frame)
if cv2.waitKey(int(1000 / fps)) != -1:
break
else:
print("no frame!")
break
out.release()

else:
print("can't open camera!")

cap.release()
cv2.destroyAllWindows()
31 changes: 31 additions & 0 deletions CV/OpenCV/Interface/video_cam_resize.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import cv2


cap = cv2.VideoCapture(0)
width = cap.get(cv2.CAP_PROP_FRAME_WIDTH)
height = cap.get(cv2.CAP_PROP_FRAME_HEIGHT)

print("Original width: %d, height:%d" % (width, height) )

cap.set(cv2.CAP_PROP_FRAME_WIDTH, 320)
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 240)
width = cap.get(cv2.CAP_PROP_FRAME_WIDTH)
height = cap.get(cv2.CAP_PROP_FRAME_HEIGHT)

print("Resized width: %d, height:%d" % (width, height) )

if cap.isOpened():
while True:
ret, img = cap.read()
if ret:
cv2.imshow('camera', img)
if cv2.waitKey(1) != -1:
break
else:
print('no frame!')
break
else:
print("can't open camera!")

cap.release()
cv2.destroyAllWindows()
Loading

0 comments on commit ad4fa3b

Please sign in to comment.