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.










share|improve this question























  • 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














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.










share|improve this question























  • 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












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.










share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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
















  • 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












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)





share|improve this answer




















    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    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

























    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)





    share|improve this answer
























      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)





      share|improve this answer






















        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)





        share|improve this answer












        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)






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 10 at 5:19









        John_Sharp1318

        399311




        399311



























            draft saved

            draft discarded
















































            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.




            draft saved


            draft discarded














            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





















































            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







            Popular posts from this blog

            Use pre created SQLite database for Android project in kotlin

            Darth Vader #20

            Ondo