Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How can I construct (and destruct) masked images? #25

Closed
herrzinter opened this issue Apr 20, 2016 · 1 comment
Closed

How can I construct (and destruct) masked images? #25

herrzinter opened this issue Apr 20, 2016 · 1 comment

Comments

@herrzinter
Copy link

I have some problems with using masked images. I try to create a masked image from a normal one by using a fromFunction and although the function itself compiles correctly, the application of the function yields a type error during compilation. I set up an equivalent example to illustrate the problem as my original function is a bit complex.

{-# LANGUAGE ScopedTypeVariables, TypeFamilies #-}

import Vision.Image
import Vision.Image.Storage.DevIL (Autodetect (..), load)
import Vision.Primitive


maskImage :: (Image i1, FromFunction i,
              FromFunctionPixel i ~ Maybe (ImagePixel i1))
          => i1 -> i
maskImage img = fromFunction (shape img) maskPixel
    where maskPixel p = Just $ img `index` p


main = do
    io <- load Autodetect "../test.jpg"

    case io of
        Right (rgb :: RGB) -> do
            let i' = maskImage rgb

            print "Processed image"

The snippet yields the compilation error:

Main.hs:27:22:
    Couldn't match type ‘FromFunctionPixel i0’ with ‘Maybe RGBPixel’
    The type variable ‘i0’ is ambiguous
    Expected type: Maybe (ImagePixel RGB)
      Actual type: FromFunctionPixel i0
    Relevant bindings include i' :: i0 (bound at Main.hs:27:17)
    In the expression: maskImage rgb
    In an equation for ‘i'’: i' = maskImage rgb
    In the expression:
      do { let i' = maskImage rgb;
           print "Processed image" }

I also tried to create to unmask the image afterwards but this didn't work either. So how would one create a masked image with fromFunction and destruct it later again? Or why doesn't my code compile correctly?

Background: I try to implement an image registration algorithm. One step of the algorithm is to interpolate the moved image at the points of the orgiinal image. As usually not all pixels of the original and the moved image overlap, a masked image was my data structure of choice to represent the result of this processing step.

@herrzinter herrzinter changed the title Constructing (and destructing) masked images How can I construct (and destruct) masked images? Apr 20, 2016
@herrzinter
Copy link
Author

The code compiles, if the type signature :: DelayedMask RGBPixel is added to the i' variable:

let i' = maskImage rgb :: DelayedMask RGBPixel

The problem arised as the fromFunction, and thus, the maskImage function are polymorphic ie. there type signature uses a type variable. At compile time, the compiler has to known the concrete type of this variable, thus, the type has to be specified explicitely or the compiler has to be able to infer the value. The type signature defines the type explicetly ie. it tells the compiler, that the application of the maskImage function results in a masked image of rgb pixels. Similiarly, if the convert function is used, the types have be instances of the corresponding typeclass, and the output type has to be specified by a type signature if it cannot be infered otherwise.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant