8000 Bindings for cv::findHomography · LumiGuide/haskell-opencv@45ef8b5 · GitHub
[go: up one dir, main page]

Skip to content

Commit 45ef8b5

Browse files
Dmitry Krylovroelvandijk
authored andcommitted
Bindings for cv::findHomography
1 parent db9f8a4 commit 45ef8b5

File tree

2 files changed

+90
-0
lines changed

2 files changed

+90
-0
lines changed

opencv/src/OpenCV/Calib3d.hs

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
{-# LANGUAGE QuasiQuotes #-}
2+
{-# LANGUAGE RecordWildCards #-}
23
{-# LANGUAGE TemplateHaskell #-}
34

45
module OpenCV.Calib3d
56
( FundamentalMatMethod(..)
7+
, FindHomographyMethod(..)
8+
, FindHomographyParams(..)
69
, WhichImage(..)
710
-- , calibrateCamera
811
, findFundamentalMat
12+
, findHomography
913
, computeCorrespondEpilines
1014
) where
1115

@@ -14,6 +18,7 @@ import "base" Data.Word
1418
import "base" Foreign.C.Types
1519
import qualified "inline-c" Language.C.Inline as C
1620
import qualified "inline-c-cpp" Language.C.Inline.Cpp as C
21+
import "data-default" Data.Default
1722
import "this" OpenCV.Internal.C.Inline ( openCvCtx )
1823
import "this" OpenCV.Internal.C.Types
1924
import "this" OpenCV.Internal.Calib3d.Constants
@@ -57,6 +62,18 @@ marshalWhichImage = \case
5762
Image1 -> 1
5863
Image2 -> 2
5964

65+
66+
data FindHomographyMethod = FindHomographyMethod0
67+
| FindHomographyMethodRANSAC
68+
| FindHomographyMethodLMEDS
69+
| FindHomographyMethodRHO
70+
71+
marshalFindHomographyMethod :: FindHomographyMethod -> Int32
72+
marshalFindHomographyMethod FindHomographyMethod0 = 0
73+
marshalFindHomographyMethod FindHomographyMethodRANSAC = c'RANSAC
74+
marshalFindHomographyMethod FindHomographyMethodLMEDS = c'LMEDS
75+
marshalFindHomographyMethod FindHomographyMethodRHO = c'RHO
76+
6077
--------------------------------------------------------------------------------
6178

6279
-- {- |
@@ -143,6 +160,74 @@ findFundamentalMat pts1 pts2 method = do
143160
c'numPts2 = fromIntegral $ V.length pts2
144161
(c'method, c'p1, c'p2) = marshalFundamentalMatMethod method
145162

163+
164+
165+
data FindHomographyParams = FindHomographyParams
166+
{ method :: FindHomographyMethod
167+
, ransacReprojThreshold :: Double
168+
, maxIters :: Int
169+
, confidence :: Double
170+
}
171+
172+
173+
instance Default FindHomographyParams where
174+
def = FindHomographyParams
175+
{ method = FindHomographyMethod0
176+
, ransacReprojThreshold = 3
177+
, maxIters = 2000
178+
, confidence = 0.995
179+
}
180+
181+
182+
findHomography
183+
:: (IsPoint2 point2 CDouble)
184+
=> V.Vector (point2 CDouble) -- ^ Points from the first image.
185+
-> V.Vector (point2 CDouble) -- ^ Points from the second image.
186+
-> FindHomographyParams
187+
-> CvExcept ( Maybe ( Mat ('S '[ 'S 3, 'S 3 ]) ('S 1) ('S Double)
188+
, Mat ('S '[ 'D, 'D ]) ('S 1) ('S Word8 )
189+
)
190+
)
191+
findHomography srcPoints dstPoints (FindHomographyParams{..}) = do
192+
(fm, pointMask) <- c'findHomography
193+
-- If the c++ function can't find a fundamental matrix it will
194+
-- return an empty matrix. We check for this case by trying to
195+
-- coerce the result to the desired type.
196+
catchE (Just . (, unsafeCoerceMat pointMask) <$> coerceMat fm)
197+
(\case CoerceMatError _msgs -> pure Nothing
198+
otherError -> throwE otherError
199+
)
200+
where
201+
c'findHomography = unsafeWrapException $ do
202+
fm <- newEmptyMat
203+
pointMask <- newEmptyMat
204+
handleCvException (pure (fm, pointMask)) $
205+
withPtr fm $ \fmPtr ->
206+
withPtr pointMask $ \pointMaskPtr ->
207+
withArrayPtr (V.map toPoint srcPoints) $ \srcPtr ->
208+
withArrayPtr (V.map toPoint dstPoints) $ \dstPtr ->
209+
[cvExcept|
210+
cv::_InputArray srcPts = cv::_InputArray($(Point2d * srcPtr), $(int32_t c'numSrcPts));
211+
cv::_InputArray dstPts = cv::_InputArray($(Point2d * dstPtr), $(int32_t c'numDstPts));
212+
*$(Mat * fmPtr) =
213+
cv::findHomography
214+
( srcPts
215+
, dstPts
216+
, $(int32_t c'method)
217+
, $(double c'ransacReprojThreshold)
218+
, *$(Mat * pointMaskPtr)
219+
, $(int32_t c'maxIters)
220+
, $(double c'confidence)
221+
);
222+
|]
223+
c'numSrcPts = fromIntegral $ V.length srcPoints
224+
c'numDstPts = fromIntegral $ V.length dstPoints
225+
c'method = marshalFindHomographyMethod method
226+
c'ransacReprojThreshold = realToFrac ransacReprojThreshold
227+
c'maxIters = fromIntegral maxIters
228+
c'confidence = realToFrac confidence
229+
230+
146231
{- | For points in an image of a stereo pair, computes the corresponding epilines in the other image
147232
148233
<http://docs.opencv.org/3.0-last-rst/modules/calib3d/doc/camera_calibration_and_3d_reconstruction.html#computecorrespondepilines OpenCV Sphinx doc>

opencv/src/OpenCV/Internal/Calib3d/Constants.hsc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,8 @@ module OpenCV.Internal.Calib3d.Constants where
1616
#num CV_FM_8POINT
1717
#num CV_FM_RANSAC
1818
#num CV_FM_LMEDS
19+
20+
#num LMEDS
21+
#num RANSAC
22+
#num RHO
23+

0 commit comments

Comments
 (0)
0