9
9
module OpenCV.Internal.ImgProc.StructuralAnalysis
10
10
( approxPolyDP
11
11
, ApproxPolyDP (.. )
12
+ , boundingRect
13
+ , BoundingRect (.. )
12
14
, convexHull
13
15
, ConvexHull (.. )
14
16
) where
@@ -24,6 +26,7 @@ import qualified "inline-c" Language.C.Inline as C
24
26
import qualified "inline-c-cpp" Language.C.Inline.Cpp as C
25
27
import qualified "inline-c" Language.C.Inline.Unsafe as CU
26
28
import "this" OpenCV.Core.Types.Point
29
+ import "this" OpenCV.Core.Types.Rect ( HRect (.. ), Rect2i , toRectIO )
27
30
import "this" OpenCV.Internal.C.Inline ( openCvCtx )
28
31
import "this" OpenCV.Internal.C.PlacementNew ( PlacementNew )
29
32
import "this" OpenCV.Internal.C.Types
@@ -164,6 +167,62 @@ instance ApproxPolyDP CFloat where
164
167
165
168
--------------------------------------------------------------------------------
166
169
170
+ {- | Calculates the up-right bounding rectangle of a point set.
171
+
172
+ The function calculates and returns the minimal up-right bounding
173
+ rectangle for the specified point set.
174
+ -}
175
+ -- TODO (RvD): non empty set of points, or check if V.length points >=
176
+ -- 1 in haskell.
177
+ boundingRect
178
+ :: forall point2 depth
179
+ . ( IsPoint2 point2 depth
180
+ , BoundingRect depth
181
+ )
182
+ => V. Vector (point2 depth )
183
+ -> CvExcept Rect2i
184
+ boundingRect points = unsafeWrapException $ do
185
+ result <- toRectIO $ HRect 0 0
186
+ withArrayPtr (V. map toPoint points) $ \ pointsPtr ->
187
+ withPtr result $ \ resultPtr ->
188
+ handleCvException (pure result) $
189
+ boundingRect_internal
190
+ (fromIntegral $ V. length points)
191
+ pointsPtr
192
+ resultPtr
193
+
194
+ -- | Internal class used to overload the 'boundingRect' depth.
195
+ class ( CSizeOf (C'Point 2 depth )
196
+ , PlacementNew (C'Point 2 depth )
197
+ ) => BoundingRect depth where
198
+ boundingRect_internal
199
+ :: Int32 -- ^ Number of input points.
200
+ -> Ptr (C (Point 2 depth )) -- ^ Input points array.
201
+ -> Ptr (C Rect2i ) -- ^ Output rectangle.
202
+ -> IO (Ptr (C CvCppException ))
203
+
204
+ instance BoundingRect Int32 where
205
+ boundingRect_internal pointsSize pointsPtr resultPtr =
206
+ [cvExcept |
207
+ cv::_InputArray points =
208
+ cv::_InputArray( $(Point2i * pointsPtr)
209
+ , $(int32_t pointsSize)
210
+ );
211
+ *$(Rect2i * resultPtr) = cv::boundingRect(points);
212
+ |]
213
+
214
+ instance BoundingRect CFloat where
215
+ boundingRect_internal pointsSize pointsPtr resultPtr =
216
+ [cvExcept |
217
+ cv::_InputArray points =
218
+ cv::_InputArray( $(Point2f * pointsPtr)
219
+ , $(int32_t pointsSize)
220
+ );
221
+ *$(Rect2i * resultPtr) = cv::boundingRect(points);
222
+ |]
223
+
224
+ --------------------------------------------------------------------------------
225
+
167
226
{- | Finds the convex hull of a point set.
168
227
169
228
Finds the convex hull of a 2D point set using the Sklansky's algorithm
0 commit comments