packages feed

chain-codes 0.1.0.0 → 0.2.0.0

raw patch · 2 files changed

+11/−26 lines, 2 files

Files

chain-codes.cabal view
@@ -1,5 +1,5 @@ name:                chain-codes-version:             0.1.0.0+version:             0.2.0.0 synopsis:            Library decoding chain codes from images description:         Library decoding chain codes from images homepage:            http://github.com/Fuuzetsu/chain-codes
src/Data/ChainCodes.hs view
@@ -26,6 +26,9 @@ -- | Simple alias for 'Int' pair type Position = (Int, Int) +-- | In a chain code, we'll keep the original pixel positions as well as+-- the direction which we can use for signal processing.+type ChainCode = [PixelPos]  -- | Reads in an 'Image' that uses 'PixelRGB8' as its base. -- Rejects any other format.@@ -35,28 +38,11 @@   Right _ → Left "Unsuported image format. RGB8 images only please."   Left err → Left err --- | Generic application over 'DynamicImage'.-onImage ∷ (∀ a. Image a → b) → DynamicImage → b-onImage f d = case d of-  ImageY8 img -> f img-  ImageY16 img -> f img-  ImageYF img -> f img-  ImageYA8 img -> f img-  ImageYA16 img -> f img-  ImageRGB8 img -> f img-  ImageRGB16 img -> f img-  ImageRGBF img -> f img-  ImageRGBA8 img -> f img-  ImageRGBA16 img -> f img-  ImageYCbCr8 img -> f img-  ImageCMYK8 img -> f img-  ImageCMYK16 img -> f img- -- | Given an 'Image' parametrised by 'PixelRGB8' and given a -- 'Colour', we try to find the first pixel that matches the 'Colour'. -- -- We start checking at the top left corner of the image, checking each row--- fully before progressing a column: we check @(width, height + 1)@ then+-- fully before progressing a column: we check @(width, height)@ then -- @(width, height + 1)@ and so on where top left corner of the image is (0, 0) -- and positive height is towards the bottom. findSpot ∷ Image PixelRGB8 → Colour → Maybe Position@@ -73,7 +59,9 @@  -- | Given an 'Image' parametrised by 'PixelRGB8' and given a -- 'Colour', we try to find the chain code in the binary image which has--- the passed in colour.+-- the passed in colour. Note that this is the colour of your shape and any+-- other colour is assumed to be the background: to process a black shape, pass+-- in a black colour. -- -- Note that only a single shape is accepted inside of the image. The -- starting positing is determined using 'findSpot'.@@ -81,7 +69,7 @@ -- The output list contains unique positions only: the beginning and -- end position are not treated the same. If 'findSpot' fails, we return -- 'Nothing'.-chainCode ∷ Image PixelRGB8 → Colour → Maybe [Position]+chainCode ∷ Image PixelRGB8 → Colour → Maybe ChainCode chainCode img@(Image w h d) c = findSpot img c >>= \pos →   let ppos = (fst pos, snd pos, 0)   in Just $ go [0 ..] (fromList [(0, ppos)]) ppos@@ -91,13 +79,10 @@            [ (0, 1), (1, 1), (1, 0), (1, -1)            , (0, -1), (-1, -1), (-1, 0), (-1, 1)] -    go ∷ [Int] → Map Int PixelPos → PixelPos → [Position]+    go ∷ [Int] → Map Int PixelPos → PixelPos → ChainCode     go (count:counts) positions p@(sx, sy, _) =-      map (dropThird . snd) . toList $ loop (count:counts) positions+      map snd . toList $ loop (count:counts) positions       where-        dropThird ∷ (a, b, c) → (a, b)-        dropThird (x, y, _) = (x, y)-         loop ∷ [Int] → Map Int PixelPos → Map Int PixelPos         loop (count:counts) positions           | count == 0 || not (eqV positions count) =