Ctr*_*spc 14 python image-processing python-imaging-library
我是一个完整的图像处理新手,我猜这很容易做,但我只是不知道术语.
基本上我有一个黑白图像,我只是想给图像添加彩色叠加,这样我就可以将图像覆盖为蓝绿色读取和黄色,如下图所示(实际上我无法显示,因为我没有足够的声誉这样做 - grrrrrr).想象一下,我有一个物理图像,以及绿色/红色/蓝色/黄色覆盖图,我将其置于图像上方.
理想情况下,我想使用Python PIL这样做,但我会很高兴使用ImageMagik,但无论哪种方式我都需要能够编写脚本,因为我有100个左右的图像,我需要执行过程.
Ste*_*alt 26
这是一段代码片段,展示了如何使用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()
Run Code Online (Sandbox Code Playgroud)
这是输出:

Ctr*_*spc 12
我最终使用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,))
Run Code Online (Sandbox Code Playgroud)
由于缺乏rep,无法发布输出图像.
请参阅我的要点https://gist.github.com/Puriney/8f89b43d96ddcaf0f560150d2ff8297e
核心功能viaopencv描述如下。
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)
Run Code Online (Sandbox Code Playgroud)