1
1
{-# LANGUAGE QuasiQuotes #-}
2
+ {-# LANGUAGE RecordWildCards #-}
2
3
{-# LANGUAGE TemplateHaskell #-}
3
4
4
5
module OpenCV.Calib3d
5
6
( FundamentalMatMethod (.. )
7
+ , FindHomographyMethod (.. )
8
+ , FindHomographyParams (.. )
6
9
, WhichImage (.. )
7
10
-- , calibrateCamera
8
11
, findFundamentalMat
12
+ , findHomography
9
13
, computeCorrespondEpilines
10
14
) where
11
15
@@ -14,6 +18,7 @@ import "base" Data.Word
14
18
import "base" Foreign.C.Types
15
19
import qualified "inline-c" Language.C.Inline as C
16
20
import qualified "inline-c-cpp" Language.C.Inline.Cpp as C
21
+ import "data-default" Data.Default
17
22
import "this" OpenCV.Internal.C.Inline ( openCvCtx )
18
23
import "this" OpenCV.Internal.C.Types
19
24
import "this" OpenCV.Internal.Calib3d.Constants
@@ -57,6 +62,18 @@ marshalWhichImage = \case
57
62
Image1 -> 1
58
63
Image2 -> 2
59
64
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
+
60
77
--------------------------------------------------------------------------------
61
78
62
79
-- {- |
@@ -143,6 +160,74 @@ findFundamentalMat pts1 pts2 method = do
143
160
c'numPts2 = fromIntegral $ V. length pts2
144
161
(c'method, c'p1, c'p2) = marshalFundamentalMatMethod method
145
162
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
+
146
231
{- | For points in an image of a stereo pair, computes the corresponding epilines in the other image
147
232
148
233
<http://docs.opencv.org/3.0-last-rst/modules/calib3d/doc/camera_calibration_and_3d_reconstruction.html#computecorrespondepilines OpenCV Sphinx doc>
0 commit comments