-
Notifications
You must be signed in to change notification settings - Fork 94
/
Copy pathopencv_capture_picture.rb
55 lines (43 loc) · 1.27 KB
/
opencv_capture_picture.rb
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
# This code also uses the ruby-opencv gem, so make sure you have it installed.
# You can install it by executing gem install ruby-opencv.
# The code assumes that you have the OpenCV library installed on your system.
require 'opencv'
cap = OpenCV::CvCapture.open(0)
def make_1080p(capture)
capture.set(3, 1920)
capture.set(4, 1080)
end
def make_720p(capture)
capture.set(3, 1020)
capture.set(4, 720)
end
def make_480p(capture)
capture.set(3, 640)
capture.set(4, 480)
end
def change_res(capture, width, height)
capture.set(3, width)
capture.set(4, height)
end
def rescale_frame(frame, percent = 75)
scale_percent = percent
width = (frame.width * scale_percent) / 100
height = (frame.height * scale_percent) / 100
dim = OpenCV::CvSize.new(width, height)
return frame.resize(dim)
end
while true
# Capture frame by frame
frame = cap.query
frame = rescale_frame(frame, 30)
# Display the resulting frame
OpenCV::GUI::Window.new('frame').show(frame)
# Display the resulting frame 2
frame2 = rescale_frame(frame, 500)
OpenCV::GUI::Window.new('frame2').show(frame2)
break if OpenCV::GUI::wait_key(20) & 0xFF == 'q'.ord
end
# When everything is done, release the capture
cap.release
OpenCV::GUI::destroy_window('frame')
OpenCV::GUI::destroy_window('frame2')