CV2 not displaying colors when converting to uint8





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







3















I have a python list that contains the RGBA data of a PNG image represented as int32, which was sent over from a Java socket server.



A sample output of this data as a numpy array without any conversions:



[[-12763847 -12763847 -12763847 ...  -5590160 -12039396 -12434915]
[-12763847 -12763847 -12763847 ... -6643102 -12828909 -12830184]
[-12763847 -12763847 -12763847 ... -8419763 -13487094 -12435167]]


CV2, of course, accepts uint8 as the data type (displaying as int32 shows a black screen), so I convert my list to a numpy array with dtype = np.uint8 with the following:



image_data = np.array(data_list, dtype = np.uint8)


A sample output of this:



[[ 57  57  57 ... 112  28  29]
[ 57 57 57 ... 98 19 24]
[ 57 57 57 ... 77 10 33]]


However, when I display the image using



cv2.imshow("Image", image_data)


I get a window showing the image, but in grayscale; it lacks colors.



How do I prevent the colors from being ignored?










share|improve this question























  • Look carefully at the integers. The of the bytes will contain the color info. Getting rid of two of them effectively gives you only the blue channel.

    – Mad Physicist
    Nov 17 '18 at 4:43


















3















I have a python list that contains the RGBA data of a PNG image represented as int32, which was sent over from a Java socket server.



A sample output of this data as a numpy array without any conversions:



[[-12763847 -12763847 -12763847 ...  -5590160 -12039396 -12434915]
[-12763847 -12763847 -12763847 ... -6643102 -12828909 -12830184]
[-12763847 -12763847 -12763847 ... -8419763 -13487094 -12435167]]


CV2, of course, accepts uint8 as the data type (displaying as int32 shows a black screen), so I convert my list to a numpy array with dtype = np.uint8 with the following:



image_data = np.array(data_list, dtype = np.uint8)


A sample output of this:



[[ 57  57  57 ... 112  28  29]
[ 57 57 57 ... 98 19 24]
[ 57 57 57 ... 77 10 33]]


However, when I display the image using



cv2.imshow("Image", image_data)


I get a window showing the image, but in grayscale; it lacks colors.



How do I prevent the colors from being ignored?










share|improve this question























  • Look carefully at the integers. The of the bytes will contain the color info. Getting rid of two of them effectively gives you only the blue channel.

    – Mad Physicist
    Nov 17 '18 at 4:43














3












3








3








I have a python list that contains the RGBA data of a PNG image represented as int32, which was sent over from a Java socket server.



A sample output of this data as a numpy array without any conversions:



[[-12763847 -12763847 -12763847 ...  -5590160 -12039396 -12434915]
[-12763847 -12763847 -12763847 ... -6643102 -12828909 -12830184]
[-12763847 -12763847 -12763847 ... -8419763 -13487094 -12435167]]


CV2, of course, accepts uint8 as the data type (displaying as int32 shows a black screen), so I convert my list to a numpy array with dtype = np.uint8 with the following:



image_data = np.array(data_list, dtype = np.uint8)


A sample output of this:



[[ 57  57  57 ... 112  28  29]
[ 57 57 57 ... 98 19 24]
[ 57 57 57 ... 77 10 33]]


However, when I display the image using



cv2.imshow("Image", image_data)


I get a window showing the image, but in grayscale; it lacks colors.



How do I prevent the colors from being ignored?










share|improve this question














I have a python list that contains the RGBA data of a PNG image represented as int32, which was sent over from a Java socket server.



A sample output of this data as a numpy array without any conversions:



[[-12763847 -12763847 -12763847 ...  -5590160 -12039396 -12434915]
[-12763847 -12763847 -12763847 ... -6643102 -12828909 -12830184]
[-12763847 -12763847 -12763847 ... -8419763 -13487094 -12435167]]


CV2, of course, accepts uint8 as the data type (displaying as int32 shows a black screen), so I convert my list to a numpy array with dtype = np.uint8 with the following:



image_data = np.array(data_list, dtype = np.uint8)


A sample output of this:



[[ 57  57  57 ... 112  28  29]
[ 57 57 57 ... 98 19 24]
[ 57 57 57 ... 77 10 33]]


However, when I display the image using



cv2.imshow("Image", image_data)


I get a window showing the image, but in grayscale; it lacks colors.



How do I prevent the colors from being ignored?







python numpy cv2 rgba






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 17 '18 at 4:28









HackNodeHackNode

828




828













  • Look carefully at the integers. The of the bytes will contain the color info. Getting rid of two of them effectively gives you only the blue channel.

    – Mad Physicist
    Nov 17 '18 at 4:43



















  • Look carefully at the integers. The of the bytes will contain the color info. Getting rid of two of them effectively gives you only the blue channel.

    – Mad Physicist
    Nov 17 '18 at 4:43

















Look carefully at the integers. The of the bytes will contain the color info. Getting rid of two of them effectively gives you only the blue channel.

– Mad Physicist
Nov 17 '18 at 4:43





Look carefully at the integers. The of the bytes will contain the color info. Getting rid of two of them effectively gives you only the blue channel.

– Mad Physicist
Nov 17 '18 at 4:43












2 Answers
2






active

oldest

votes


















2














Hopefully this will help you see how to extract the Red, Green and Blue channels from the 32-bit values. I started off with the first row of your array.



im=np.array([-12763847,-12763847,-12763847,-5590160,-12039396,-12434915],dtype=np.int32)

R = ((im & 0xff).astype(np.uint8)
# array([ 57, 57, 57, 112, 28, 29], dtype=uint8)

G = ((im>>8) & 0xff).astype(np.uint8)
# array([ 61, 61, 61, 179, 75, 66], dtype=uint8)

B = ((im>>16) & 0xff).astype(np.uint8)
# array([ 61, 61, 61, 170, 72, 66], dtype=uint8)


If those values look correct, you should be able to merge them into a colour image with:



img = cv2.merge((B,G,R))


bearing in mind that OpenCV uses BGR channel ordering rather than the more conventional RGB.





The ordering of the bytes in your 32-bit number may be different from what I am guessing above. The easiest way to test is to put a red card in front of your camera, and see what comes through, then a green card, then a blue one. The four channels (R,G,B,A) should be given by the following but which is which may be a matter for experiment:



(im    ) & 0xff
(im>>8 ) & 0xff
(im>>16) & 0xff
(im>>24) & 0xff





share|improve this answer


























  • No worries, I already know where to shift the bits :) Thanks for showing me cv2.merge()!

    – HackNode
    Nov 17 '18 at 18:14











  • Cool - good luck with your project! And remember, questions (and answers) are free, so come back if you get stuck again.

    – Mark Setchell
    Nov 17 '18 at 18:17











  • By the way, you can do the same with PIL/Pillow using im = Image.merge("RGB", (b, g, r)) but I guess you are happy with OpenCV.

    – Mark Setchell
    Nov 17 '18 at 18:32



















2














For outputting color image with cv2 you need to image to be of shape (x,y,3), the last axis being the colors.



As your data is int32 representation of RGBA, I assume each number actually hold 4 different numbers, where the first 2 bytes are the uint8 of red, the second green, third blue and last alpha: (0000 0001, 0000 0010, 0000 0100,0000 1000) in this example r is 1, g is 2, b is 4 and alpha is 8.

You will need to extract this 8 bytes into the separate colors.


In addition, keep in mind that cv2 color space is BGR and not RGB, so you will have to switch channels after converting.






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',
    autoActivateHeartbeat: false,
    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%2f53348215%2fcv2-not-displaying-colors-when-converting-to-uint8%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    2














    Hopefully this will help you see how to extract the Red, Green and Blue channels from the 32-bit values. I started off with the first row of your array.



    im=np.array([-12763847,-12763847,-12763847,-5590160,-12039396,-12434915],dtype=np.int32)

    R = ((im & 0xff).astype(np.uint8)
    # array([ 57, 57, 57, 112, 28, 29], dtype=uint8)

    G = ((im>>8) & 0xff).astype(np.uint8)
    # array([ 61, 61, 61, 179, 75, 66], dtype=uint8)

    B = ((im>>16) & 0xff).astype(np.uint8)
    # array([ 61, 61, 61, 170, 72, 66], dtype=uint8)


    If those values look correct, you should be able to merge them into a colour image with:



    img = cv2.merge((B,G,R))


    bearing in mind that OpenCV uses BGR channel ordering rather than the more conventional RGB.





    The ordering of the bytes in your 32-bit number may be different from what I am guessing above. The easiest way to test is to put a red card in front of your camera, and see what comes through, then a green card, then a blue one. The four channels (R,G,B,A) should be given by the following but which is which may be a matter for experiment:



    (im    ) & 0xff
    (im>>8 ) & 0xff
    (im>>16) & 0xff
    (im>>24) & 0xff





    share|improve this answer


























    • No worries, I already know where to shift the bits :) Thanks for showing me cv2.merge()!

      – HackNode
      Nov 17 '18 at 18:14











    • Cool - good luck with your project! And remember, questions (and answers) are free, so come back if you get stuck again.

      – Mark Setchell
      Nov 17 '18 at 18:17











    • By the way, you can do the same with PIL/Pillow using im = Image.merge("RGB", (b, g, r)) but I guess you are happy with OpenCV.

      – Mark Setchell
      Nov 17 '18 at 18:32
















    2














    Hopefully this will help you see how to extract the Red, Green and Blue channels from the 32-bit values. I started off with the first row of your array.



    im=np.array([-12763847,-12763847,-12763847,-5590160,-12039396,-12434915],dtype=np.int32)

    R = ((im & 0xff).astype(np.uint8)
    # array([ 57, 57, 57, 112, 28, 29], dtype=uint8)

    G = ((im>>8) & 0xff).astype(np.uint8)
    # array([ 61, 61, 61, 179, 75, 66], dtype=uint8)

    B = ((im>>16) & 0xff).astype(np.uint8)
    # array([ 61, 61, 61, 170, 72, 66], dtype=uint8)


    If those values look correct, you should be able to merge them into a colour image with:



    img = cv2.merge((B,G,R))


    bearing in mind that OpenCV uses BGR channel ordering rather than the more conventional RGB.





    The ordering of the bytes in your 32-bit number may be different from what I am guessing above. The easiest way to test is to put a red card in front of your camera, and see what comes through, then a green card, then a blue one. The four channels (R,G,B,A) should be given by the following but which is which may be a matter for experiment:



    (im    ) & 0xff
    (im>>8 ) & 0xff
    (im>>16) & 0xff
    (im>>24) & 0xff





    share|improve this answer


























    • No worries, I already know where to shift the bits :) Thanks for showing me cv2.merge()!

      – HackNode
      Nov 17 '18 at 18:14











    • Cool - good luck with your project! And remember, questions (and answers) are free, so come back if you get stuck again.

      – Mark Setchell
      Nov 17 '18 at 18:17











    • By the way, you can do the same with PIL/Pillow using im = Image.merge("RGB", (b, g, r)) but I guess you are happy with OpenCV.

      – Mark Setchell
      Nov 17 '18 at 18:32














    2












    2








    2







    Hopefully this will help you see how to extract the Red, Green and Blue channels from the 32-bit values. I started off with the first row of your array.



    im=np.array([-12763847,-12763847,-12763847,-5590160,-12039396,-12434915],dtype=np.int32)

    R = ((im & 0xff).astype(np.uint8)
    # array([ 57, 57, 57, 112, 28, 29], dtype=uint8)

    G = ((im>>8) & 0xff).astype(np.uint8)
    # array([ 61, 61, 61, 179, 75, 66], dtype=uint8)

    B = ((im>>16) & 0xff).astype(np.uint8)
    # array([ 61, 61, 61, 170, 72, 66], dtype=uint8)


    If those values look correct, you should be able to merge them into a colour image with:



    img = cv2.merge((B,G,R))


    bearing in mind that OpenCV uses BGR channel ordering rather than the more conventional RGB.





    The ordering of the bytes in your 32-bit number may be different from what I am guessing above. The easiest way to test is to put a red card in front of your camera, and see what comes through, then a green card, then a blue one. The four channels (R,G,B,A) should be given by the following but which is which may be a matter for experiment:



    (im    ) & 0xff
    (im>>8 ) & 0xff
    (im>>16) & 0xff
    (im>>24) & 0xff





    share|improve this answer















    Hopefully this will help you see how to extract the Red, Green and Blue channels from the 32-bit values. I started off with the first row of your array.



    im=np.array([-12763847,-12763847,-12763847,-5590160,-12039396,-12434915],dtype=np.int32)

    R = ((im & 0xff).astype(np.uint8)
    # array([ 57, 57, 57, 112, 28, 29], dtype=uint8)

    G = ((im>>8) & 0xff).astype(np.uint8)
    # array([ 61, 61, 61, 179, 75, 66], dtype=uint8)

    B = ((im>>16) & 0xff).astype(np.uint8)
    # array([ 61, 61, 61, 170, 72, 66], dtype=uint8)


    If those values look correct, you should be able to merge them into a colour image with:



    img = cv2.merge((B,G,R))


    bearing in mind that OpenCV uses BGR channel ordering rather than the more conventional RGB.





    The ordering of the bytes in your 32-bit number may be different from what I am guessing above. The easiest way to test is to put a red card in front of your camera, and see what comes through, then a green card, then a blue one. The four channels (R,G,B,A) should be given by the following but which is which may be a matter for experiment:



    (im    ) & 0xff
    (im>>8 ) & 0xff
    (im>>16) & 0xff
    (im>>24) & 0xff






    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Nov 17 '18 at 18:18

























    answered Nov 17 '18 at 12:27









    Mark SetchellMark Setchell

    93.6k785192




    93.6k785192













    • No worries, I already know where to shift the bits :) Thanks for showing me cv2.merge()!

      – HackNode
      Nov 17 '18 at 18:14











    • Cool - good luck with your project! And remember, questions (and answers) are free, so come back if you get stuck again.

      – Mark Setchell
      Nov 17 '18 at 18:17











    • By the way, you can do the same with PIL/Pillow using im = Image.merge("RGB", (b, g, r)) but I guess you are happy with OpenCV.

      – Mark Setchell
      Nov 17 '18 at 18:32



















    • No worries, I already know where to shift the bits :) Thanks for showing me cv2.merge()!

      – HackNode
      Nov 17 '18 at 18:14











    • Cool - good luck with your project! And remember, questions (and answers) are free, so come back if you get stuck again.

      – Mark Setchell
      Nov 17 '18 at 18:17











    • By the way, you can do the same with PIL/Pillow using im = Image.merge("RGB", (b, g, r)) but I guess you are happy with OpenCV.

      – Mark Setchell
      Nov 17 '18 at 18:32

















    No worries, I already know where to shift the bits :) Thanks for showing me cv2.merge()!

    – HackNode
    Nov 17 '18 at 18:14





    No worries, I already know where to shift the bits :) Thanks for showing me cv2.merge()!

    – HackNode
    Nov 17 '18 at 18:14













    Cool - good luck with your project! And remember, questions (and answers) are free, so come back if you get stuck again.

    – Mark Setchell
    Nov 17 '18 at 18:17





    Cool - good luck with your project! And remember, questions (and answers) are free, so come back if you get stuck again.

    – Mark Setchell
    Nov 17 '18 at 18:17













    By the way, you can do the same with PIL/Pillow using im = Image.merge("RGB", (b, g, r)) but I guess you are happy with OpenCV.

    – Mark Setchell
    Nov 17 '18 at 18:32





    By the way, you can do the same with PIL/Pillow using im = Image.merge("RGB", (b, g, r)) but I guess you are happy with OpenCV.

    – Mark Setchell
    Nov 17 '18 at 18:32













    2














    For outputting color image with cv2 you need to image to be of shape (x,y,3), the last axis being the colors.



    As your data is int32 representation of RGBA, I assume each number actually hold 4 different numbers, where the first 2 bytes are the uint8 of red, the second green, third blue and last alpha: (0000 0001, 0000 0010, 0000 0100,0000 1000) in this example r is 1, g is 2, b is 4 and alpha is 8.

    You will need to extract this 8 bytes into the separate colors.


    In addition, keep in mind that cv2 color space is BGR and not RGB, so you will have to switch channels after converting.






    share|improve this answer




























      2














      For outputting color image with cv2 you need to image to be of shape (x,y,3), the last axis being the colors.



      As your data is int32 representation of RGBA, I assume each number actually hold 4 different numbers, where the first 2 bytes are the uint8 of red, the second green, third blue and last alpha: (0000 0001, 0000 0010, 0000 0100,0000 1000) in this example r is 1, g is 2, b is 4 and alpha is 8.

      You will need to extract this 8 bytes into the separate colors.


      In addition, keep in mind that cv2 color space is BGR and not RGB, so you will have to switch channels after converting.






      share|improve this answer


























        2












        2








        2







        For outputting color image with cv2 you need to image to be of shape (x,y,3), the last axis being the colors.



        As your data is int32 representation of RGBA, I assume each number actually hold 4 different numbers, where the first 2 bytes are the uint8 of red, the second green, third blue and last alpha: (0000 0001, 0000 0010, 0000 0100,0000 1000) in this example r is 1, g is 2, b is 4 and alpha is 8.

        You will need to extract this 8 bytes into the separate colors.


        In addition, keep in mind that cv2 color space is BGR and not RGB, so you will have to switch channels after converting.






        share|improve this answer













        For outputting color image with cv2 you need to image to be of shape (x,y,3), the last axis being the colors.



        As your data is int32 representation of RGBA, I assume each number actually hold 4 different numbers, where the first 2 bytes are the uint8 of red, the second green, third blue and last alpha: (0000 0001, 0000 0010, 0000 0100,0000 1000) in this example r is 1, g is 2, b is 4 and alpha is 8.

        You will need to extract this 8 bytes into the separate colors.


        In addition, keep in mind that cv2 color space is BGR and not RGB, so you will have to switch channels after converting.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 17 '18 at 8:58









        DinariDinari

        1,709623




        1,709623






























            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.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53348215%2fcv2-not-displaying-colors-when-converting-to-uint8%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

            List item for chat from Array inside array React Native

            Thiostrepton

            Caerphilly