如何在matplotlib python中的白色背景上以不同的随机颜色显示对象? [英] How to display objects in different random colors on a white background in matplotlib python?
问题描述
我有一个图像,其中我有标有数字的对象,例如属于对象 1 的所有像素的值都是 1,依此类推.图像的其余部分为零.
I have an image where i have objects labeled with numbers e.g all the pixels belong to object 1 has the value 1 and so on. Rest of the image is zero.
我想用白色背景看到每个具有不同随机颜色的对象.
I want to see every object in different random colors with white background.
我尝试了几种颜色贴图,例如灰色、喷射等,但没有一个符合要求,因为它们按顺序将对象从暗到亮着色.
I have tried several color maps like gray,jet etc but none of them meet the requirement because they sequentially color the objects from dark to light.
非常感谢.
推荐答案
使用随机颜色制作自己的颜色图是解决此问题的快速方法:
Make your own colormap with random colors is a quick way to solve this problem:
colors = [(1,1,1)] + [(random(),random(),random()) for i in xrange(255)]
new_map = matplotlib.colors.LinearSegmentedColormap.from_list('new_map', colors, N=256)
第一种颜色是白色,为您提供白色背景.
First color is white, to give you the white background.
完整的代码在起作用:
import scipy
from scipy import ndimage
import matplotlib.pyplot as plt
import matplotlib
from random import random
colors = [(1,1,1)] + [(random(),random(),random()) for i in xrange(255)]
new_map = matplotlib.colors.LinearSegmentedColormap.from_list('new_map', colors, N=256)
im = scipy.misc.imread('blobs.jpg',flatten=1)
blobs, number_of_blobs = ndimage.label(im)
plt.imshow(blobs, cmap=new_map)
plt.imsave('jj2.png',blobs, cmap=new_map)
plt.show()
带有标签的,随机彩色的输出:
Sample labelled, randomly colored output:
希望这对你来说是随机的!
Hope thats random enuff for ya!
这篇关于如何在matplotlib python中的白色背景上以不同的随机颜色显示对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!