Visualize values in numpy array as colors in a bitmap












1















I have been given a segmentation mask: a HxW numpy array in which each location represents a pixel. The value at each location is not RGB, however; it is an arbitrary integer in the range 0-60, where the integer is simply a code representing a type of image content.



E.g. array locations whose value is 12 indicate pixels categorized as 'table'; locations whose value is 34 indicate pixels categorized as 'carpet'.



I want to load this segmentation mask into a canvas using tkinter, so I need to make an image out of it, and I need each of the values 0-60 to correspond to colors whose hues are as distinct as can be.



How do I choose these colors? How do I convert my HxW array into an RGB image?










share|improve this question























  • you shouldn't need to convert to rgb. simply visualise your array via pcolor using a custom colormap

    – Tasos Papastylianou
    Nov 27 '18 at 22:01


















1















I have been given a segmentation mask: a HxW numpy array in which each location represents a pixel. The value at each location is not RGB, however; it is an arbitrary integer in the range 0-60, where the integer is simply a code representing a type of image content.



E.g. array locations whose value is 12 indicate pixels categorized as 'table'; locations whose value is 34 indicate pixels categorized as 'carpet'.



I want to load this segmentation mask into a canvas using tkinter, so I need to make an image out of it, and I need each of the values 0-60 to correspond to colors whose hues are as distinct as can be.



How do I choose these colors? How do I convert my HxW array into an RGB image?










share|improve this question























  • you shouldn't need to convert to rgb. simply visualise your array via pcolor using a custom colormap

    – Tasos Papastylianou
    Nov 27 '18 at 22:01
















1












1








1








I have been given a segmentation mask: a HxW numpy array in which each location represents a pixel. The value at each location is not RGB, however; it is an arbitrary integer in the range 0-60, where the integer is simply a code representing a type of image content.



E.g. array locations whose value is 12 indicate pixels categorized as 'table'; locations whose value is 34 indicate pixels categorized as 'carpet'.



I want to load this segmentation mask into a canvas using tkinter, so I need to make an image out of it, and I need each of the values 0-60 to correspond to colors whose hues are as distinct as can be.



How do I choose these colors? How do I convert my HxW array into an RGB image?










share|improve this question














I have been given a segmentation mask: a HxW numpy array in which each location represents a pixel. The value at each location is not RGB, however; it is an arbitrary integer in the range 0-60, where the integer is simply a code representing a type of image content.



E.g. array locations whose value is 12 indicate pixels categorized as 'table'; locations whose value is 34 indicate pixels categorized as 'carpet'.



I want to load this segmentation mask into a canvas using tkinter, so I need to make an image out of it, and I need each of the values 0-60 to correspond to colors whose hues are as distinct as can be.



How do I choose these colors? How do I convert my HxW array into an RGB image?







python numpy hue






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 27 '18 at 21:27









JellicleCatJellicleCat

14.1k1680121




14.1k1680121













  • you shouldn't need to convert to rgb. simply visualise your array via pcolor using a custom colormap

    – Tasos Papastylianou
    Nov 27 '18 at 22:01





















  • you shouldn't need to convert to rgb. simply visualise your array via pcolor using a custom colormap

    – Tasos Papastylianou
    Nov 27 '18 at 22:01



















you shouldn't need to convert to rgb. simply visualise your array via pcolor using a custom colormap

– Tasos Papastylianou
Nov 27 '18 at 22:01







you shouldn't need to convert to rgb. simply visualise your array via pcolor using a custom colormap

– Tasos Papastylianou
Nov 27 '18 at 22:01














2 Answers
2






active

oldest

votes


















2














You don't need to convert to RGB. Just use pcolor with an appropriate colormap.



It could be something as simple as this:



import matplotlib.pyplot as plt
import numpy

M = numpy.array([[1,2,3,4,5,6,7,8,9,10], [11,12,13,14,15,16,17,18,19,20]])
plt.pcolor( M , cmap = 'hsv' )
plt.show()




Added by Mark Setchell:








share|improve this answer





















  • 2





    It would be helpful to show how that looks and also to explain how to make that into something OP can load into a TkInter canvas. I hope you don't mind my adding the plot - just delete it if you do.

    – Mark Setchell
    Nov 27 '18 at 22:13






  • 1





    @MarkSetchell ah apologies, I missed the tkinter bit, thanks for pointing it out. I haven't done it in a while, but effectively, one can use a matplotlib object in a tkinter gui, e.g. as in this post: stackoverflow.com/q/4073660/4183191 Again, there would be no need to convert to RGB, just use the resulting figure in the tkinter gui.

    – Tasos Papastylianou
    Nov 27 '18 at 22:28






  • 1





    I had to use pcolormesh instead of pcolor because the former was too slow

    – JellicleCat
    Nov 28 '18 at 0:04



















2














IMHO, the obvious thing to do would be to make a palettised image where you have a palette of 60 colours and an index into the palette at each location. That is very simple with PIL/Pillow.



Now you come to choosing those 60 colours. I would think of using HSL (Hue, Saturation and Lightness) colourspace. Now you need 60 different values. So you could use full Saturation and Lightness for all pixels and spread the available 360 degrees of Hue into 6 degree chunks for 60 colours. Or you could use 100% Lightness and 50% Lightness plus 100% Saturation and 50% Saturation to give you 4x as many Lightness/Saturation combinations meaning you could rotate 24 degrees of Hue for greater separation.



See here for example of how to make an image from a Numpy array, add a palette to an image and make into Pillow image and save.



I am basically suggesting a 60-entry palette, with the following values:



Hue, Saturation, Lightness
0 50% 50%
0 50% 100%
0 100% 50%
0 100% 100%
23 50% 50%
23 50% 100%
23 100% 50%
23 100% 100%
47 50% 50%
47 50% 100%
47 100% 50%
47 100% 100%
61 ...





share|improve this answer


























  • I like this method, but I'm having scant luck. I'm trying the toy example Image.fromarray([[0,1,2,3,4]], mode='P') and am getting a single, solid color which matches the first color in my palette.

    – JellicleCat
    Nov 27 '18 at 23:21











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%2f53508413%2fvisualize-values-in-numpy-array-as-colors-in-a-bitmap%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














You don't need to convert to RGB. Just use pcolor with an appropriate colormap.



It could be something as simple as this:



import matplotlib.pyplot as plt
import numpy

M = numpy.array([[1,2,3,4,5,6,7,8,9,10], [11,12,13,14,15,16,17,18,19,20]])
plt.pcolor( M , cmap = 'hsv' )
plt.show()




Added by Mark Setchell:








share|improve this answer





















  • 2





    It would be helpful to show how that looks and also to explain how to make that into something OP can load into a TkInter canvas. I hope you don't mind my adding the plot - just delete it if you do.

    – Mark Setchell
    Nov 27 '18 at 22:13






  • 1





    @MarkSetchell ah apologies, I missed the tkinter bit, thanks for pointing it out. I haven't done it in a while, but effectively, one can use a matplotlib object in a tkinter gui, e.g. as in this post: stackoverflow.com/q/4073660/4183191 Again, there would be no need to convert to RGB, just use the resulting figure in the tkinter gui.

    – Tasos Papastylianou
    Nov 27 '18 at 22:28






  • 1





    I had to use pcolormesh instead of pcolor because the former was too slow

    – JellicleCat
    Nov 28 '18 at 0:04
















2














You don't need to convert to RGB. Just use pcolor with an appropriate colormap.



It could be something as simple as this:



import matplotlib.pyplot as plt
import numpy

M = numpy.array([[1,2,3,4,5,6,7,8,9,10], [11,12,13,14,15,16,17,18,19,20]])
plt.pcolor( M , cmap = 'hsv' )
plt.show()




Added by Mark Setchell:








share|improve this answer





















  • 2





    It would be helpful to show how that looks and also to explain how to make that into something OP can load into a TkInter canvas. I hope you don't mind my adding the plot - just delete it if you do.

    – Mark Setchell
    Nov 27 '18 at 22:13






  • 1





    @MarkSetchell ah apologies, I missed the tkinter bit, thanks for pointing it out. I haven't done it in a while, but effectively, one can use a matplotlib object in a tkinter gui, e.g. as in this post: stackoverflow.com/q/4073660/4183191 Again, there would be no need to convert to RGB, just use the resulting figure in the tkinter gui.

    – Tasos Papastylianou
    Nov 27 '18 at 22:28






  • 1





    I had to use pcolormesh instead of pcolor because the former was too slow

    – JellicleCat
    Nov 28 '18 at 0:04














2












2








2







You don't need to convert to RGB. Just use pcolor with an appropriate colormap.



It could be something as simple as this:



import matplotlib.pyplot as plt
import numpy

M = numpy.array([[1,2,3,4,5,6,7,8,9,10], [11,12,13,14,15,16,17,18,19,20]])
plt.pcolor( M , cmap = 'hsv' )
plt.show()




Added by Mark Setchell:








share|improve this answer















You don't need to convert to RGB. Just use pcolor with an appropriate colormap.



It could be something as simple as this:



import matplotlib.pyplot as plt
import numpy

M = numpy.array([[1,2,3,4,5,6,7,8,9,10], [11,12,13,14,15,16,17,18,19,20]])
plt.pcolor( M , cmap = 'hsv' )
plt.show()




Added by Mark Setchell:









share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 27 '18 at 22:22

























answered Nov 27 '18 at 22:06









Tasos PapastylianouTasos Papastylianou

11.1k11133




11.1k11133








  • 2





    It would be helpful to show how that looks and also to explain how to make that into something OP can load into a TkInter canvas. I hope you don't mind my adding the plot - just delete it if you do.

    – Mark Setchell
    Nov 27 '18 at 22:13






  • 1





    @MarkSetchell ah apologies, I missed the tkinter bit, thanks for pointing it out. I haven't done it in a while, but effectively, one can use a matplotlib object in a tkinter gui, e.g. as in this post: stackoverflow.com/q/4073660/4183191 Again, there would be no need to convert to RGB, just use the resulting figure in the tkinter gui.

    – Tasos Papastylianou
    Nov 27 '18 at 22:28






  • 1





    I had to use pcolormesh instead of pcolor because the former was too slow

    – JellicleCat
    Nov 28 '18 at 0:04














  • 2





    It would be helpful to show how that looks and also to explain how to make that into something OP can load into a TkInter canvas. I hope you don't mind my adding the plot - just delete it if you do.

    – Mark Setchell
    Nov 27 '18 at 22:13






  • 1





    @MarkSetchell ah apologies, I missed the tkinter bit, thanks for pointing it out. I haven't done it in a while, but effectively, one can use a matplotlib object in a tkinter gui, e.g. as in this post: stackoverflow.com/q/4073660/4183191 Again, there would be no need to convert to RGB, just use the resulting figure in the tkinter gui.

    – Tasos Papastylianou
    Nov 27 '18 at 22:28






  • 1





    I had to use pcolormesh instead of pcolor because the former was too slow

    – JellicleCat
    Nov 28 '18 at 0:04








2




2





It would be helpful to show how that looks and also to explain how to make that into something OP can load into a TkInter canvas. I hope you don't mind my adding the plot - just delete it if you do.

– Mark Setchell
Nov 27 '18 at 22:13





It would be helpful to show how that looks and also to explain how to make that into something OP can load into a TkInter canvas. I hope you don't mind my adding the plot - just delete it if you do.

– Mark Setchell
Nov 27 '18 at 22:13




1




1





@MarkSetchell ah apologies, I missed the tkinter bit, thanks for pointing it out. I haven't done it in a while, but effectively, one can use a matplotlib object in a tkinter gui, e.g. as in this post: stackoverflow.com/q/4073660/4183191 Again, there would be no need to convert to RGB, just use the resulting figure in the tkinter gui.

– Tasos Papastylianou
Nov 27 '18 at 22:28





@MarkSetchell ah apologies, I missed the tkinter bit, thanks for pointing it out. I haven't done it in a while, but effectively, one can use a matplotlib object in a tkinter gui, e.g. as in this post: stackoverflow.com/q/4073660/4183191 Again, there would be no need to convert to RGB, just use the resulting figure in the tkinter gui.

– Tasos Papastylianou
Nov 27 '18 at 22:28




1




1





I had to use pcolormesh instead of pcolor because the former was too slow

– JellicleCat
Nov 28 '18 at 0:04





I had to use pcolormesh instead of pcolor because the former was too slow

– JellicleCat
Nov 28 '18 at 0:04













2














IMHO, the obvious thing to do would be to make a palettised image where you have a palette of 60 colours and an index into the palette at each location. That is very simple with PIL/Pillow.



Now you come to choosing those 60 colours. I would think of using HSL (Hue, Saturation and Lightness) colourspace. Now you need 60 different values. So you could use full Saturation and Lightness for all pixels and spread the available 360 degrees of Hue into 6 degree chunks for 60 colours. Or you could use 100% Lightness and 50% Lightness plus 100% Saturation and 50% Saturation to give you 4x as many Lightness/Saturation combinations meaning you could rotate 24 degrees of Hue for greater separation.



See here for example of how to make an image from a Numpy array, add a palette to an image and make into Pillow image and save.



I am basically suggesting a 60-entry palette, with the following values:



Hue, Saturation, Lightness
0 50% 50%
0 50% 100%
0 100% 50%
0 100% 100%
23 50% 50%
23 50% 100%
23 100% 50%
23 100% 100%
47 50% 50%
47 50% 100%
47 100% 50%
47 100% 100%
61 ...





share|improve this answer


























  • I like this method, but I'm having scant luck. I'm trying the toy example Image.fromarray([[0,1,2,3,4]], mode='P') and am getting a single, solid color which matches the first color in my palette.

    – JellicleCat
    Nov 27 '18 at 23:21
















2














IMHO, the obvious thing to do would be to make a palettised image where you have a palette of 60 colours and an index into the palette at each location. That is very simple with PIL/Pillow.



Now you come to choosing those 60 colours. I would think of using HSL (Hue, Saturation and Lightness) colourspace. Now you need 60 different values. So you could use full Saturation and Lightness for all pixels and spread the available 360 degrees of Hue into 6 degree chunks for 60 colours. Or you could use 100% Lightness and 50% Lightness plus 100% Saturation and 50% Saturation to give you 4x as many Lightness/Saturation combinations meaning you could rotate 24 degrees of Hue for greater separation.



See here for example of how to make an image from a Numpy array, add a palette to an image and make into Pillow image and save.



I am basically suggesting a 60-entry palette, with the following values:



Hue, Saturation, Lightness
0 50% 50%
0 50% 100%
0 100% 50%
0 100% 100%
23 50% 50%
23 50% 100%
23 100% 50%
23 100% 100%
47 50% 50%
47 50% 100%
47 100% 50%
47 100% 100%
61 ...





share|improve this answer


























  • I like this method, but I'm having scant luck. I'm trying the toy example Image.fromarray([[0,1,2,3,4]], mode='P') and am getting a single, solid color which matches the first color in my palette.

    – JellicleCat
    Nov 27 '18 at 23:21














2












2








2







IMHO, the obvious thing to do would be to make a palettised image where you have a palette of 60 colours and an index into the palette at each location. That is very simple with PIL/Pillow.



Now you come to choosing those 60 colours. I would think of using HSL (Hue, Saturation and Lightness) colourspace. Now you need 60 different values. So you could use full Saturation and Lightness for all pixels and spread the available 360 degrees of Hue into 6 degree chunks for 60 colours. Or you could use 100% Lightness and 50% Lightness plus 100% Saturation and 50% Saturation to give you 4x as many Lightness/Saturation combinations meaning you could rotate 24 degrees of Hue for greater separation.



See here for example of how to make an image from a Numpy array, add a palette to an image and make into Pillow image and save.



I am basically suggesting a 60-entry palette, with the following values:



Hue, Saturation, Lightness
0 50% 50%
0 50% 100%
0 100% 50%
0 100% 100%
23 50% 50%
23 50% 100%
23 100% 50%
23 100% 100%
47 50% 50%
47 50% 100%
47 100% 50%
47 100% 100%
61 ...





share|improve this answer















IMHO, the obvious thing to do would be to make a palettised image where you have a palette of 60 colours and an index into the palette at each location. That is very simple with PIL/Pillow.



Now you come to choosing those 60 colours. I would think of using HSL (Hue, Saturation and Lightness) colourspace. Now you need 60 different values. So you could use full Saturation and Lightness for all pixels and spread the available 360 degrees of Hue into 6 degree chunks for 60 colours. Or you could use 100% Lightness and 50% Lightness plus 100% Saturation and 50% Saturation to give you 4x as many Lightness/Saturation combinations meaning you could rotate 24 degrees of Hue for greater separation.



See here for example of how to make an image from a Numpy array, add a palette to an image and make into Pillow image and save.



I am basically suggesting a 60-entry palette, with the following values:



Hue, Saturation, Lightness
0 50% 50%
0 50% 100%
0 100% 50%
0 100% 100%
23 50% 50%
23 50% 100%
23 100% 50%
23 100% 100%
47 50% 50%
47 50% 100%
47 100% 50%
47 100% 100%
61 ...






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 27 '18 at 22:26

























answered Nov 27 '18 at 21:55









Mark SetchellMark Setchell

91.2k779183




91.2k779183













  • I like this method, but I'm having scant luck. I'm trying the toy example Image.fromarray([[0,1,2,3,4]], mode='P') and am getting a single, solid color which matches the first color in my palette.

    – JellicleCat
    Nov 27 '18 at 23:21



















  • I like this method, but I'm having scant luck. I'm trying the toy example Image.fromarray([[0,1,2,3,4]], mode='P') and am getting a single, solid color which matches the first color in my palette.

    – JellicleCat
    Nov 27 '18 at 23:21

















I like this method, but I'm having scant luck. I'm trying the toy example Image.fromarray([[0,1,2,3,4]], mode='P') and am getting a single, solid color which matches the first color in my palette.

– JellicleCat
Nov 27 '18 at 23:21





I like this method, but I'm having scant luck. I'm trying the toy example Image.fromarray([[0,1,2,3,4]], mode='P') and am getting a single, solid color which matches the first color in my palette.

– JellicleCat
Nov 27 '18 at 23:21


















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%2f53508413%2fvisualize-values-in-numpy-array-as-colors-in-a-bitmap%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

Contact image not getting when fetch all contact list from iPhone by CNContact

count number of partitions of a set with n elements into k subsets

A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks