Using Clipping on an image

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.

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

i suppose you have a code like this:

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:

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:

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 :grin:.

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!

1 Like

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;

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

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

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

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

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

1 Like


me sale este error en consola

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

Capitalization matters: TestImage is different from testimage.

using TestImages
img = testimage("mandrill")
1 Like

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

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

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

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
[1] top-level scope at none:0

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