8000 Added getAffineTransformation · LumiGuide/haskell-opencv@03eff5d · GitHub
[go: up one dir, main page]

Skip to content

Commit 03eff5d

Browse files
author
Nick Van den Broeck
committed
Added getAffineTransformation
Fixed character encoding problems (stemming from T.readFile)
1 parent a3396ad commit 03eff5d

File tree

3 files changed

+38
-1
lines changed

3 files changed

+38
-1
lines changed

doc/ExampleExtractor.hs

Lines changed: 7 additions & 1 deletion
< 8000 /tr>
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import qualified "opencv" OpenCV as CV
3131
import qualified "opencv" OpenCV.Juicy as CVJ
3232
import qualified "text" Data.Text as T
3333
import qualified "text" Data.Text.IO as T
34+
import qualified "text" Data.Text.Encoding as TE
3435
import qualified "bytestring" Data.ByteString as B
3536
import qualified "bytestring" Data.ByteString.Lazy as BL
3637
import "template-haskell" Language.Haskell.TH
@@ -298,7 +299,12 @@ haddockToHaskell =
298299
. T.replace "\\/" "/"
299300

300301
findExamples :: FilePath -> IO ([ExampleSrc], [RenderTarget])
301-
findExamples fp = ((parseExamples &&& parseGeneratedImages) . textToSource fp) <$> T.readFile fp
302+
findExamples fp = ((parseExamples &&& parseGeneratedImages) . textToSource fp) <$> readFileUtf8 fp
303+
304+
-- https://www.snoyman.com/blog/2016/12/beware-of-readfile
305+
-- Data.Text.readFile has problems with character encoding.
306+
readFileUtf8 :: FilePath -> IO T.Text
307+
readFileUtf8 = fmap TE.decodeUtf8 . B.readFile
302308

303309
textToSource :: FilePath -> T.Text -> [SrcLine]
304310
textToSource fp txt = zipWith lineToSource [1..] (T.lines txt)

opencv/src/OpenCV/ImgProc/GeometricImgTransform.hsc

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,19 +52,22 @@ module OpenCV.ImgProc.GeometricImgTransform
5252
, linearPolar
5353
, logPolar
5454
, getPerspectiveTransform
55+
, getAffineTransform
5556
, getRotationMatrix2D
5657
, remap
5758
, undistort
5859
) where
5960

6061
import "base" Data.Int ( Int32 )
62+
import "base" Data.Foldable
6163
import "base" Foreign.C.Types ( CFloat, CDouble )
6264
import "base" System.IO.Unsafe ( unsafePerformIO )
6365
import qualified Data.Vector as V
6466
import qualified "inline-c" Language.C.Inline as C
6567
import qualified "inline-c" Language.C.Inline.Unsafe as CU
6668
import qualified "inline-c-cpp" Language.C.Inline.Cpp as C
6769
import "linear" Linear.V2 ( V2(..) )
70+
import "linear" Linear.V3 ( V3(..) )
6871
import "linear" Linear.Vector ( zero )
6972
import "mtl" Control.Monad.Error.Class ( MonadError )
7073
import "this" OpenCV.Core.Types
@@ -458,6 +461,30 @@ getPerspectiveTransform srcPts dstPts = unsafeCoerceMat $ unsafePerformIO $
458461
);
459462
}|]
460463

464+
{- | Calculates an affine transformation matrix for 2D affine transform
465+
466+
<http://docs.opencv.org/3.0-last-rst/modules/imgproc/doc/geometric_transformations.html#getaffinetransform OpenCV Sphinx doc>
467+
-}
468+
getAffineTransform
469+
:: forall m point2. (MonadError CvException m, IsPoint2 point2 CFloat)
470+
=> V3 (point2 CFloat) -- ^ Points representing vertices in source image
471+
-> V3 (point2 CFloat) -- ^ Points representing vertices in destination image
472+
-> m (Mat (ShapeT [2, 3]) ('S 1) ('S Double)) -- ^ The output affine transformation, 2x3 floating-point-matrix.
473+
getAffineTransform srcPts dstPts = unsafeWrapException $ do
474+
result <- newEmptyMat
475+
handleCvException (pure $ unsafeCoerceMat result) $
476+
withPtr result $ \resultPtr ->
477+
withArrayPtr (v3ToVector srcPts) $ \srcPtsPtr ->
478+
withArrayPtr (v3ToVector dstPts) $ \dstPtsPtr ->
479+
[cvExcept|
480+
*$(Mat * resultPtr) =
481+
cv::getAffineTransform($(Point2f * srcPtsPtr), $(Point2f * dstPtsPtr));
482+
}|]
483+
where
484+
v3ToVector :: V3 (point2 CFloat) -> V.Vector Point2f
485+
v3ToVector = V.map toPoint . V.fromList . toList
486+
487+
461488
{- | Calculates an affine matrix of 2D rotation
462489
463490
<http://docs.opencv.org/3.0-last-rst/modules/imgproc/doc/geometric_transformations.html#getrotationmatrix2d OpenCV Sphinx doc>

stack.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ extra-deps:
1313
extra-lib-dirs:
1414
- /usr/local/lib
1515

16+
nix:
17+
path: [ "nixpkgs=https://github.com/NixOS/nixpkgs/archive/4ccaa7de8eb34a0bb140f109a0e88095480118eb.tar.gz" ]
18+
packages: [ zlib pkgconfig opencv3 ]
19+
1620
# Uncomment if you wish to use Docker integration
1721
# See README.md for further information.
1822
#docker:

0 commit comments

Comments
 (0)
0