# How do i normalize an image better?

**URL:** <https://forum.opencv.org/t/how-do-i-normalize-an-image-better/1563>\
**Category:** Python\
**Tags:** core\
**Created:** [February 15, 2021, 11:16am UTC](https://forum.opencv.org/t/how-do-i-normalize-an-image-better/1563 "2021-02-15T11:16:51Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Qilos](https://avatars.discourse-cdn.com/v4/letter/q/e8c25b/32.png) [@Qilos](https://forum.opencv.org/u/Qilos)\
**Post date:** [February 15, 2021, 11:16am UTC](https://forum.opencv.org/t/how-do-i-normalize-an-image-better/1563/1 "2021-02-15T11:16:51Z")

</div>

Hi,

Im using a python script to recieve images from a depth camera and when i started reading in the values of each pixel i had to normalize it to actually display it with imshow()

So i used this command to normalize my image:  
cv2.normalize(depth\_array, depth\_array, 0, 1, cv2.NORM\_MINMAX)

depth\_array is a 2D numpy array that contains the values for each pixel.

After working some time with it I realised that the closest objects were always the darkest no matter how near they were and it realised that it takes all the read in values and normalises the between the min and max values that were read.

So i want that the images are normalised from the minimum distance of the depth camer to the maximum distance of it, so that 0.2 - 8 gets normalised between 0 and 1.

Is there a different way to normalize the image so that it works like i need to?

Im really sorry for the bad english, im no native speaker.

Thanks a lot in advance 🙂

---

<div class="post-metadata">

**Author:** ![laurent.berger](https://avatars.discourse-cdn.com/v4/letter/l/ec9cab/32.png) [@laurent.berger](https://forum.opencv.org/u/laurent.berger)\
**Post date:** [February 15, 2021, 11:32am UTC](https://forum.opencv.org/t/how-do-i-normalize-an-image-better/1563/2 "2021-02-15T11:32:35Z")

</div>

Hi,  
I think that’s [cv.normalize](https://docs.opencv.org/master/d2/de8/group __core__ array.html#ga87eef7ee3970f86906d69a92cbf064bd) can help you

```
m = np.array([1,2,3,4,5,6,7,8,9])
p = np.zeros(shape=m.shape)
p = cv.normalize(m,p,alpha=255,beta=-5,norm_type=cv.NORM_MINMAX)
print(p)
p = cv.normalize(m,p,alpha=255,beta=-5,norm_type=cv.NORM_MINMAX, dtype=cv.CV_8UC1)
print(p)

```

results are  
[[-5]  
[28]  
[60]  
[92]  
[125]  
[158]  
[190]  
[222]  
[255]]

and

[[0]  
[28]  
[60]  
[92]  
[125]  
[158]  
[190]  
[222]  
[255]]

---

<div class="post-metadata">

**Author:** ![Qilos](https://avatars.discourse-cdn.com/v4/letter/q/e8c25b/32.png) [@Qilos](https://forum.opencv.org/u/Qilos)\
**Post date:** [February 15, 2021, 11:46am UTC](https://forum.opencv.org/t/how-do-i-normalize-an-image-better/1563/3 "2021-02-15T11:46:34Z")

</div>

Thanks a lot and thank you for your help.

This is what ive been looking for.

Do you know a data type which is between 0 and 1?

---

<div class="post-metadata">

**Author:** ![laurent.berger](https://avatars.discourse-cdn.com/v4/letter/l/ec9cab/32.png) [@laurent.berger](https://forum.opencv.org/u/laurent.berger)\
**Post date:** [February 15, 2021, 12:07pm UTC](https://forum.opencv.org/t/how-do-i-normalize-an-image-better/1563/4 "2021-02-15T12:07:26Z")

</div>

There is no data type between 0 and 1 but you can use CV\_32F or CV\_64F for 32 bits or 64 bits floating value. You have to check that all values are in range 0 to 1
