-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathh1.py
More file actions
161 lines (149 loc) · 4.84 KB
/
h1.py
File metadata and controls
161 lines (149 loc) · 4.84 KB
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# import cv2
# import numpy as np
# from sklearn.cluster import DBSCAN
# import matplotlib.pyplot as plt
#
# img = cv2.imread("../assets/pic1.jpg")
# h, w = img.shape[:2]
# # canvas = np.zeros((5*h, 5*w, 3), dtype=np.uint8)
# # canvas[:] = 0
# # canvas[2*h:3*h, 2*w:3*w] = img
# # img = canvas
#
# bbox = (int(h / 3), int(h - h // 8.5), int(w // 5.3), int(w - w // 7.5))
# copy = img[bbox[0]: bbox[1], bbox[2]: bbox[3]].copy()
# img[:, :] = 0
# img[bbox[0]: bbox[1], bbox[2]: bbox[3]] = copy
# # h, w = img.shape[:2]
# canvas = np.zeros((3*h, 3*w, 3), dtype=np.uint8)
# canvas[:] = 0
# canvas[h:2*h, w:2*w] = img
# img = canvas
# img_draw = img.copy()
#
# gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# gray = np.power(gray / 255.0, 5) * 255
# gray = gray.astype(np.uint8)
#
# plt.hist(gray.ravel(), bins=256, range=(0, 256))
# plt.show()
#
# cv2.imshow("gamma", gray)
#
# edges = cv2.Canny(gray, 160, 240) # edge detection
# # lines = cv2.HoughLinesP(edges, rho=1, theta=np.pi / 180, threshold=160,
# # minLineLength=128, maxLineGap=16) # line detection
# cv2.imshow("edge", edges)
# cv2.waitKey()
#
# lines = cv2.HoughLinesP(edges, rho=1, theta=np.pi / 180, threshold=64,
# minLineLength=48, maxLineGap=16) # line detection
#
# line_eq = []
# for line in lines:
# x1, y1, x2, y2 = line[0]
# cv2.line(img_draw, (x1, y1), (x2, y2), (0, 255, 0), 1)
# a, b = y2 - y1, x1 - x2
# c = x2 * y1 - x1 * y2
# line_eq.append(np.array([a, b, c]))
#
# intersections = []
# for i in range(len(line_eq)):
# for j in range(i + 1, len(line_eq)):
# l1, l2 = line_eq[i], line_eq[j]
# v = np.cross(l1, l2)
# if abs(v[2]) > 1e-6:
# v = v / v[2]
# intersections.append(v[:2]) # compute intersection
# print(f"{len(intersections)} intersections in total.")
#
# for i, vp in enumerate(intersections):
# x, y = int(vp[0]), int(vp[1])
# cv2.circle(img_draw, (x, y), 2, (0, 0, 255), -1)
# cv2.putText(img_draw, f"Int{i}", (x + 10, y),
# cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0, 0, 255), 1)
#
# # intersections = np.array(intersections)
# # clustering = DBSCAN(eps=60, min_samples=6).fit(intersections) # select vanishing points from intersection
# # labels = clustering.labels_
#
# # vanishing_points = []
# # for label in set(labels):
# # if label == -1:
# # continue
# #
# # cluster = intersections[labels == label]
# # vp = cluster.mean(axis=0)
# # vanishing_points.append(vp)
# # print(f"{len(vanishing_points)} vp in total:", vanishing_points)
# #
# # for i, vp in enumerate(vanishing_points):
# # x, y = int(vp[0]), int(vp[1])
# # cv2.circle(img_draw, (x, y), 3, (0, 0, 255), -1)
# # cv2.putText(img_draw, f"VP{i}", (x + 10, y),
# # cv2.FONT_HERSHEY_SIMPLEX, 0.8, (0, 0, 255), 2)
#
#
# # if len(vanishing_points) >= 2:
# # v1, v2 = np.array([*vanishing_points[0], 1]), np.array([*vanishing_points[1], 1])
# # line = np.cross(v1, v2)
# # a, b, c = line # compute vanishing lines based on vanishing points
# # h, w = img.shape[:2]
# #
# # x1, x2 = 0, w
# # y1, y2 = int((-c - a * x1) / b), int((-c - a * w) / b)
# # cv2.line(img_draw, (x1, y1), (x2, y2), (255, 0, 0), 1)
# # cv2.putText(img_draw, "Vanishing Line", (50, 50),
# # cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 0, 0), 2)
# #
# # # contours, _ = cv2.findContours(edges, cv2.RETR_LIST, cv2.CHAIN_APPROX_NONE) # contour detection
# # # ellipse_count = 0
# # # for cnt in contours:
# # # if len(cnt) < 100:
# # # continue
# # #
# # # area = cv2.contourArea(cnt)
# # # if area < 500:
# # # continue
# # #
# # # try:
# # # ellipse = cv2.fitEllipse(cnt)
# # # cv2.ellipse(img_draw, ellipse, (255, 255, 0), 3)
# # # ellipse_count += 1
# # # if ellipse_count >= 2:
# # # break
# # # except:
# # # pass
#
# plt.figure(figsize=(10, 8))
# plt.imshow(cv2.cvtColor(img_draw, cv2.COLOR_BGR2RGB))
# plt.axis("off")
# plt.show()
from math import *
def valid_number(x, x_pred, n):
"""
abs(x - x_pred) <= (1 / 2) * (10 ^ n - p)
"""
diff = abs(x - x_pred)
res = log(2 * diff, 10)
return floor(n - res)
def factorial(n):
if n == 0:
return 1
res = 1
for i in range(1, n + 1):
res *= i
return res
if __name__ == '__main__':
print(valid_number(x=pi, x_pred=3.141, n=1))
print(valid_number(x=pi, x_pred=3.15, n=1))
print(valid_number(x=pi, x_pred=22 / 7, n=2))
x1 = 0.0
for i in range(9):
x1 += (-1) ** i * 5 ** i / factorial(i)
print(x1)
x2 = 0.0
for i in range(9):
x2 += 5 ** i / factorial(i)
print(1 / x2)
print(e ** -5)