-- Hoogle documentation, generated by Haddock -- See Hoogle, http://www.haskell.org/hoogle/ -- | Functional Combinators for Computer Vision -- -- Initial version; using the HOpenCV package as a backend. -- -- Provides a functional combinator library, naturally expressed as Arrow -- instances (but also Category, Functor and Applicative). -- -- Online documentation, if not built below, can be found at -- http://www.ee.bgu.ac.il/~noamle/. -- -- Read the module docs for more information. See the test program -- (src/Test.hs) for example usage. @package cv-combinators @version 0.1.2.1 -- | Framework for expressing IO actions that require initialization and -- finalizers. This module provides a *functional* interface for defining -- and chaining a series of processors. -- -- Motivating example: bindings to C libraries that use functions such -- as: f(foo *src, foo *dst), where the pointer dst must be -- pre-allocated. In this case we normally do: -- --
-- foo *dst = allocateFoo(); -- ... -- while (something) { -- f(src, dst); -- ... -- } -- releaseFoo(dst); ---- -- You can use the runUntil function below to emulate that loop. -- -- Processor is an instance of Category, Functor, Applicative and Arrow. -- -- In addition to the general type Processor m a b, this -- module also defines the semantic model for Processor IO a -- b, which has synonym IOProcessor a b. module AI.CV.Processor -- | The type of Processors -- --
-- [[ 'IOProcessor' a b ]] = a -> b ---- -- And the following laws: -- --
-- [[ 'IOSource' a b ]] = (a, Time) -> b ---- -- Thus, it is ok to implement a processing action that outputs arbitrary -- time-dependent values during runtime regardless of input. (Although -- the more useful case is to calculate something from the input -- a that is also time-dependent. The a input is often -- not required and in those cases a = () is used. -- -- Notice that this means that IOSource doesn't qualify as an -- IOProcessor. However, currently the implementation does -- NOT enforce this, i.e. IOSource is not a newtype; I don't know how -- to implement it correctly. Also, one question is whether primitives -- like chain will have to disallow placing IOSource as the -- second element in a chain. Maybe they should, maybe they shouldn't. type IOSource a b = Processor IO a b -- | TODO: What's the semantic model for IOSink a? type IOSink a = IOProcessor a () -- | TODO: do we need this? we're exporting the data constructor anyway for -- now, so maybe we don't. processor :: (Monad m) => (a -> x -> m x) -> (a -> m x) -> (x -> m b) -> (x -> m ()) -> Processor m a b -- | Chains two processors serially, so one feeds the next. chain :: Processor m a b' -> Processor m b' b -> Processor m a b -- | A processor that represents two sub-processors in parallel (although -- the current implementation runs them sequentially, but that may change -- in the future) parallel :: Processor m a b -> Processor m c d -> Processor m (a, c) (b, d) -- | Constructs a processor that: given two processors, gives source as -- input to both processors and runs them independently, and after both -- have have finished, outputs their combined outputs. -- -- Semantic meaning, using Arrow's (&&&) operator: [[ -- forkJoin ]] = &&& Or, considering the Applicative instance -- of functions (which are the semantic meanings of a processor): [[ -- forkJoin ]] = liftA2 (,) Alternative implementation to consider: f -- &&& g = (,) & f * g forkJoin :: Processor m a b -> Processor m a b' -> Processor m a (b, b') -- | The identity processor: output = input. Semantically, [[ empty ]] = id empty :: (Monad m) => Processor m a a -- | Splits (duplicates) the output of a functor, or on this case a -- processor. split :: (Functor f) => f a -> f (a, a) -- | 'f --< g' means: split f and feed it into g. Useful for feeding -- parallelized (***'d) processors. For example, a -- (b *** c) = a -- >> (b &&& c) (--<) :: (Functor (cat a), Category cat) => cat a a1 -> cat (a1, a1) c -> cat a c -- | Runs the processor once: allocates, processes, converts to output, and -- deallocates. run :: (Monad m) => Processor m a b -> a -> m b -- | Keeps running the processing function in a loop until a predicate on -- the output is true. Useful for processors whose main function is after -- the allocation and before deallocation. runUntil :: (Monad m) => Processor m a b -> a -> (b -> m Bool) -> m b -- | Runs the processor once, but passes the processing + conversion action -- to the given function. runWith :: (Monad m) => (m b -> m b') -> Processor m a b -> a -> m b' type DTime = Double type Clock m = m Double -- | scanlT provides the primitive for performing memory-full operations on -- time-dependent processors, as described in -- http://www.ee.bgu.ac.il/~noamle/_downloads/gaccum.pdf. -- -- Untested. scanlT :: Clock IO -> (b -> b -> DTime -> c -> c) -> c -> IOSource a b -> IOSource a c -- | Differentiate using scanlT. TODO: test, and also generalize for any -- monad (trivial change of types). differentiate :: (Real b) => Clock IO -> IOSource a b -> IOSource a Double integrate :: (Real b) => Clock IO -> IOSource a b -> IOSource a Double max_ :: (Ord b) => Clock IO -> b -> IOSource a b -> IOSource a b min_ :: (Ord b) => Clock IO -> b -> IOSource a b -> IOSource a b instance (Monad m) => Arrow (Processor m) instance (Monad m) => Applicative (Processor m a) instance (Monad m) => Functor (Processor m a) instance (Monad m) => Category (Processor m) -- | ImageProcessors is a functional (Processor-based) interface to -- computer vision using OpenCV. -- -- The Processor interface allows the primitives in this library to take -- care of all the allocation / deallocation of resources and other -- setup/teardown requirements, and to appropriately nest them when -- combining primitives. -- -- Simple example: -- --
-- win = window 0 -- The number is essentially a label for the window -- cam = camera 0 -- Autodetect camera -- edge = canny 30 190 3 -- Edge detecting processor using canny operator -- -- test = cam >>> edge >>> win ---- -- The last expression is a processor that captures frames from camera -- and displays edge-detected version in the window. module AI.CV.ImageProcessors type ImageSink = IOSink Image type ImageSource = IOSource () Image type ImageProcessor = IOProcessor Image Image type Image = PImage -- | A capture device, using OpenCV's HighGui lib's cvCreateCameraCapture -- should work with most webcames. See OpenCV's docs for information. -- This processor outputs the latest image from the camera at each -- invocation. camera :: Int -> ImageSource videoFile :: String -> ImageSource -- | A window that displays images. Note: windows with the same index will -- be the same window....is this ok? window :: Int -> ImageSink -- | OpenCV's cvResize resize :: Int -> Int -> InterpolationMethod -> ImageProcessor -- | OpenCV's cvDilate dilate :: Int -> ImageProcessor -- | OpenCV's cvCanny canny :: Int -> Int -> Int -> ImageProcessor -- | Wrapper for OpenCV's cvHaarDetectObjects and the surrounding required -- things (mem storage, cascade loading, etc). haarDetect :: String -> Double -> Int -> HaarDetectFlag -> CvSize -> IOProcessor Image [CvRect] -- | OpenCV's cvRectangle, currently without width, color or line type -- control drawRects :: IOProcessor (Image, [CvRect]) Image -- | Runs the processor until a predicate is true, for predicates, and -- processors that take () as input (such as chains that start with a -- camera). runTill :: IOProcessor () b -> (b -> IO Bool) -> IO b -- | Name (and type) says it all. runTillKeyPressed :: (Show a) => IOProcessor () a -> IO () -- | Some general utility functions for use with Processors and OpenCV -- -- Predicate for pressed keys keyPressed :: (Show a) => a -> IO Bool