Commit a6a5f39e2247f4533b577eb8f6f547836f385c9f

Renamed background estimator source
backgroundEstimator.hs
(66 / 0)
  
1{-# LANGUAGE ParallelListComp, RecordWildCards, DeriveDataTypeable #-}
2module Main where
3import CV.Image
4import CV.Video
5import Control.Monad
6import qualified CV.ImageMath as IM
7import Utils.List
8import Data.Function
9import Data.List
10import System.Console.CmdArgs
11
12estimateBackground cap =
13 do
14 let noFrames = getNumberOfFrames cap
15 n = 35
16 m = noFrames `div` n
17 print ("Frames",noFrames,"Skip",m)
18 frames <- replicateM n (replicateM_ m (dropFrame cap)
19 >> print ("Frame",getFrameNumber cap)
20 >> getFrame cap
21 >>= convertToGrayScale)
22 -- >>= return . drop 10
23 -- sequence_ [saveImage ("frame_"++show i++".png") img
24 -- | img <- frames | i<-[1..]]
25
26 background <- assemble frames
27 return background
28
29assemble frames = do
30 let (w,h) = getSize (head frames)
31 (fw,fh) = (20,20)
32 empty = emptyCopy (head frames)
33 areas = [(x,y) | x <- ([0,fw..w-fw]++[w-fw])
34 , y <- ([0,fh..h-fh]++[h-fh])]
35 sequence_ [blit empty (fst $ chooseBestTile p (fw,fh) frames)
36 p
37 | p <- areas ]
38 return empty
39
40chooseBestTile pos size imgs = minimumBy (compare`on`snd)
41 $ zip tiles dists
42 where
43 dists = [ median [IM.sum (IM.absDiff a b) | a <- tiles]
44 | b <- tiles]
45 tiles = map (getRegion pos size) imgs
46
47
48data Args = Args {file :: String, out :: String}
49 deriving (Show, Data, Typeable)
50
51arguments = mode $ Args {file = "" &= argPos 0 & typFile
52 & text "input file",
53 out = "background" &= typFile
54 & text "output file" }
55
56
57main = do
58 Args{..} <- cmdArgs
59 ("Background estimator, @Ville Tirronen 2010 -- \n" ++
60 "Assumes no shadows, relatively few moving objects,\n"++
61 "and a static camera.")
62 [arguments]
63 cap <- captureFromFile file
64 bkg <- estimateBackground cap
65 saveImage out bkg
66
testi.hs
(0 / 66)
  
1{-# LANGUAGE ParallelListComp, RecordWildCards, DeriveDataTypeable #-}
2module Main where
3import CV.Image
4import CV.Video
5import Control.Monad
6import qualified CV.ImageMath as IM
7import Utils.List
8import Data.Function
9import Data.List
10import System.Console.CmdArgs
11
12estimateBackground cap =
13 do
14 let noFrames = getNumberOfFrames cap
15 n = 35
16 m = noFrames `div` n
17 print ("Frames",noFrames,"Skip",m)
18 frames <- replicateM n (replicateM_ m (dropFrame cap)
19 >> print ("Frame",getFrameNumber cap)
20 >> getFrame cap
21 >>= convertToGrayScale)
22 -- >>= return . drop 10
23 -- sequence_ [saveImage ("frame_"++show i++".png") img
24 -- | img <- frames | i<-[1..]]
25
26 background <- assemble frames
27 return background
28
29assemble frames = do
30 let (w,h) = getSize (head frames)
31 (fw,fh) = (20,20)
32 empty = emptyCopy (head frames)
33 areas = [(x,y) | x <- ([0,fw..w-fw]++[w-fw])
34 , y <- ([0,fh..h-fh]++[h-fh])]
35 sequence_ [blit empty (fst $ chooseBestTile p (fw,fh) frames)
36 p
37 | p <- areas ]
38 return empty
39
40chooseBestTile pos size imgs = minimumBy (compare`on`snd)
41 $ zip tiles dists
42 where
43 dists = [ median [IM.sum (IM.absDiff a b) | a <- tiles]
44 | b <- tiles]
45 tiles = map (getRegion pos size) imgs
46
47
48data Args = Args {file :: String, out :: String}
49 deriving (Show, Data, Typeable)
50
51arguments = mode $ Args {file = "" &= argPos 0 & typFile
52 & text "input file",
53 out = "background" &= typFile
54 & text "output file" }
55
56
57main = do
58 Args{..} <- cmdArgs
59 ("Background estimator, @Ville Tirronen 2010 -- \n" ++
60 "Assumes no shadows, relatively few moving objects,\n"++
61 "and a static camera.")
62 [arguments]
63 cap <- captureFromFile file
64 bkg <- estimateBackground cap
65 saveImage out bkg
66