我想拍摄RGB图像并将其转换为黑白RGB图像,如果其HSV值介于某个范围之间,则像素为黑色,否则为白色。
目前我创建了一个新图像,然后通过迭代其数据创建了一个新的像素值列表,然后列出了.putdata()
列表以形成新图像。
感觉应该有更快的方法来做到这一点,例如:使用.point()
,但似乎.point()
没有给定像素,而是从0到255的值。是否有.point()
转换但是在像素上?
答案 0 :(得分:21)
好的,这个 工作(修复了一些溢出错误):
import numpy, Image
i = Image.open(fp).convert('RGB')
a = numpy.asarray(i, int)
R, G, B = a.T
m = numpy.min(a,2).T
M = numpy.max(a,2).T
C = M-m #chroma
Cmsk = C!=0
# Hue
H = numpy.zeros(R.shape, int)
mask = (M==R)&Cmsk
H[mask] = numpy.mod(60*(G-B)/C, 360)[mask]
mask = (M==G)&Cmsk
H[mask] = (60*(B-R)/C + 120)[mask]
mask = (M==B)&Cmsk
H[mask] = (60*(R-G)/C + 240)[mask]
H *= 255
H /= 360 # if you prefer, leave as 0-360, but don't convert to uint8
# Value
V = M
# Saturation
S = numpy.zeros(R.shape, int)
S[Cmsk] = ((255*C)/V)[Cmsk]
# H, S, and V are now defined as integers 0-255
它基于维基百科对HSV的定义。随着时间的推移,我会仔细研究。肯定有加速和错误。如果您发现任何问题,请告诉我。欢呼声。
结果:
从这个色轮开始:
我得到了这些结果:
色相:
值:
饱和度:
答案 1 :(得分:5)
编辑2:现在返回与Paul的代码相同的结果,因为它应该......
import numpy, scipy
image = scipy.misc.imread("test.png") / 255.0
r, g, b = image[:,:,0], image[:,:,1], image[:,:,2]
m, M = numpy.min(image[:,:,:3], 2), numpy.max(image[:,:,:3], 2)
d = M - m
# Chroma and Value
c = d
v = M
# Hue
h = numpy.select([c ==0, r == M, g == M, b == M], [0, ((g - b) / c) % 6, (2 + ((b - r) / c)), (4 + ((r - g) / c))], default=0) * 60
# Saturation
s = numpy.select([c == 0, c != 0], [0, c/v])
scipy.misc.imsave("h.png", h)
scipy.misc.imsave("s.png", s)
scipy.misc.imsave("v.png", v)
从0到360给出色调,从0到1的饱和度和从0到1的值。我以图像格式查看结果,它们看起来很好。
我不确定通过阅读你的问题是否只是你感兴趣的HSV中的“价值”。如果是,那么你可以绕过大部分代码。
然后,您可以根据这些值选择像素,并使用以下内容将它们设置为1(或白/黑):
newimage = (v > 0.3) * 1
答案 2 :(得分:2)
此解决方案基于Paul的代码。我修复了DivByZero Bug并将RGB实现为HSL。还有HSL到RGB:
import numpy
def rgb_to_hsl_hsv(a, isHSV=True):
"""
Converts RGB image data to HSV or HSL.
:param a: 3D array. Retval of numpy.asarray(Image.open(...), int)
:param isHSV: True = HSV, False = HSL
:return: H,S,L or H,S,V array
"""
R, G, B = a.T
m = numpy.min(a, 2).T
M = numpy.max(a, 2).T
C = M - m #chroma
Cmsk = C != 0
# Hue
H = numpy.zeros(R.shape, int)
mask = (M == R) & Cmsk
H[mask] = numpy.mod(60 * (G[mask] - B[mask]) / C[mask], 360)
mask = (M == G) & Cmsk
H[mask] = (60 * (B[mask] - R[mask]) / C[mask] + 120)
mask = (M == B) & Cmsk
H[mask] = (60 * (R[mask] - G[mask]) / C[mask] + 240)
H *= 255
H /= 360 # if you prefer, leave as 0-360, but don't convert to uint8
# Saturation
S = numpy.zeros(R.shape, int)
if isHSV:
# This code is for HSV:
# Value
V = M
# Saturation
S[Cmsk] = ((255 * C[Cmsk]) / V[Cmsk])
# H, S, and V are now defined as integers 0-255
return H.swapaxes(0, 1), S.swapaxes(0, 1), V.swapaxes(0, 1)
else:
# This code is for HSL:
# Value
L = 0.5 * (M + m)
# Saturation
S[Cmsk] = ((C[Cmsk]) / (1 - numpy.absolute(2 * L[Cmsk]/255.0 - 1)))
# H, S, and L are now defined as integers 0-255
return H.swapaxes(0, 1), S.swapaxes(0, 1), L.swapaxes(0, 1)
def rgb_to_hsv(a):
return rgb_to_hsl_hsv(a, True)
def rgb_to_hsl(a):
return rgb_to_hsl_hsv(a, False)
def hsl_to_rgb(H, S, L):
"""
Converts HSL color array to RGB array
H = [0..360]
S = [0..1]
l = [0..1]
http://en.wikipedia.org/wiki/HSL_and_HSV#From_HSL
Returns R,G,B in [0..255]
"""
C = (1 - numpy.absolute(2 * L - 1)) * S
Hp = H / 60.0
X = C * (1 - numpy.absolute(numpy.mod(Hp, 2) - 1))
# initilize with zero
R = numpy.zeros(H.shape, float)
G = numpy.zeros(H.shape, float)
B = numpy.zeros(H.shape, float)
# handle each case:
mask = (Hp >= 0) == ( Hp < 1)
R[mask] = C[mask]
G[mask] = X[mask]
mask = (Hp >= 1) == ( Hp < 2)
R[mask] = X[mask]
G[mask] = C[mask]
mask = (Hp >= 2) == ( Hp < 3)
G[mask] = C[mask]
B[mask] = X[mask]
mask = (Hp >= 3) == ( Hp < 4)
G[mask] = X[mask]
B[mask] = C[mask]
mask = (Hp >= 4) == ( Hp < 5)
R[mask] = X[mask]
B[mask] = C[mask]
mask = (Hp >= 5) == ( Hp < 6)
R[mask] = C[mask]
B[mask] = X[mask]
m = L - 0.5*C
R += m
G += m
B += m
R *=255.0
G *=255.0
B *=255.0
return R.astype(int),G.astype(int),B.astype(int)
def combineRGB(r,g,b):
"""
Combines separated R G B arrays into one array = image.
scipy.misc.imsave("rgb.png", combineRGB(R,G,B))
"""
rgb = numpy.zeros((r.shape[0],r.shape[1],3), 'uint8')
rgb[..., 0] = r
rgb[..., 1] = g
rgb[..., 2] = b
return rgb
答案 3 :(得分:1)
我认为最快的结果将是通过numpy。该函数看起来像(更新,在示例中添加了更多细节):
limg = im.convert("L", ( 0.5, 0.5, 0.5, 0.5 ) )
na = numpy.array ( limg.getdata() )
na = numpy.piecewise(na, [ na > 128 ], [255, 0])
limg.pytdata(na)
limg.save("new.png")
理想情况下,您可以使用分段函数而无需先转换为黑白,这更像是原始示例。语法将类似于:
na = numpy.piecewise(na, [ na[0] > 128 ], [255, 0])
但是,你必须要小心,因为RGB图像的返回值是3或4元组。