2012-02-08 78 views
10

我是图像处理的完全新手,我猜这很容易做,但我不知道术语。基本上我有一个黑白图像,我只是想涂上一个彩色覆盖图像,这样我就得到了覆盖着蓝绿色图像和黄色的图像,如下面显示的图像(实际上我可以'因为我没有足够的声望去做 - grrrrrr)。想象一下,我有一张物理图像,以及一张绿色/红色/蓝色/黄色覆盖图,放置在图像的顶部。在PIL或Imagemagik中对图像应用彩色叠加层

理想情况下,我想使用Python PIL来做这个,但我会同样高兴地使用ImageMagik来做,但是无论哪种方式我都需要能够编写脚本,因为我需要100个左右的图像开展这一进程。

回答

18

下面是一个代码片段,显示如何使用scikit-image覆盖灰度图像上的颜色。我们的想法是将这两个图像转换为HSV色彩空间,然后用灰度图像的色调和饱和度值替换色彩图像的色调和饱和度值。

from skimage import data, color, io, img_as_float 
import numpy as np 
import matplotlib.pyplot as plt 

alpha = 0.6 

img = img_as_float(data.camera()) 
rows, cols = img.shape 

# Construct a colour image to superimpose 
color_mask = np.zeros((rows, cols, 3)) 
color_mask[30:140, 30:140] = [1, 0, 0] # Red block 
color_mask[170:270, 40:120] = [0, 1, 0] # Green block 
color_mask[200:350, 200:350] = [0, 0, 1] # Blue block 

# Construct RGB version of grey-level image 
img_color = np.dstack((img, img, img)) 

# Convert the input image and color mask to Hue Saturation Value (HSV) 
# colorspace 
img_hsv = color.rgb2hsv(img_color) 
color_mask_hsv = color.rgb2hsv(color_mask) 

# Replace the hue and saturation of the original image 
# with that of the color mask 
img_hsv[..., 0] = color_mask_hsv[..., 0] 
img_hsv[..., 1] = color_mask_hsv[..., 1] * alpha 

img_masked = color.hsv2rgb(img_hsv) 

# Display the output 
f, (ax0, ax1, ax2) = plt.subplots(1, 3, 
            subplot_kw={'xticks': [], 'yticks': []}) 
ax0.imshow(img, cmap=plt.cm.gray) 
ax1.imshow(color_mask) 
ax2.imshow(img_masked) 
plt.show() 

下面是输出:

enter image description here

+0

感谢斯蒂芬,这是非常有用的。 – Ctrlspc 2012-02-09 09:30:44

+1

谢谢Stefan,我经常使用这种安静。在颜色方案(即plt.cm.cmap)之后,这怎么能延伸超过3种颜色? 我有一个7位二进制贴图,我需要覆盖灰度图像。 – iratzhash 2016-11-08 11:22:39

+0

plt.cm.viridis(或任何颜色)将为任何呼叫返回RGB值,例如“[10]:plt.cm.viridis(15) Out [10] :(0.28192400000000001,0.089665999999999996,0.41241499999999998,1.0)”。您也可以在整个阵列上操作,例如“plt.cm.viridis(my_image)”来获取适当的RGB值。 – 2016-11-11 01:54:30

8

我最终找到答案这使用PIL,基本上与块的颜色创建一个新的图像,然后合成的原始图像,与此有关新图像,使用定义透明alpha层的遮罩。下面的代码(适用于每个图像转换成一个文件夹,名为数据,输出到一个文件夹名为输出):

from PIL import Image 
import os 

dataFiles = os.listdir('data/') 

for filename in dataFiles: 

    #strip off the file extension 
    name = os.path.splitext(filename)[0] 

    bw = Image.open('data/%s' %(filename,)) 

    #create the coloured overlays 
    red = Image.new('RGB',bw.size,(255,0,0)) 
    green = Image.new('RGB',bw.size,(0,255,0)) 
    blue = Image.new('RGB',bw.size,(0,0,255)) 
    yellow = Image.new('RGB',bw.size,(255,255,0)) 

    #create a mask using RGBA to define an alpha channel to make the overlay transparent 
    mask = Image.new('RGBA',bw.size,(0,0,0,123)) 

    Image.composite(bw,red,mask).convert('RGB').save('output/%sr.bmp' % (name,)) 
    Image.composite(bw,green,mask).convert('RGB').save('output/%sg.bmp' % (name,)) 
    Image.composite(bw,blue,mask).convert('RGB').save('output/%sb.bmp' % (name,)) 
    Image.composite(bw,yellow,mask).convert('RGB').save('output/%sy.bmp' % (name,)) 

不能不幸后输出图像,由于缺乏销售代表。

+2

谢谢,这里是你的一些代表 – LoveGandhi 2012-06-29 03:09:57

1

见我的要旨https://gist.github.com/Puriney/8f89b43d96ddcaf0f560150d2ff8297e

核心功能经由opencv如下所述。

def mask_color_img(img, mask, color=[0, 255, 255], alpha=0.3): 
    ''' 
    img: cv2 image 
    mask: bool or np.where 
    color: BGR triplet [_, _, _]. Default: [0, 255, 255] is yellow. 
    alpha: float [0, 1]. 

    Ref: http://www.pyimagesearch.com/2016/03/07/transparent-overlays-with-opencv/ 
    ''' 
    out = img.copy() 
    img_layer = img.copy() 
    img_layer[mask] = color 
    out = cv2.addWeighted(img_layer, alpha, out, 1 - alpha, 0, out) 
    return(out) 

添加色透明的重叠式RGB或灰度图像可以工作: highlight-on-color highlight-on-gray