如何为图像中的每个对象着色不同的颜色 [英] How to color objects in an image with different color each

查看:176
本文介绍了如何为图像中的每个对象着色不同的颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到以下问题:我有黑白图像,必须对其进行着色:图像中的每个白色斑点"都代表一个对象的实例,我想用一种新颜色为每个对象着色,但是对于每个图像,我都希望使用相同的颜色方案:

例如:

  • 第一个图像:3个对象->使用的颜色:红色,绿色,黄色
  • 第二张图片:2个对象->使用的颜色:红色,绿色
  • 第三张图片:5个对象->使用的颜色:红色,绿色,黄色,粉红色,橙色

我已经手工着色了几幅图像以显示结果应该是什么样的:

必须着色的黑白面具

2个对象,2种颜色:绿色,黄色

4个对象,4种颜色:绿色,黄色,红色,浅灰色

要自动执行此操作,我尝试了

另一个带有2个对象的图像,分别是橙色和粉红色

具有3个对象的图像,现在颜色更改为:橙色,黄色和绿色(我需要:橙色,粉红色和新颜色

解决方案

由于您正在生成随机颜色图,因此颜色不一致也就不足为奇了.

您想使用自己选择的颜色创建一个颜色图,并将该颜色图传递给每个图像,而不考虑存在的斑点数.但是请注意,默认情况下,颜色图会根据您的数据范围进行标准化.由于数据范围根据找到的斑点数而变化,因此您需要使用 vmin = vmax 显式设置标准化.这是使用4张不同图片的演示:

 导入imageio从scipy导入ndimage颜色= [黑色",红色",绿色",黄色",粉红色",橙色"]vmin = 0vmax = len(颜色)cmap = matplotlib.colors.ListedColormap(colors)无花果,斧头= plt.subplots(4,1,图大小=(3,3 * 4))对于文件,zip中的ax(['test1.png','test2.png','test3.png','test4.png'],axs):im = imageio.imread(文件)Blob,number_of_blobs = ndimage.label(im)ax.imshow(斑点,cmap = cmap,vmin = vmin,vmax = vmax) 

I'm having following problem: I have black/white images, which I have to colorize: Every white "blob" in the image represents an instance of an object and I want to color every object with a new color, but for every image i want to use the same color scheme:

For example:

  • 1st image: 3 Objects -> used colors: red, green, yellow
  • 2nd image: 2 Objects -> used colors: red, green
  • 3rd image: 5 objects -> used colors: red, green, yellow, pink, orange

I've colored a couple of images by hand to show what the result should look like:

Black / white mask that has to be colorized

2 objects, 2 colors: green, yellow

4 objects, 4 colors: green, yellow, red, light grey

To do it automatically i've tried the approach here:

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()

Problem with that is, that if I run in on my images, the objects get colorized differently depending on how many objects there are in each image:

For example:

  • 1st image: 3 Objects -> used colors: red, green, yellow

  • 2nd image: 2 Objects -> used colors: orange, yellow

  • 3rd image: 5 objects -> used colors: red, orange, green, limegreen, yellow

  • 4th image: 3 objects -> used colors: red, green, yellow

Here are some pictures to visualize the incorrect coloring of the 3rd image:

2 objects, coloured orange and pink

Another image with 2 objects, coloured orange and pink

Image with 3 objects, now colors change: orange, yellow and green (what I need: orange, pink and new color

解决方案

Since you are generating a random colormap, it's no surprise the colors are not consistent.

You want to create one colormap with your colors of choice, and pass that colormap to each image, regardless of the number of blobs present. Note however, that by default the colormaps are normalized to the range of your data. Since the range of the data changes depending on the number of blobs found, you need to explicitly set the normalization using vmin= and vmax. Here is a demonstration using 4 different images:

import imageio
from scipy import ndimage

colors = ['black','red', 'green', 'yellow', 'pink', 'orange']
vmin = 0
vmax = len(colors)
cmap = matplotlib.colors.ListedColormap(colors)


fig, axs = plt.subplots(4,1, figsize=(3,3*4))
for file,ax in zip(['test1.png','test2.png','test3.png','test4.png'], axs):
    im = imageio.imread(file)
    blobs, number_of_blobs = ndimage.label(im)
    ax.imshow(blobs, cmap=cmap, vmin=vmin, vmax=vmax)

这篇关于如何为图像中的每个对象着色不同的颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆