 --                 hg <- buildHistogram cbins image
 --                 bins <-  mapM (getBin hg) [0..cbins-1]
 --                 let avg = sum bins / (fromIntegral.length) bins
---                 let u3 = sum.map (\(value,bin) -> 
+--                 let u3 = sum.map (\(value,bin) ->
 --                                     (value-avg)*(value-avg)*(value-avg)
 --                                     *bin) $
---                            zip binValues bins 
---                 let u2 = sum.map (\(value,bin) -> 
+--                            zip binValues bins
+--                 let u2 = sum.map (\(value,bin) ->
 --                                     (value-avg)*(value-avg)
 --                                     *bin) $
---                            zip binValues bins 
-----                
+--                            zip binValues bins
+----
 --                 return (u3 / (sqrt u2*sqrt u2*sqrt u2))
 --                where
 --                 cbins :: CInt

liftValues op (HGD a) = zip bins (op values)
            where (bins,values) = unzip a

sub (HGD a) (HGD b) | bins a == bins b 
                    = HGD $ zip (bins a) values
                where
                 bins a = map fst a
                 msnd = map snd
                 values = zipWith (-) (msnd a) (msnd b)
              

noBins (HGD a) = length a


-- | Given a set of images, such as the color channels of color image, and
--   a histogram with corresponding number of channels, replace the pixels of
--   the image with the likelihoods from the histogram
backProjectHistogram :: [Image GrayScale D8] -> I.Histogram -> Image GrayScale D8
backProjectHistogram images@(img:_) (I.Histogram hist) = unsafePerformIO $ do
    r <- cloneImage img
    withImage r $ \c_r ->
     withPtrList (map imageFPTR images) $ \ptrs ->
      withForeignPtr hist $ \c_hist ->
        I.c'cvCalcArrBackProject (castPtr ptrs) (castPtr c_r) c_hist
    return r
backProjectHistogram _ _ = error "Empty list of images"

-- | Calculate an opencv histogram object from set of images, each with it's
-- own number of bins.
--histogram :: [(Image GrayScale D8, Int)] -> Bool -> Maybe (Image GrayScale D8)
--               -> I.Histogram

--histogram imageBins
--  I.creatingHistogram $ do
--        hist <-  I.emptyUniformHistogramND ds
--        withPtrList (map imageFPTR images) $ \ptrs ->
--         case mask of
--            Just m -> do
--                withImage m $ \c_mask -> do
--                I.c'cvCalcArrHist (castPtr ptrs) hist c_accumulate (castPtr c_mask)
--                return hist
--            Nothing -> do
--                I.c'cvCalcArrHist (castPtr ptrs) hist c_accumulate (nullPtr)
--                return hist
--   where
--    (images,ds) = unzip imageBins
--    c_accumulate = 0

-- getHistogramBin (I.Histogram hs) n = unsafePerformIO $ withForeignPtr hs (\h -> I.c'cvGetHistValue_1D (castPtr h) n)

