Skip to content

Commit 84b1654

Browse files
committed
overloaded boundingRect
1 parent a710151 commit 84b1654

File tree

2 files changed

+60
-29
lines changed

2 files changed

+60
-29
lines changed

opencv/src/OpenCV/ImgProc/StructuralAnalysis.hsc

+1-29
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ module OpenCV.ImgProc.StructuralAnalysis
99

1010
, approxPolyDP, ApproxPolyDP
1111
, arcLength
12-
, boundingRect
12+
, boundingRect, BoundingRect
1313
, contourArea
1414
, convexHull, ConvexHull
1515
, findContours
@@ -39,7 +39,6 @@ import qualified "inline-c" Language.C.Inline.Unsafe as CU
3939
import "linear" Linear.V4 ( V4(..) )
4040
import "this" OpenCV.Core.Types ( Mut )
4141
import "this" OpenCV.Core.Types.Point
42-
import "this" OpenCV.Core.Types.Rect ( HRect(..), Rect2i, toRectIO )
4342
import "this" OpenCV.Core.Types.Vec ( fromVec )
4443
import "this" OpenCV.Internal.C.Inline ( openCvCtx )
4544
import "this" OpenCV.Internal.C.Types
@@ -155,33 +154,6 @@ arcLength curve isClosed = unsafeWrapException $
155154
c'isClosed = fromBool isClosed
156155
c'numCurvePoints = fromIntegral $ V.length curve
157156

158-
{- | Calculates the up-right bounding rectangle of a point set.
159-
160-
The function calculates and returns the minimal up-right bounding
161-
rectangle for the specified point set.
162-
-}
163-
-- TODO (RvD): non empty set of points, or check if V.length points >=
164-
-- 1 in haskell.
165-
-- TODO (RvD): support Int32 points
166-
boundingRect
167-
:: (IsPoint2 point2 CFloat)
168-
=> V.Vector (point2 CFloat)
169-
-> CvExcept Rect2i
170-
boundingRect points = unsafeWrapException $ do
171-
result <- toRectIO $ HRect 0 0
172-
withArrayPtr (V.map toPoint points) $ \pointsPtr ->
173-
withPtr result $ \resultPtr ->
174-
handleCvException (pure result) $
175-
[cvExcept|
176-
cv::_InputArray points =
177-
cv::_InputArray( $(Point2f * pointsPtr)
178-
, $(int32_t c'numPoints)
179-
);
180-
*$(Rect2i * resultPtr) = cv::boundingRect(points);
181-
|]
182-
where
183-
c'numPoints = fromIntegral $ V.length points
184-
185157
{- | Calculates a contour area.
186158
187159
The function computes a contour area. Similarly to `moments`, the area is

opencv/src/OpenCV/Internal/ImgProc/StructuralAnalysis.hs

+59
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
module OpenCV.Internal.ImgProc.StructuralAnalysis
1010
( approxPolyDP
1111
, ApproxPolyDP(..)
12+
, boundingRect
13+
, BoundingRect(..)
1214
, convexHull
1315
, ConvexHull(..)
1416
) where
@@ -24,6 +26,7 @@ import qualified "inline-c" Language.C.Inline as C
2426
import qualified "inline-c-cpp" Language.C.Inline.Cpp as C
2527
import qualified "inline-c" Language.C.Inline.Unsafe as CU
2628
import "this" OpenCV.Core.Types.Point
29+
import "this" OpenCV.Core.Types.Rect ( HRect(..), Rect2i, toRectIO )
2730
import "this" OpenCV.Internal.C.Inline ( openCvCtx )
2831
import "this" OpenCV.Internal.C.PlacementNew ( PlacementNew )
2932
import "this" OpenCV.Internal.C.Types
@@ -164,6 +167,62 @@ instance ApproxPolyDP CFloat where
164167

165168
--------------------------------------------------------------------------------
166169

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+
167226
{- | Finds the convex hull of a point set.
168227
169228
Finds the convex hull of a 2D point set using the Sklansky's algorithm

0 commit comments

Comments
 (0)