0

我正在尝试创建一对可以增加或减少图像整体色彩饱和度的函数。我知道“饱和度”本质上是指“远离或接近灰色”,所以我需要增加或减少 RGB 通道,但我不能平等地完成它们(即 r * 2、g * 2、b * 2) 因为那只会使图像更亮。

我试图使用这里给出的公式:http ://www.georeference.org/doc/colors_as_hue_saturation_and_brightness.htm 但是当我尝试在我的代码中使用它时,图像几乎完全是黑色的,有一些黄色斑点。

这是我迄今为止尝试过的:

def upSaturation(pictu):
   '''Takes a picture and increases the overall color saturation'''
   satuP = duplicatePicture(pictu)
   for pixel in getPixels(satuP):
   r = getRed(pixel)
   g = getGreen(pixel)
   b = getBlue(pixel)
   mn = min(r, g, b)
   mx = max(r, g, b)
   lht = (mx + mn) / 2
   if lht <= 128:
     satu = 255 * ((mx - mn) / (mx + mn))
     clr = makeColor(r * satu, g * satu, b * satu)
     setColor(pixel, clr)
   else:
     sat = 255 * ((mx - mn) / (511 - (mx + mn)))
     color = makeColor(r * sat, g * sat, b * sat)
     setColor(pixel, color)
 show(satuP)
 return satuP

我也尝试过只使用 makeColor(sat, sat, sat) ,但它完全是黑色的,带有一些白色的斑点。我不确定此时还能做什么。我将不胜感激一些指导。

4

1 回答 1

1

要增加饱和度,您必须增加原色的值。例如,如果像素主要是红色,则必须增加像素的红色含量并减少其余部分。

def upSaturation(pictu): 
'''Takes a picture and increases the overall color saturation'''
 satuP = duplicatePicture(pictu)
 for pixel in getPixels(satuP):
  r = getRed(pixel)
  g = getGreen(pixel)
  b = getBlue(pixel)
  # check if red is primary colour
  if r > g and r > b:
   # Saturate with red
   r = r + 5
   if g < b:
     g = g - 5
   else:
     b = b - 5

  # check if green is primary colour
  if g > r and g > b:
   # Saturate with green
   g = g + 5
   if r < b:
     r = r - 5
   else:
     b = b - 5

  # check if blue is primary colour
  if b > r and b > g:
   # Saturate with blue
   b = b + 5
   if r < g:
     r = r - 5
   else:
     g = g - 5

 color = makeColor(r, g, b)
 setColor(pixel, color)
 explore(satuP)
 return satuP
于 2014-11-29T02:47:39.330 回答