How should I convert a float32 image to an uint8 image?
up vote
0
down vote
favorite
I want to convert a float32
image into uint8
image in Python using the openCV library. I used the following code, but I do not know whether it is correct or not.
Here I
is the float32
image.
J = I*255
J = J.astype(np.uint8)
I really appreciate if can you help me.
python opencv image-processing
add a comment |
up vote
0
down vote
favorite
I want to convert a float32
image into uint8
image in Python using the openCV library. I used the following code, but I do not know whether it is correct or not.
Here I
is the float32
image.
J = I*255
J = J.astype(np.uint8)
I really appreciate if can you help me.
python opencv image-processing
Since no one explicitly said it, so long as your values are between 0 and 1 in your float image, then yes, your code is correct.
– Alexander Reynolds
Nov 10 at 5:30
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I want to convert a float32
image into uint8
image in Python using the openCV library. I used the following code, but I do not know whether it is correct or not.
Here I
is the float32
image.
J = I*255
J = J.astype(np.uint8)
I really appreciate if can you help me.
python opencv image-processing
I want to convert a float32
image into uint8
image in Python using the openCV library. I used the following code, but I do not know whether it is correct or not.
Here I
is the float32
image.
J = I*255
J = J.astype(np.uint8)
I really appreciate if can you help me.
python opencv image-processing
python opencv image-processing
edited Nov 10 at 7:54
Greenonline
97121424
97121424
asked Nov 10 at 2:56
Hamidreza
145
145
Since no one explicitly said it, so long as your values are between 0 and 1 in your float image, then yes, your code is correct.
– Alexander Reynolds
Nov 10 at 5:30
add a comment |
Since no one explicitly said it, so long as your values are between 0 and 1 in your float image, then yes, your code is correct.
– Alexander Reynolds
Nov 10 at 5:30
Since no one explicitly said it, so long as your values are between 0 and 1 in your float image, then yes, your code is correct.
– Alexander Reynolds
Nov 10 at 5:30
Since no one explicitly said it, so long as your values are between 0 and 1 in your float image, then yes, your code is correct.
– Alexander Reynolds
Nov 10 at 5:30
add a comment |
1 Answer
1
active
oldest
votes
up vote
0
down vote
If you to convert your image from float32 to uint8 numpy
and opencv
in python offers two convenient approaches.
If you know that your image have a range between 0 and 255 or between 0 and 1 then you can simply make the convertion the way you already do:
I *= 255 # or any coefficient
I = I.astype(np.uint8)
If you don't know the range I suggest you to apply a min max normalization
i.e. : (value - min) / (max - min)
opencv have that already implemented:
I = cv2.normalize(I,None,0,255,cv2.NORM_MINMAX,cv2.CV_8U)
The returned I will already have the type np.uint8
and a range between 0 and 255
Using numpy
you can also write something similar:
def norm8(I):
mn = I.min()
mx = I.max()
mx -= mn
I = (I - mn)/mx
return I.astype(np.uint8)
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
If you to convert your image from float32 to uint8 numpy
and opencv
in python offers two convenient approaches.
If you know that your image have a range between 0 and 255 or between 0 and 1 then you can simply make the convertion the way you already do:
I *= 255 # or any coefficient
I = I.astype(np.uint8)
If you don't know the range I suggest you to apply a min max normalization
i.e. : (value - min) / (max - min)
opencv have that already implemented:
I = cv2.normalize(I,None,0,255,cv2.NORM_MINMAX,cv2.CV_8U)
The returned I will already have the type np.uint8
and a range between 0 and 255
Using numpy
you can also write something similar:
def norm8(I):
mn = I.min()
mx = I.max()
mx -= mn
I = (I - mn)/mx
return I.astype(np.uint8)
add a comment |
up vote
0
down vote
If you to convert your image from float32 to uint8 numpy
and opencv
in python offers two convenient approaches.
If you know that your image have a range between 0 and 255 or between 0 and 1 then you can simply make the convertion the way you already do:
I *= 255 # or any coefficient
I = I.astype(np.uint8)
If you don't know the range I suggest you to apply a min max normalization
i.e. : (value - min) / (max - min)
opencv have that already implemented:
I = cv2.normalize(I,None,0,255,cv2.NORM_MINMAX,cv2.CV_8U)
The returned I will already have the type np.uint8
and a range between 0 and 255
Using numpy
you can also write something similar:
def norm8(I):
mn = I.min()
mx = I.max()
mx -= mn
I = (I - mn)/mx
return I.astype(np.uint8)
add a comment |
up vote
0
down vote
up vote
0
down vote
If you to convert your image from float32 to uint8 numpy
and opencv
in python offers two convenient approaches.
If you know that your image have a range between 0 and 255 or between 0 and 1 then you can simply make the convertion the way you already do:
I *= 255 # or any coefficient
I = I.astype(np.uint8)
If you don't know the range I suggest you to apply a min max normalization
i.e. : (value - min) / (max - min)
opencv have that already implemented:
I = cv2.normalize(I,None,0,255,cv2.NORM_MINMAX,cv2.CV_8U)
The returned I will already have the type np.uint8
and a range between 0 and 255
Using numpy
you can also write something similar:
def norm8(I):
mn = I.min()
mx = I.max()
mx -= mn
I = (I - mn)/mx
return I.astype(np.uint8)
If you to convert your image from float32 to uint8 numpy
and opencv
in python offers two convenient approaches.
If you know that your image have a range between 0 and 255 or between 0 and 1 then you can simply make the convertion the way you already do:
I *= 255 # or any coefficient
I = I.astype(np.uint8)
If you don't know the range I suggest you to apply a min max normalization
i.e. : (value - min) / (max - min)
opencv have that already implemented:
I = cv2.normalize(I,None,0,255,cv2.NORM_MINMAX,cv2.CV_8U)
The returned I will already have the type np.uint8
and a range between 0 and 255
Using numpy
you can also write something similar:
def norm8(I):
mn = I.min()
mx = I.max()
mx -= mn
I = (I - mn)/mx
return I.astype(np.uint8)
answered Nov 10 at 5:19
John_Sharp1318
399311
399311
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53235638%2fhow-should-i-convert-a-float32-image-to-an-uint8-image%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Since no one explicitly said it, so long as your values are between 0 and 1 in your float image, then yes, your code is correct.
– Alexander Reynolds
Nov 10 at 5:30