Commit 4cb96a5dfe9e8e8403fba21778a8126511f8595f

Started work on color histogram
testclassification.hs
(82 / 5)
  
11{-# LANGUAGE FlexibleInstances #-}
22{-# LANGUAGE TypeSynonymInstances #-}
3{-# LANGUAGE ParallelListComp #-}
34module Main where
45
56--import Graphical
4141
4242import Foreign.ForeignPtr
4343import System.IO.Unsafe
44import System.FilePath.Glob
45import System.Environment
4446import Debug.Trace
4547
4648import CV.Image
335335 | i == 5 = w6
336336 | otherwise = w1
337337
338data ColorHistogramBin =
339 ColorHistogramBin{
340 pos :: (Float,Float),
341 minVal :: (Int,Int),
342 maxVal :: (Int,Int),
343 count :: Float
344 } deriving Show
345
346data ColorHistogram =
347 ColorHistogram{
348 divisions :: (Int,Int),
349 bins :: [ColorHistogramBin]
350 } deriving Show
351
352instance Graphical ColorHistogramBin where
353 draw (ColorHistogramBin (pu,pv) (minu,minv) (maxu,maxv) _) =
354 translate pu pv $
355 color (uvToColor $ colorPairToUV $ (((maxu-minu) `div` 2), ((maxv-minv) `div` 2))) $
356 circleSolid 1
357
358instance Graphical ColorHistogram where
359 draw (ColorHistogram (du,dv) bs) =
360 pictures $ map ((scale (1.0/(fromIntegral du)) (1.0/(fromIntegral dv))) . draw) bs
361
362-- number of divisions
363-- minimum value
364-- maximum value
365makeColorHistogramBins :: (Int,Int) -> (Int,Int) -> (Int,Int) -> [ColorHistogramBin]
366makeColorHistogramBins (nu,nv) (minu,minv) (maxu,maxv) =
367 [ (ColorHistogramBin (((fromIntegral pu) * (fromIntegral nu - 0.5)),
368 ((fromIntegral pv) * (fromIntegral nv - 0.5)))
369 ((bu (fromIntegral pu)), (bv (fromIntegral pv)))
370 ((bu (fromIntegral $ pu+1)), (bv (fromIntegral $ pv+1))) 0.0) |
371 pu <- [0..nu-1], pv <- [0..nv-1] ]
372 where
373 su = 1.0/(fromIntegral nu)
374 sv = 1.0/(fromIntegral nv)
375 wu = ((fromIntegral $ maxu - minu) / (fromIntegral nu))::Float
376 wv = ((fromIntegral $ maxv - minv) / (fromIntegral nv))::Float
377 bu p = (minu + (floor $ p * wu))::Int
378 bv p = (minv + (floor $ p * wv))::Int
379
380-- list of color pairs with intensity
381-- number of divisions in horizontal and vertical direction
382-- number of bins on intensity axis
383-- list of bins containing row and col number of bin, number of elements in bin
384-- and list containing bin values on intensity axis
385makeColorHistogram :: [(Int,Int)] -> (Int,Int) -> Int -> [ColorHistogramBin]
386makeColorHistogram cs (nu,nv) ni = bs
387 where
388 (minu,maxu) = (0,255)
389 (minv,maxv) = (0,255)
390 bs = makeColorHistogramBins (nu,nv) (minu,minv) (maxu,maxv)
391
338392plotColor :: (Int, Int) -> Picture
339393plotColor (c1,c2) =
340394 (translate (u * 200) (v * 200)) $ color (uvToColor (u,v)) $ (circleSolid 10.0)
543543plotHistList xs = do
544544 Plot.plot X11.cons $ Plot2D.list Graph2D.boxes $ zip [0..(length xs)] xs
545545
546imagefile = "./sign/200px-Pakollinen_pysayttaminen_232.svg.png" --ajoneuvolla_ajo_kielletty.png"
546imagefile = "./sign/418px-Muu_nahtavyys_772f.svg.png" --ajoneuvolla_ajo_kielletty.png"
547547b1 = cvColorImageToBlocksWithFeatures imagefile
548548p1 = cvColorImageToColorPairs imagefile
549549
550main = --plotScatterAll
550main = do
551 args <- getArgs
552 if null args
553 then do
554 print $ "Please give a pattern to find image files"
555 else do
556 fs <- glob $ compile $ head $ args
557 print $ "Loading " ++ (show $ length fs) ++ " images"
558
559 --let ps = normalize $ map fileToFeatureVector fs
560 --plotHistList ps
561 --let ps = concatMap cvColorImageToColorPairs fs
562 --plotScatterPairs ps
563 displayInWindow "forest" (512, 512) (200, 200) white $
564 translate (-50) 50 $ scale 100 100 $ draw $ makeColorHistogram p1 (16,16) 16
565 -- pictures $ map plotColor $ ps
566 print $ "Finished analyzing " ++ (show $ length fs) ++ " images"
567
568 --plotScatterAll
551569 --print $ normShapes
552 do
570 --do
571 {-
553572 plotHistList $ fileToFeatureVector "./sign/ajoneuvolla_ajo_kielletty.png"
554573 plotHistList $ fileToFeatureVector "./sign/kielletty_ajosuunta.png"
555574 plotHistList $ fileToFeatureVector "./sign/kapeneva_tie.png"
556575 plotHistList $ fileToFeatureVector "./sign/lapsia.png"
557576 plotHistList $ fileToFeatureVector "./sign/leirintaalue.png"
558577 plotHistList $ fileToFeatureVector "./sign/levahdysalue.png"
578 -}
559579 --plotHistList $ fileToFeatureVector "./sign/Pakollinen_pysayttaminen_232.svg.png"
560580 --plotHistList $ fileToFeatureVector "./sign/200px-Pakollinen_pysayttaminen_232.svg.png"
561581
597597 --print $ bw1
598598 --displayInWindow "forest" (512, 512) (200, 200) white $
599599 -- translate (-256) (256) $ pictures $ map draw $ map mirrorYBlock b1
600 --pictures $ map plotColor p1
600 -- pictures $ map plotColor p1
601601 --print $ length p1
602602
603--amnipar@amnipar-aspire-kubuntu:~/Projects/trafficsigner/signclassification/sign$ convert -background "#808080" -mattecolor "#808080" -bordercolor "#808080" Pakollinen_pysäyttäminen_232.svg -size 512x512 -rotate 4 -distort perspective "0,0,0,0 0,512,0,512 512,0,512,64 512,512,512,448" -frame 20x20 -flatten ---channel RGB -depth 8 -evaluate Gaussian-noise 6 Pakollinen_pysäyttäminen_232.svg.png
603--convert -background "#808080" -mattecolor "#808080" -bordercolor "#808080" Pakollinen_pysäyttäminen_232.svg -size 512x512 -rotate 4 -distort perspective "0,0,0,0 0,512,0,512 512,0,512,64 512,512,512,448" -frame 20x20 -flatten ---channel RGB -depth 8 -evaluate Gaussian-noise 6 Pakollinen_pysäyttäminen_232.svg.png