How to convert precision in Image type from Double to Word8 using HIP?

167 views Asked by At

I have an image in the type Image VS Y Double (after using function readImageY from HIP library) and I want to convert it to Image VS Y Word8. How should I go about doing this? I need to have it in this precision for the next function I am applying this to.

Here's a snippet of the relevant code:

import Codec.Picture         
import Codec.Picture.Types
import Control.Arrow
import Data.Ratio 
import Data.Monoid
import Graphics.Image.Processing
import qualified Graphics.Image as I
import Graphics.Image.IO
import Graphics.Image.IO.Formats
import Graphics.Image.Interface.Vector
import qualified Graphics.Image.Interface as Interface
import Data.Word (Word8)
import qualified Data.Matrix as M
import System.FilePath.Posix (splitExtension)

to2DMatrix :: FilePath -> FilePath -> Border(Interface.Pixel I.Y Word8) -> (Int, Int) -> IO ()
to2DMatrix fp fpout bor (dim1, dim2)= do
    eimg <- I.readImageY VS fp
    let new_res :: Interface.Image VS I.Y Word8
        new_res = I.resize Bilinear bor (dim1, dim2) eimg 
    let rle = twoDToMatrix $ pixelToInt $ toJPImageY8 new_res
    let (name, _) = splitExtension fp
    writeFile (name ++ ".txt") (show rle)
    writeImage fpout rle

This is the error:

Couldn't match type ‘Double’ with ‘Word8’
      Expected type: Interface.Image VS I.Y Word8
        Actual type: Interface.Image VS I.Y Double
    • In the fourth argument of ‘resize’, namely ‘eimg’
      In the expression: resize Bilinear bor (dim1, dim2) eimg
      In an equation for ‘new_res’:
          new_res = resize Bilinear bor (dim1, dim2) eimg
   |
29 |         new_res = I.resize Bilinear bor (dim1, dim2) eimg
   |                                                      ^^^^

EDIT: The doubles are the pixel values in grayscale stored as a VS vector type in the Image type. The problem I am having is getting access to the doubles to be able to convert them. Trying to interpret/find a way the HIP library here but I am new to Haskell and can't figure it out.

1

There are 1 answers

0
FifthCode On BEST ANSWER

There is a class Elevator that is available in Graphics.Image.Interface in the Haskell Image Processing (HIP) library, that allows you to change the pixel precision from/to several precision types.

Here's the snippet of the code that changed:

to2DMatrix :: FilePath  -> (Int, Int) -> IO (Maybe (M.Matrix Int)) 
to2DMatrix fp (dim1, dim2)= do
    eimg <- I.readImageY VS fp 
    let new_res :: Interface.Image VS I.Y Word8
        new_res = I.resize Bilinear Edge  (dim1, dim2) $ Interface.map conv eimg
    let rle = twoDToMatrix $ pixelToInt $ toJPImageY8 new_res
    return $ Just (rle)

conv :: Pixel Y Double -> Pixel Y Word8 
conv d = toWord8 <$> d

The <$> is an infix operator version of fmap. So this is the same as:

conv :: Pixel Y Double -> Pixel Y Word8 
conv d = fmap toWord8 d

Extra details to convert input image to matrix:

pixelToInt changes from type Pixel8 to [[Int]]:

pixelToInt :: Image Pixel8 -> [[Int]]
pixelToInt =
  map reverse . reverse .  snd . pixelFold 
    (\(lastY, ps:pss) x y p ->           
      if y == lastY                        
        then (y, (fromIntegral p:ps):pss)
        else (y, [fromIntegral p]:ps:pss))
    (0,[[]])

Then, you can use Data.Matrix to change from [[Int]] to Data.Matrix Matrix type as needed.