Haskell反卷积的实现(Richardson Lucy)

时间:2018-08-18 04:26:11

标签: haskell image-processing deconvolution

我正在尝试在Haskell中实现一种反卷积算法,找不到比Richardson Lucy更简单的算法。我查看了现有的matlab / python实现,但无法理解从何处开始或如何实现。 我要使用的库是https://github.com/lehins/hip。 如果有人可以用一些代码片段提供一些实现的概述或有关功能的一般概念,那对我将非常有帮助。

谢谢!

1 个答案:

答案 0 :(得分:1)

该算法实际上非常简单。使用Wikipedia页面上用于Richardson-Lucy deconvolution的表示法,如果内核u0将基础图像p卷积以产生观察到的图像d,则可以迭代该函数:

deconvolve p d u = u * conv (transpose p) (d / conv p u)

u上使用初始开始估算值(例如d)以获得u0的逐步更好的估算值。

在HIP中,实际的一步deconvolve功能可能类似于:

deconvolve :: Image VS X Double
           -> Image VS RGB Double
           -> Image VS RGB Double
           -> Image VS RGB Double
deconvolve p d u
  = u * conv (transpose p) (d / conv p u)
  where conv = convolve Edge

,您可以使用类似这样的内容:

let us = iterate (deconvolve p d) d
    u10 = us !! 10  -- ten iterations

完整程序的示例是:

import Graphics.Image as I
import Graphics.Image.Interface as I
import Prelude as P

blur :: Image VS X Double
blur = blur' / scalar (I.sum blur')
  where blur' = fromLists [[0,0,4,3,2]
                          ,[0,1,3,4,3]
                          ,[1,2,3,3,4]
                          ,[0,1,2,1,0]
                          ,[0,0,1,0,0]]

deconvolve :: Image VS X Double
           -> Image VS RGB Double
           -> Image VS RGB Double
           -> Image VS RGB Double
deconvolve p d u
  = u * conv (transpose p) (d / conv p u)
  where conv = convolve Edge

main :: IO ()
main = do
  -- original underlying image
  u0 <- readImage' "images/frog.jpg" :: IO (Image VS RGB Double)
  -- the kernel
  let p = blur
  -- blurred imaged
  let d = convolve Edge p u0
  -- iterative deconvolution
  let us = iterate (deconvolve p d) d
      u1 = us !! 1 -- one iteration
      u2 = us !! 20 -- twenty iterations

  let output = makeImage (rows u0, cols u0 * 4)
        (\(r,c) ->
           let (i, c') = c `quotRem` cols u0
           in index ([u0,d,u1,u2] !! i) (r,c'))
        :: Image VS RGB Double

  writeImage "output.jpg" output

生成以下图像(从左到右),原始青蛙,模糊青蛙,一倍反褶积和二十倍反褶积。

Ribbit!