66import numpy as np
77
88from opsi .manager .manager_schema import Function
9- from opsi .manager .types import Slide
109from opsi .util .cv import Contours , Mat , MatBW , Point
10+ from opsi .util .cv .shape import Corners
1111
1212__package__ = "opsi.contours"
1313__version__ = "0.123"
@@ -79,7 +79,7 @@ class Outputs:
7979 def run (self , inputs ):
8080 if len (inputs .contours .l ) == 0 :
8181 return self .Outputs (center = None , success = False , visual = inputs .img )
82-
82+ res = inputs . contours . l [ 0 ]. res
8383 center = inputs .contours .centroid_of_all
8484
8585 if self .settings .draw :
@@ -99,7 +99,11 @@ def run(self, inputs):
9999 else :
100100 img = None
101101
102- return self .Outputs (center = center , success = True , visual = img )
102+ scaled_center = Point (
103+ x = ((center .x * 2 ) / res .x ) - 1 , y = ((center .y * 2 ) / res .y ) - 1
104+ )
105+
106+ return self .Outputs (center = scaled_center , success = True , visual = img )
103107
104108
105109class FindAngle (Function ):
@@ -118,39 +122,91 @@ def calculate_focal_length(cls, diagonalFOV, horizontalAspect, verticalAspect):
118122 math .atan (math .tan (diagonalView / 2 ) * (horizontalAspect / diagonalAspect ))
119123 * 2
120124 )
121- # verticalView = math.atan(math.tan(diagonalView/2) * (verticalAspect / diagonalAspect)) * 2
125+ verticalView = (
126+ math .atan (math .tan (diagonalView / 2 ) * (verticalAspect / diagonalAspect ))
127+ * 2
128+ )
122129
123130 # Since point is -1 <= (x, y) <= 1: width, height = 2; center = (0, 0)
124131
125132 # Focal Length calculations: https://docs.google.com/presentation/d/1ediRsI-oR3-kwawFJZ34_ZTlQS2SDBLjZasjzZ-eXbQ/pub?start=false&loop=false&slide=id.g12c083cffa_0_165
126133 H_FOCAL_LENGTH = 2 / (2 * math .tan ((horizontalView / 2 )))
127- # V_FOCAL_LENGTH = 2 / (2* math.tan((verticalView/ 2)))
134+ V_FOCAL_LENGTH = 2 / (2 * math .tan ((verticalView / 2 )))
128135
129- return H_FOCAL_LENGTH
136+ return H_FOCAL_LENGTH , V_FOCAL_LENGTH
130137
131138 @dataclass
132139 class Settings :
140+ mode : ("Degrees" , "Radians" ) = "Radians"
133141 diagonalFOV : float = 68.5
134142
135143 @dataclass
136144 class Inputs :
137- pnt : Point
145+ point : Point
138146 img : Mat
139147
140148 @dataclass
141149 class Outputs :
142- radians : float
150+ angle : Point
143151
144152 def run (self , inputs ):
145- width = inputs .img .shape [ 1 ]
146- height = inputs .img .shape [ 0 ]
153+ width = inputs .img .res . x
154+ height = inputs .img .res . y
147155
148156 center_x = 0
149- x = inputs .point [0 ]
157+ x = inputs .point .x
158+ y = inputs .point .y
150159
151- H_FOCAL_LENGTH = self .calculate_focal_length (
160+ h_focal_length , v_focal_length = self .calculate_focal_length (
152161 self .settings .diagonalFOV , width , height
153162 )
154- radians = math .atan2 (x , H_FOCAL_LENGTH )
155163
156- return self .Outputs (radians = radians )
164+ if self .settings .mode == "Radians" :
165+ radians = Point (
166+ x = math .atan2 (x , h_focal_length ), y = math .atan2 (y , v_focal_length )
167+ )
168+ return self .Outputs (angle = radians )
169+ else :
170+ degrees = Point (
171+ x = math .degrees (math .atan2 (x , h_focal_length )),
172+ y = math .degrees (math .atan2 (y , v_focal_length )),
173+ )
174+ return self .Outputs (angle = degrees )
175+
176+
177+ class FindCorners (Function ):
178+ @dataclass
179+ class Inputs :
180+ contours : Contours
181+
182+ @dataclass
183+ class Outputs :
184+ corners : Corners
185+ success : bool
186+
187+ def run (self , inputs ):
188+ if len (inputs .contours .l ) == 0 :
189+ return self .Outputs (corners = None , success = False )
190+
191+ cnt = inputs .contours .l [0 ]
192+
193+ ret , corners = cnt .corners
194+
195+ return self .Outputs (corners = corners , success = ret )
196+
197+
198+ class FindArea (Function ):
199+ @dataclass
200+ class Inputs :
201+ contours : Contours
202+
203+ @dataclass
204+ class Outputs :
205+ area : float
206+
207+ def run (self , inputs ):
208+ if inputs .contours is None or len (inputs .contours .l ) == 0 :
209+ return self .Outputs (area = 0 )
210+ else :
211+ return self .Outputs (area = sum ([cnt .area for cnt in inputs .contours .l ]))
212+
0 commit comments