Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Conversion of image type int16 to uint8

I have an image with data type int16. So when I have to convert its range to 0-255, I got two ways to do that in Python.

1) Use numpy.uint8 function directly

2) Use OpenCV cv2.normalize function with 0-255 range and then use numpy.uint8.

In Matlab, we directly get the conversion using uint8 function. In

Also in second case, I used NORM_MINMAX and the range of intensity values gets changed to 0-4.

What is the correct way to do the conversion?

like image 552
Gunjan naik Avatar asked Oct 21 '17 18:10

Gunjan naik


People also ask

How do I change my image to uint8?

J = im2uint8( I ) converts the grayscale, RGB, or binary image I to uint8 , rescaling or offsetting the data as necessary. If the input image is of class uint8 , then the output image is identical.


2 Answers

All of these do different things.

np.uint8 considers only the lowest byte of your number. It's like doing value & 0xff.

>>> img = np.array([2000, -150, 11], dtype=np.int16)
>>> np.uint8(img)
array([208, 106,  11], dtype=uint8)

cv2.normalize with the cv2.NORM_MINMAX norm type normalises your values according to the normalisation function

img_new = (img - img.min()) * ((max_new - min_new) / (img.max() - img.min())) + min_new

It effectively changes one range to another and all the values in the between are scaled accordingly. By definition the original min/max values become the targetted min/max values.

>>> cv2.normalize(img, out, 0, 255, cv2.NORM_MINMAX)
array([255,   0,  19], dtype=int16)

uint8 in Matlab simply saturates your values. Everything above 255 becomes 255 and everything below 0 becomes 0.

>> uint8([2000 -150 11])

ans =

  255    0   11

If you want to replicate Matlab's functionality, you can do

>>> img[img > 255] = 255
>>> img[img < 0] = 0

Which one you want to use depends on what you're trying to do. If your int16 covers the range of your pixel values and you want to rescale those to uint8, then cv2.normalize is the answer.

like image 151
Reti43 Avatar answered Sep 28 '22 00:09

Reti43


the simply way to convert data format is to use the following formula. In this case it is possible to convert any array to a new custom format.

# In the case of image, or matrix/array conversion to the uint8 [0, 255] 
# range

Import numpy as np

new_data = (newd_ata - np.min(new_data)) * ((255 - 0) / (np.max(new_data) - 
np.new_data))) + 0
like image 22
vittorio Avatar answered Sep 28 '22 00:09

vittorio