Skip to content

@Mastergrape /histogram.hs
Created

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Haskell Histogram
import Data.Word
import Data.Array.Repa.IO.DevIL as RD
import qualified Data.Array.Repa as R
import qualified Data.Array.Repa.Unsafe as RU
import Data.Array.Repa ((:.), Array, (:.)(..), Z(..), DIM3)
import Data.Array.Repa.Repr.ForeignPtr (F)
type Histogram = R.Array R.D R.DIM2 Int
calcHisto :: Array F DIM3 Word8 -> Histogram
calcHisto arr = let (Z:.nrRow:.nrCol:._) = R.extent arr
zero = R.fromFunction (Z:.3:.256) (\_ -> 0 :: Int)
in foldl (\hist (row,col) ->
let r = R.unsafeIndex arr (Z:.row:.col:.0)
g = R.unsafeIndex arr (Z:.row:.col:.1)
b = R.unsafeIndex arr (Z:.row:.col:.2)
in travAll (fromIntegral r,fromIntegral g,fromIntegral b) hist)
zero
[(row,col) | row <- [0..nrRow-1], col <- [0..nrCol-1]]
travAll (idr,idg,idb) hist = RU.unsafeTraverse hist id (\f t@(Z:.k:.i) -> addRight f i k t)
where
addRight f i k t
| ((i == idr) && (k == 0)) = f (Z:.k:.0) + 1
| ((i == idg) && (k == 1)) = f (Z:.k:.1) + 1
| ((i == idb) && (k == 2)) = f (Z:.k:.2) + 1
| otherwise = f t
createHist :: FilePath -> IO ()
createHist path = do
(RGB v)<- RD.runIL $ RD.readImage path
let t = calcHisto v
print "extent Histo: "
print $ R.extent t
print "Häufigkeit Wert 17 (Grün) = "
print $ t R.! (Z:.1:.17::R.DIM2)
print "Komplettes Histo Rot:\n"
print $ R.toList $ R.slice t (R.Any :. (0::Int) :. R.All)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.