-
Notifications
You must be signed in to change notification settings - Fork 0
/
focal_length_calculator.py
118 lines (88 loc) · 3.65 KB
/
focal_length_calculator.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
import cv2
import numpy as np
import math
from enum import Enum
import general_settings as g_sets
class GripPipeline:
"""
An OpenCV pipeline generated by GRIP.
"""
def __init__(self):
"""initializes all values to presets or None if need to be set
"""
self.__hsv_threshold_hue = [0.0, 0.0]
self.__hsv_threshold_saturation = [0.0, 0.0]
self.__hsv_threshold_value = [201.79856115107913, 255.0]
self.hsv_threshold_output = None
self.__find_contours_input = self.hsv_threshold_output
self.__find_contours_external_only = False
self.find_contours_output = None
def process(self, source0):
"""
Runs the pipeline and sets all outputs to new values.
"""
# Step HSV_Threshold0:
self.__hsv_threshold_input = source0
(self.hsv_threshold_output) = self.__hsv_threshold(self.__hsv_threshold_input, self.__hsv_threshold_hue, self.__hsv_threshold_saturation, self.__hsv_threshold_value)
# Step Find_Contours0:
self.__find_contours_input = self.hsv_threshold_output
(self.find_contours_output) = self.__find_contours(self.__find_contours_input, self.__find_contours_external_only)
contour = self.__find_largest_contour(self.find_contours_output)
return contour
@staticmethod
def __hsv_threshold(input, hue, sat, val):
"""Segment an image based on hue, saturation, and value ranges.
Args:
input: A BGR numpy.ndarray.
hue: A list of two numbers the are the min and max hue.
sat: A list of two numbers the are the min and max saturation.
lum: A list of two numbers the are the min and max value.
Returns:
A black and white numpy.ndarray.
"""
out = cv2.cvtColor(input, cv2.COLOR_BGR2HSV)
return cv2.inRange(out, (hue[0], sat[0], val[0]), (hue[1], sat[1], val[1]))
@staticmethod
def __find_contours(input, external_only):
"""Sets the values of pixels in a binary image to their distance to the nearest black pixel.
Args:
input: A numpy.ndarray.
external_only: A boolean. If true only external contours are found.
Return:
A list of numpy.ndarray where each one represents a contour.
"""
if(external_only):
mode = cv2.RETR_EXTERNAL
else:
mode = cv2.RETR_LIST
method = cv2.CHAIN_APPROX_SIMPLE
contours, hierarchy =cv2.findContours(input, mode=mode, method=method)
return contours
@staticmethod
def __find_largest_contour(contours):
# returns the biggest contour in the image
if len(contours) == 0:
return contours
largest = contours[0]
if len(contours) > 0:
for contour in contours:
if cv2.contourArea(contour) > cv2.contourArea(largest):
largest = contour
return largest
frame = cv2.imread("paper.png")
video = cv2.VideoCapture(0)
while True:
_, frame = video.read()
frame = cv2.resize(
frame, (g_sets.FRAME_SIZE_WIDTH, g_sets.FRAME_SIZE_HEIGHT))
processed = GripPipeline().process(frame)
rect = cv2.minAreaRect(processed)
tape_coords, tape_dims, _ = rect
tape_width, _ = tape_dims
focal_length = tape_width * 0.5 / g_sets.TAPE_ACTUAL_WIDTH
print(focal_length)
blank_image = np.zeros([g_sets.FRAME_SIZE_HEIGHT, g_sets.FRAME_SIZE_WIDTH, 3])
image = cv2.drawContours(blank_image, processed, -1, (255, 255, 255))
cv2.imshow('pic', frame)
cv2.imshow('processed', image)
cv2.waitKey(5)