haskellimage-processinghip

Haskell HIP : Apply Filter to an Image


I'm trying to add a filter to an image using the Haskell Image Processing package HIP, I was able to read the image using the ByteString Package and convert the image to type Image VS YCbCr Word8 using HIP. Now, how do I convert from Image VS YCbCr Word8 to Border (Pixel cs e) or Pixel cs e? I'm still learning Haskell so please keep it simple. see code below :

addFilterJpg :: FilePath -> IO ()
addFilterJpg fc = do
    case validPath fc of 
        Left err -> putStrLn err
        Right img -> do
            case readImage img of
                Left err -> putStrLn err
                Right img -> do
                  -- convert img::(Image VS YCbCr Word8) to Border (Pixel cs e)
                  -- apply filter
                  -- save image
                  putStrLn "Convolution Filter"

Solution

  • There are a couple of issues with your question:

    Some more image specific remarks:

    applyFilterJpg :: FilePath -> FilePath -> IO ()
    applyFilterJpg fcin fcout = do
      eImg <- readImageExact JPG fcin
      case eImg of
        Left err -> putStrLn err
        Right img -> do
          let imgRGB :: Image VS RGB Double
              imgRGB = convert (img :: Image VS YCbCr Word8)
              gaussianBlurKernel :: Image VS X Double
              gaussianBlurKernel = fromLists $ [ [ 1/16, 1/8, 1/16 ]
                                               , [  1/8, 1/4,  1/8 ]
                                               , [ 1/16, 1/8, 1/16 ] ]
              convRGB = convolve Edge gaussianBlurKernel imgRGB
          writeImage fcout convRGB
    

    This is what we get when we run it:

    enter image description here

    That being said, there are already functions built in that will simplify this whole process for you:

    addFilterJpg' :: FilePath -> FilePath -> IO ()
    addFilterJpg' fcin fcout = do
      imgRGB <- readImageRGB VS fcin
      let convRGB = applyFilter (gaussianBlur 1) imgRGB
      writeImage fcout convRGB
    

    This is the outcome of the above function:

    enter image description here