# Using Clipping on an image

**URL:** <https://discourse.julialang.org/t/using-clipping-on-an-image/21782>\
**Category:** General Usage\
**Created:** [March 12, 2019, 2:28pm UTC](https://discourse.julialang.org/t/using-clipping-on-an-image/21782 "2019-03-12T14:28:19Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![GERSON\_EZEQUIEL\_HERN](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gerson_ezequiel_hern/32/7437_2.png) [@GERSON\_EZEQUIEL\_HERN](https://discourse.julialang.org/u/GERSON_EZEQUIEL_HERN)\
**Post date:** [March 12, 2019, 2:28pm UTC](https://discourse.julialang.org/t/using-clipping-on-an-image/21782/1 "2019-03-12T14:28:20Z")

</div>

Using Clipping on an image.

When you have a grayscale image, it is quantized to one byte.  
That means that only 255 bts can be quantified and can not be exceeded or have negative values.  
Clipping = set limits of the quantification between 0 and 255  
yes x \<0 yes x\> 255  
x == 0 x == 255

(I would like to do it with a color image such as the mandrill)  
I have problems with Clipping on an image, someone could help me.

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [March 13, 2019, 9:31pm UTC](https://discourse.julialang.org/t/using-clipping-on-an-image/21782/2 "2019-03-13T21:31:04Z")

</div>

Could you post a minimum working example of exactly what you’re trying to do and what error you’re seeing?

---

<div class="post-metadata">

**Author:** ![longemen3000](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/longemen3000/32/7298_2.png) [@longemen3000](https://discourse.julialang.org/u/longemen3000)\
**Post date:** [March 14, 2019, 6:31am UTC](https://discourse.julialang.org/t/using-clipping-on-an-image/21782/3 "2019-03-14T06:31:10Z")

</div>

i suppose you have a code like this:

```julia
using Images, TestImages
img = TestImage("mandrill")
img2 = Gray.(img) #grayscale image from color image

```

the format of the images are a type of Normalized integer, between 0 and 1, so to convert to values between 0 and 255, we just have to multiply:

```julia
img3 = 255 .* img2
img4 = convert.(Float64,img3) # to transform the array of colors to array of Float64

```

Knowing about the implicit types used in the images.jl package, you can actually extract directly the integer value, without converting to a floating number:

```julia
img3 = broadcast(x->Int(x.val.i),img2) #applies the function over the entire image

```

this works because:

- Gray is a struct with val as a field of type Normed number
- a Normed Number is a struct with i as the integer value, and because is a N0f8 type, is normalized with 8 bits, so the max value of that integer is 255 (what you are looking for, using the Mandrill example from TestImages)

So, the fist solution is general, the second solution is more specific and fast, you choose 😁.

Also, remember to always put a minimal working example, the people can’t help you if they don’t understand what your code is like, i think what you have based on former questions you asked here.

PD: i don’t work with images in julia, but is fun!

---

<div class="post-metadata">

**Author:** ![GERSON\_EZEQUIEL\_HERN](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gerson_ezequiel_hern/32/7437_2.png) [@GERSON\_EZEQUIEL\_HERN](https://discourse.julialang.org/u/GERSON_EZEQUIEL_HERN)\
**Post date:** [March 19, 2019, 5:42pm UTC](https://discourse.julialang.org/t/using-clipping-on-an-image/21782/4 "2019-03-19T17:42:39Z")

</div>

> [@longemen3000](#):
>
> ```julia
> 
> ```

Clipping = establishes limits of the quantification between 0 and 255, this is something in matlab (so they expressed it to me)

example in matlab

im = imread (‘pout.tif’);  
imhsow (im)  
im = double (im); % the data type was changed  
[ren, col] = size (im); % number of rows and columns  
im2 = zeros (ren, col);  
k = 100;  
k = 100;  
for i = 1: re  
for j = 1: col  
im2 (i, j) = im (i, j) + k;  
end  
end

something like this or similar I would like to do it in matlab, step by step, could someone help me?

---

<div class="post-metadata">

**Author:** ![longemen3000](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/longemen3000/32/7298_2.png) [@longemen3000](https://discourse.julialang.org/u/longemen3000)\
**Post date:** [March 20, 2019, 1:55am UTC](https://discourse.julialang.org/t/using-clipping-on-an-image/21782/5 "2019-03-20T01:55:13Z")

</div>

> [@GERSON\_EZEQUIEL\_HERN](#):
>
> im = double (im); % the data type was changed  
> [ren, col] = size (im); % number of rows and columns  
> im2 = zeros (ren, col);  
> k = 100;  
> k = 100;  
> for i = 1: re  
> for j = 1: col  
> im2 (i, j) = im (i, j) + k;  
> end  
> end

I see a problem with the overflow, but in theory, you can easily sum said number to the image, this is the matlab algorithm transformed to julia

```julia
using Images, TestImages
img = TestImage("mandrill")
im = Gray.(img)
im = float64(img2) #im = double(im)
(ren, col) = size(im)
img2 = zeros(ren,col)
k = 100/255
for i = 1:re
for j = 1:col
im [i, j] = im[i, j]+k
end
end

```

Respecting the clipping, i really don´t know, i’m not a professional, even an amateur, in that area.

---

<div class="post-metadata">

**Author:** ![GERSON\_EZEQUIEL\_HERN](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gerson_ezequiel_hern/32/7437_2.png) [@GERSON\_EZEQUIEL\_HERN](https://discourse.julialang.org/u/GERSON_EZEQUIEL_HERN)\
**Post date:** [March 20, 2019, 2:33pm UTC](https://discourse.julialang.org/t/using-clipping-on-an-image/21782/6 "2019-03-20T14:33:46Z")

</div>

algo asi lo terngo interpretado pero me falto  
im = float64(img2)

---

<div class="post-metadata">

**Author:** ![longemen3000](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/longemen3000/32/7298_2.png) [@longemen3000](https://discourse.julialang.org/u/longemen3000)\
**Post date:** [March 20, 2019, 7:39pm UTC](https://discourse.julialang.org/t/using-clipping-on-an-image/21782/7 "2019-03-20T19:39:37Z")

</div>

funcionó?

---

<div class="post-metadata">

**Author:** ![GERSON\_EZEQUIEL\_HERN](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gerson_ezequiel_hern/32/7437_2.png) [@GERSON\_EZEQUIEL\_HERN](https://discourse.julialang.org/u/GERSON_EZEQUIEL_HERN)\
**Post date:** [March 21, 2019, 11:44pm UTC](https://discourse.julialang.org/t/using-clipping-on-an-image/21782/8 "2019-03-21T23:44:31Z")

</div>

me sale este error en consola

julia\> img = TestImage(“mandrill”)  
ERROR: UndefVarError: TestImage not defined  
Stacktrace:  
[1] top-level scope at none:0

---

<div class="post-metadata">

**Author:** ![stillyslalom](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stillyslalom/32/45687_2.png) [@stillyslalom](https://discourse.julialang.org/u/stillyslalom)\
**Post date:** [March 21, 2019, 11:55pm UTC](https://discourse.julialang.org/t/using-clipping-on-an-image/21782/9 "2019-03-21T23:55:59Z")

</div>

> [@GERSON\_EZEQUIEL\_HERN](#):
>
> me sale este error en consola

Capitalization matters: `TestImage` is different from `testimage`.

```julia
using TestImages
img = testimage("mandrill")

```

---

<div class="post-metadata">

**Author:** ![longemen3000](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/longemen3000/32/7298_2.png) [@longemen3000](https://discourse.julialang.org/u/longemen3000)\
**Post date:** [March 21, 2019, 11:56pm UTC](https://discourse.julialang.org/t/using-clipping-on-an-image/21782/10 "2019-03-21T23:56:38Z")

</div>

> [@GERSON\_EZEQUIEL\_HERN](#):
>
> me sale este error en consola
> 
> julia\> img = TestImage(“mandrill”)  
> ERROR: UndefVarError: TestImage not defined  
> Stacktrace:  
> [1] top-level scope at none:0

jaajj, a mi igual, es que es en minusculas:

```julia
using TestImages, Images
img = testimage("mandrill")

```

---

<div class="post-metadata">

**Author:** ![GERSON\_EZEQUIEL\_HERN](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gerson_ezequiel_hern/32/7437_2.png) [@GERSON\_EZEQUIEL\_HERN](https://discourse.julialang.org/u/GERSON_EZEQUIEL_HERN)\
**Post date:** [March 22, 2019, 12:14am UTC](https://discourse.julialang.org/t/using-clipping-on-an-image/21782/11 "2019-03-22T00:14:23Z")

</div>

eso no tiene que ver mucho, bueno yo estoy haciendo con mayusculas y minusculas y sale los mism, mismo error

---

<div class="post-metadata">

**Author:** ![GERSON\_EZEQUIEL\_HERN](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gerson_ezequiel_hern/32/7437_2.png) [@GERSON\_EZEQUIEL\_HERN](https://discourse.julialang.org/u/GERSON_EZEQUIEL_HERN)\
**Post date:** [March 22, 2019, 12:28am UTC](https://discourse.julialang.org/t/using-clipping-on-an-image/21782/12 "2019-03-22T00:28:00Z")

</div>

todo los demas si me dalio bien pero hasta aqui estoy atorado… me imagino

que im = float64(img2) se realiza de otra forma… por que asi me sale el error

julia\> im = float64(img2)  
ERROR: UndefVarError: img2 not defined  
Stacktrace:  
[1] top-level scope at none:0

---

<div class="post-metadata">

**Author:** ![stillyslalom](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stillyslalom/32/45687_2.png) [@stillyslalom](https://discourse.julialang.org/u/stillyslalom)\
**Post date:** [March 22, 2019, 1:21am UTC](https://discourse.julialang.org/t/using-clipping-on-an-image/21782/13 "2019-03-22T01:21:57Z")

</div>

The error message is clear: you didn’t define `img2` before using it. Programming is dependent on context, and it’s hard to help you if we don’t know the exact sequence of commands you’re using. Please try to produce a minimal example: [How to create a Minimal, Reproducible Example - Help Center - Stack Overflow](https://stackoverflow.com/help/mcve)
