I have an image with data type int16。
sowhenihavetoconvertitsrangeto0- 255,I got two ways to do that in Python。
1 ) use numpy.uint 8功能目录
2 ) useopencvcv2. tsdlqalizefunctionwith0- 255 rangeandthenusenumpy.uint 8。
In Matlab,wedirectlygettheconversionusinguint 8功能区。
In
Also in second case,iused norm _ minmaxandtherangeofintensityvaluesgetschangedto0- 4。
watisthecorrectwaytodotheconversion?
All of these do different things。
NP.uint8considersonlythelowestbyteofyournumber.it ' slikedoingvalue0x ff。
img=NP.Array([2000,- 150,11 ],dtype=np.int16 ) )。
是NP.uint8(img )
阵列([ 208,106,11 ],dtype=uint8) )。
cv2.tsdlqalizewiththecv2. norm _ minmaxtsdlqtypetsdlqalisesyourvaluesaccordingtothetsdlqalisationfunction
img_new=(img- img.min () ) jkdcc ) _new-min_new )/(img.max )-img.min () ) ) min_new
iteffectivelychangesonerangetoanotherandallthevaluesinthebetweenarescaledaccordingly.bydefinitiontheoriginalmin/maxvaluesbbbetion
cv2.tsdlqalize(img,out,0,255,cv2.NORM_MINMAX ) )。
array ([ 255,0,19 ],dtype=int16 )。
uint8inmatlabsimplysaturatesyourvalues.everything above 255 becomes 255 andeverythingbelow0becomes 0。
uint8([2000-15011]
255 0 11
ifyouwanttoreplicatematlab ' sfunctionality,you can do
img[img 255]=255
img[img 0]=0
whichoneyouwanttousedependsonwhatyou ' retrying todo.if your int 16 coverstherangeofyourpixelvaluesandyouwanttorescalethosetouint