2012-02-08 38 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

相關問題