# Deblurring an Image using Julia (Transform from Matlab to Julia)

**URL:** <https://discourse.julialang.org/t/deblurring-an-image-using-julia-transform-from-matlab-to-julia/909>\
**Category:** Offtopic\
**Created:** [December 13, 2016, 3:11am UTC](https://discourse.julialang.org/t/deblurring-an-image-using-julia-transform-from-matlab-to-julia/909 "2016-12-13T03:11:49Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![cee878](https://avatars.discourse-cdn.com/v4/letter/c/a88e4f/32.png) [@cee878](https://discourse.julialang.org/u/cee878)\
**Post date:** [December 13, 2016, 3:11am UTC](https://discourse.julialang.org/t/deblurring-an-image-using-julia-transform-from-matlab-to-julia/909/1 "2016-12-13T03:11:49Z")

</div>

I wrote the code for deblurring an image using the heat equation. I transformed the matlab code to Julia code. But it’s not working and I don’t really understand the error message. I get an error message:

```julia
ERROR: LoadError: MethodError: no method matching ccolor(::Type{ColorTypes.Gray{FixedPointNumbers.UFixed{UInt8,8}}}, ::Type{Array{ColorTypes.Gray{FixedPointNumbers.UFixed{UInt8,8}},1}})
Closest candidates are:
  ccolor{Cdest<:ColorTypes.Colorant{T,N},Csrc<:ColorTypes.Colorant{T,N}}(::Type{Cdest<:ColorTypes.Colorant}, ::Type{Csrc<:ColorTypes.Colorant}) at /Users/hi88/.julia/v0.5/ColorTypes/src/traits.jl:237
  ccolor{Cdest<:ColorTypes.Color{T,1},T<:Number}(::Type{Cdest<:ColorTypes.Color{T,1}}, ::Type{T<:Number}) at /Users/hi88/.julia/v0.5/ColorTypes/src/traits.jl:238
 in convert(::Type{ColorTypes.Gray{FixedPointNumbers.UFixed{UInt8,8}}}, ::Array{ColorTypes.Gray{FixedPointNumbers.UFixed{UInt8,8}},1}) at /Users/hi88/.julia/v0.5/ColorTypes/src/conversions.jl:7
 in macro expansion at ./multidimensional.jl:350 [inlined]
 in macro expansion at ./cartesian.jl:64 [inlined]
 in macro expansion at ./multidimensional.jl:348 [inlined]
 in _unsafe_getindex! at ./multidimensional.jl:340 [inlined]
 in macro expansion at ./multidimensional.jl:298 [inlined]
 in _unsafe_getindex(::Base.LinearFast, ::Array{ColorTypes.Gray{FixedPointNumbers.UFixed{UInt8,8}},2}, ::Colon, ::Array{Any,1}) at ./multidimensional.jl:291
 in getindex(::Array{ColorTypes.Gray{FixedPointNumbers.UFixed{UInt8,8}},2}, ::Colon, ::Array{Any,1}) at ./abstractarray.jl:752
 in macro expansion; at /Users/hi88/Desktop/julia/heat.jl:14 [inlined]
 in anonymous at ./<missing>:?
 in include_from_node1(::String) at ./loading.jl:488
 in include_from_node1(::String) at /Applications/Julia-0.5.app/Contents/Resources/julia/lib/julia/sys.dylib:?
while loading /Users/hi88/Desktop/julia/heat.jl, in expression starting on line 13

```

This is my code:

```julia
#clear,clc;
using ImageView, Images, Colors, FixedPointNumbers, TestImages

  img=imread("clock.tiff")
  A=data(img)
  u=convert(Array,A)

  (m,n)=size(u)
  k=1;
  dt=0.2
  T=10

  for t in 0:dt:T
    u_xx=u[:, [2:n,n]]-2*u+u[:,[1, 1:n-1]]
    u_yy=u[[2:m,m],:]-2*u+u[[1,1:m-1],:]
    L=u_xx+u_yy
    u=u-k*dt*T
  end

  imshow(reinterpret(Uint8,u))
  imwrite(reinterpret(Uint8,u),"deblurredimage.tiff")
end

```

---

<div class="post-metadata">

**Author:** ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)\
**Post date:** [December 13, 2016, 4:01am UTC](https://discourse.julialang.org/t/deblurring-an-image-using-julia-transform-from-matlab-to-julia/909/2 "2016-12-13T04:01:47Z")

</div>

The error is to do with colors, and hence with the image processing. If you enter the code line by line in the REPL or in a notebook, where does the first error occur?

Also, what is the final end doing?

---

<div class="post-metadata">

**Author:** ![cee878](https://avatars.discourse-cdn.com/v4/letter/c/a88e4f/32.png) [@cee878](https://discourse.julialang.org/u/cee878)\
**Post date:** [December 13, 2016, 4:17am UTC](https://discourse.julialang.org/t/deblurring-an-image-using-julia-transform-from-matlab-to-julia/909/3 "2016-12-13T04:17:28Z")

</div>

It keeps saying that my loop is incorrect. But I think it’s fine. I don’t know if my imshow, data, and convert is used correctly in my code.

---

<div class="post-metadata">

**Author:** ![lwabeke](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lwabeke/32/4005_2.png) [@lwabeke](https://discourse.julialang.org/u/lwabeke)\
**Post date:** [December 13, 2016, 10:47am UTC](https://discourse.julialang.org/t/deblurring-an-image-using-julia-transform-from-matlab-to-julia/909/4 "2016-12-13T10:47:47Z")

</div>

Hi

You have

```julia
u_xx=u[:, [2:n,n]]-2*u+u[:,[1, 1:n-1]]

```

however

```julia
@show typeof([2:n,n])
typeof([2:n,n]) = Array{Any,1}
Array{Any,1}

julia> @show size([2:n,n])
size([2:n,n]) = (2,)
(2,)

```

I suspect what you want is:

```julia
@show typeof([2:n;n])
typeof([2:n;n]) = Array{Int64,1}
Array{Int64,1}

julia> @show size([2:n;n])
size([2:n;n]) = (512,)
(512,)

```

Thus a loop looking closer to this:

```julia
for t in 0:dt:T
           u_xx=u[:, [2:n;n]]-2*u+u[:,[1; 1:n-1]]
           u_yy=u[[2:m;m],:]-2*u+u[[1;1:m-1],:]
           L=u_xx+u_yy
           u=u-k*dt*T
         end

```

Even with that change I still get an error due to `-k*dt*T` being a float and the - operator not being implemented for the color type I have in combination with a float. You would probably need to cast between float and Fixed in some manner (sorry, I have almost no experience with the FixedPointNumbers so cannot quickly supply the cast)  
Note I don’t have the same “clock.tiff”, so I’m getting a different colortype (GrayA) on the test image I used.

---

<div class="post-metadata">

**Author:** ![cee878](https://avatars.discourse-cdn.com/v4/letter/c/a88e4f/32.png) [@cee878](https://discourse.julialang.org/u/cee878)\
**Post date:** [December 13, 2016, 1:13pm UTC](https://discourse.julialang.org/t/deblurring-an-image-using-julia-transform-from-matlab-to-julia/909/5 "2016-12-13T13:13:32Z")

</div>

I used the “clock.tiff” from [http://sipi.usc.edu/database/database.php?volume=misc](http://sipi.usc.edu/database/database.php?volume=misc). I’m just trying to deblur the image for grayscale images. Is that the problem?

---

<div class="post-metadata">

**Author:** ![lwabeke](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lwabeke/32/4005_2.png) [@lwabeke](https://discourse.julialang.org/u/lwabeke)\
**Post date:** [December 13, 2016, 1:57pm UTC](https://discourse.julialang.org/t/deblurring-an-image-using-julia-transform-from-matlab-to-julia/909/6 "2016-12-13T13:57:10Z")

</div>

As mentioned in my first post, your main problem is using a , instead of ; and thus creating a weird vector instead of the indexing vector you expected.

With that file and my modified for loop, the code now runs. Atleast until it doesn’t find the imshow on my system.

However I guess you still have a mistake, since L is never used and thus u is just the original image being modified by a constant very time through the loop.

---

<div class="post-metadata">

**Author:** ![cee878](https://avatars.discourse-cdn.com/v4/letter/c/a88e4f/32.png) [@cee878](https://discourse.julialang.org/u/cee878)\
**Post date:** [December 13, 2016, 3:08pm UTC](https://discourse.julialang.org/t/deblurring-an-image-using-julia-transform-from-matlab-to-julia/909/7 "2016-12-13T15:08:28Z")

</div>

Could I see your code? My still doesn’t run as expected. Thanks!

---

<div class="post-metadata">

**Author:** ![lwabeke](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lwabeke/32/4005_2.png) [@lwabeke](https://discourse.julialang.org/u/lwabeke)\
**Post date:** [December 13, 2016, 3:25pm UTC](https://discourse.julialang.org/t/deblurring-an-image-using-julia-transform-from-matlab-to-julia/909/8 "2016-12-13T15:25:10Z")

</div>

```julia
using ImageView, Images, Colors, FixedPointNumbers, TestImages

img=imread("clock.tiff")
  A=data(img)
  u=convert(Array,A)

  (m,n)=size(u)
  k=1;
  dt=0.2
  T=10

  for t in 0:dt:T
    u_xx=u[:, [2:n;n]]-2*u+u[:,[1; 1:n-1]]
    u_yy=u[[2:m;m],:]-2*u+u[[1;1:m-1],:]
    L=u_xx+u_yy
    u=u-k*dt*T
  end

# Renormalise to be within 0 and 1
u -= minimum(u)
u ./= maximum(u)

ImageView.view(grayim(u)) # display as seperate window
grayim(u) # display in IJulia

```

---

<div class="post-metadata">

**Author:** ![cormullion](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cormullion/32/49131_2.png) [@cormullion](https://discourse.julialang.org/u/cormullion)\
**Post date:** [December 14, 2016, 12:07pm UTC](https://discourse.julialang.org/t/deblurring-an-image-using-julia-transform-from-matlab-to-julia/909/9 "2016-12-14T12:07:12Z")

</div>

As a beginner I do find Images.jl tricky to use (if you’re more used to other systems), but I’m sure it’s more than capable of doing the job, once you’ve got the hang of it.

I went back to the original paper ([PDF](http://www4.ncsu.edu/eos/users/w/white/www/white/ma325/MVlec3.pdf) here), and tried to get their method working. I didn’t have a lot of success, but here’s as far as I’ve got. Using the `cameraman` from TestImages.jl:

 ![](https://global.discourse-cdn.com/julialang/original/3X/e/d/edafe89e0a749ddf9ae984e3d6decd9b5ed31469.png)

```julia
using Images, TestImages
function deblurimage(inputimage, targetfilename)
    width, height = widthheight(inputimage)
    dt = .00006
    dx = 1/(width-1)
    dy = 3.2/(height-1)

    source = inputimage
    target = similar(source)

    for n in 1:20
        for j = 2:height-1
            for i = 2:width-1
                target[i, j] = clamp(
                    source[i, j] - (dt/(dx^2)) * (source[i-1, j] -2source[i,j]) + 
                    source[i+1, j] - (dt/(dy^2)) * (source[i, j-1] -2source[i, j] + source[i, j+1]), 0, 1)
            end
        end
    end
    save(targetfilename, target)
end

inputimage = testimage("cameraman")
deblurimage(inputimage, "/tmp/output.tiff")

```

I’ve used `clamp()` because I don’t see how you can do all these calculations on values without pushing them outside the 0…1.0 range. @lwabeke’s idea of renormalising is good but I got too many NaN values.

I’m sure there’s a better way. I hope Images.jl starts to build up some basic tutorial material before too long…! 🙂

---

<div class="post-metadata">

**Author:** ![cee878](https://avatars.discourse-cdn.com/v4/letter/c/a88e4f/32.png) [@cee878](https://discourse.julialang.org/u/cee878)\
**Post date:** [December 20, 2016, 5:59pm UTC](https://discourse.julialang.org/t/deblurring-an-image-using-julia-transform-from-matlab-to-julia/909/10 "2016-12-20T17:59:53Z")

</div>

I tried it. But the code you provided doesn’t seem to work. Do you have any other suggestions?

---

<div class="post-metadata">

**Author:** ![lwabeke](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lwabeke/32/4005_2.png) [@lwabeke](https://discourse.julialang.org/u/lwabeke)\
**Post date:** [December 21, 2016, 12:06pm UTC](https://discourse.julialang.org/t/deblurring-an-image-using-julia-transform-from-matlab-to-julia/909/11 "2016-12-21T12:06:17Z")

</div>

Please define what you mean by not working?  
Is it giving error messages? What is the error message?

Yes, the code doesn’t deblur the image, but that is due to the fact that your original code had a bug in the algorithm.
