[英]Applying a coloured overlay to an image in either PIL or Imagemagik
我是圖像處理的完全新手,我猜這很容易做到,但我只是不知道術語。
基本上,我有一個黑白圖像,我只是想對圖像應用彩色疊加層,這樣我就可以將圖像用藍綠色紅色和黃色疊加,如下圖所示(實際上我無法顯示,因為我沒有足夠的聲譽這樣做 - grrrrrr)。 想象一下,我有一個物理圖像和一個綠色/紅色/藍色/黃色疊加層,我將其放置在圖像頂部。
理想情況下,我想使用 Python PIL 來執行此操作,但我也很樂意使用 ImageMagik 來執行此操作,但是無論哪種方式,我都需要能夠編寫該過程的腳本,因為我有 100 個左右的圖像需要執行過程。
編輯:正如馬特在評論中提到的,這個功能現在在skimage.color.label2rgb
可用。
在最新的開發版本中,我們還引入了saturation
參數,它允許您為彩色圖像添加疊加層。
這是一個代碼片段,展示了如何使用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()
這是輸出:
我最終使用 PIL 找到了這個問題的答案,基本上是創建一個帶有塊顏色的新圖像,然后使用定義透明 alpha 層的蒙版將原始圖像與這個新圖像合成。 下面的代碼(適用於轉換名為 data 的文件夾中的每個圖像,輸出到名為 output 的文件夾中):
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,))
遺憾的是,由於缺少代表,無法發布輸出圖像。
請參閱我的要點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)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.