# Dct error when using Images

**URL:** https://discourse.julialang.org/t/dct-error-when-using-images/111775
**Category:** New to Julia
**Tags:** float
**Created:** [March 18, 2024, 2:54pm UTC](https://discourse.julialang.org/t/dct-error-when-using-images/111775 "2024-03-18T14:54:00Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![AbdelazimHussien](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/abdelazimhussien/32/207723_2.png) [@AbdelazimHussien](https://discourse.julialang.org/u/AbdelazimHussien)
#### Post date: [March 18, 2024, 2:54pm UTC](https://discourse.julialang.org/t/dct-error-when-using-images/111775/1 "2024-03-18T14:54:00Z")

</div>

Hello everyone,  
I have the following code

```julia
using TestImages, ImageView
img = testimage("cameraman")
pixels = img[1:8,1:8]
pixels = convert(Array{Float32}, pixels)
freqs = dct(pixels)
round(freqs)
pixels2 = idct(freqs)

```

but i can not find dct function so i got the following error  
UndefVarError: dct not defined  
Thanks in advanced

---

<div class="post-metadata">

### Author: ![sefffal](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sefffal/32/23640_2.png) [@sefffal](https://discourse.julialang.org/u/sefffal)
#### Post date: [March 18, 2024, 2:58pm UTC](https://discourse.julialang.org/t/dct-error-when-using-images/111775/2 "2024-03-18T14:58:20Z")

</div>

Hello,  
Try loading the FFTW package: `using FFTW`

[https://juliamath.github.io/FFTW.jl/latest/fft.html](https://juliamath.github.io/FFTW.jl/latest/fft.html)

---

<div class="post-metadata">

### Author: ![AbdelazimHussien](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/abdelazimhussien/32/207723_2.png) [@AbdelazimHussien](https://discourse.julialang.org/u/AbdelazimHussien)
#### Post date: [March 18, 2024, 4:24pm UTC](https://discourse.julialang.org/t/dct-error-when-using-images/111775/3 "2024-03-18T16:24:03Z")

</div>

Thanks a lot.  
It works now. But my question is that pixels == pixels2 should be true but it is false. can U help me with this?  
Here is the full code

````img
pixels = img[1:8,1:8]
pixels = convert(Array{Float32}, pixels)
freqs = dct(pixels)
round(freqs)
pixels2 = idct(freqs)
pixels == pixels2```

Thanks
````

---

<div class="post-metadata">

### Author: ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)
#### Post date: [March 18, 2024, 4:46pm UTC](https://discourse.julialang.org/t/dct-error-when-using-images/111775/4 "2024-03-18T16:46:32Z")

</div>

> [@AbdelazimHussien](#):
>
> But my question is that pixels == pixels2 should be true but it is false.

Floating-point rounding errors will make them slightly unequal. If you do:

```julia
using LinearAlgebra
norm(pixels - pixels2) / norm(pixels2)

```

you should see that the relative difference is extremely small. More succinctly, you could check `pixels ≈ pixels2`, which calls [`isapprox`](https://docs.julialang.org/en/v1/base/math/#Base.isapprox).

---

<div class="post-metadata">

### Author: ![AbdelazimHussien](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/abdelazimhussien/32/207723_2.png) [@AbdelazimHussien](https://discourse.julialang.org/u/AbdelazimHussien)
#### Post date: [March 18, 2024, 5:03pm UTC](https://discourse.julialang.org/t/dct-error-when-using-images/111775/5 "2024-03-18T17:03:46Z")

</div>

so how to solve this problem?

---

<div class="post-metadata">

### Author: ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)
#### Post date: [March 18, 2024, 5:06pm UTC](https://discourse.julialang.org/t/dct-error-when-using-images/111775/6 "2024-03-18T17:06:06Z")

</div>

You should always expect small rounding errors in any nontrivial floating-point calculation. Why is it a problem for you?

---

<div class="post-metadata">

### Author: ![AbdelazimHussien](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/abdelazimhussien/32/207723_2.png) [@AbdelazimHussien](https://discourse.julialang.org/u/AbdelazimHussien)
#### Post date: [August 18, 2024, 8:40am UTC](https://discourse.julialang.org/t/dct-error-when-using-images/111775/7 "2024-08-18T08:40:49Z")

</div>

I am so sorry  
but when I tried it again now. It is not work. I don’t know why even if I write FFTW.dct  
Any Suggestions?

---

<div class="post-metadata">

### Author: ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)
#### Post date: [August 18, 2024, 12:54pm UTC](https://discourse.julialang.org/t/dct-error-when-using-images/111775/8 "2024-08-18T12:54:01Z")

</div>

Broadcast `round()` when applied to the matrix of floats `freqs`, as follows:  
`round.(freqs)`

---

<div class="post-metadata">

### Author: ![yhuang](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yhuang/32/42523_2.png) [@yhuang](https://discourse.julialang.org/u/yhuang)
#### Post date: [August 26, 2024, 2:54am UTC](https://discourse.julialang.org/t/dct-error-when-using-images/111775/9 "2024-08-26T02:54:27Z")

</div>

Actually, `pixels` and `pixels2` should not even be close since you applied the `round` step, which brings you some loss.
