如何将头发颜色更改为紫色?

时间:2019-02-19 08:08:21

标签: python computer-vision

我的代码有问题吗?我想将下面图片的头发颜色从绿色更改为紫色。但是,我的输出看起来很奇怪,如果有计算机视觉/图像处理方面的经验的人会帮助我,请谢谢。

from scipy import misc,ndimage
import matplotlib.pyplot as plt
import numpy as np

def dye_hair(filename):
    pic = misc.imread(filename)
    output = []
    for i in range(len(pic)):
        for j in pic[i]:
            if (j[1] > j[0]) & (j[1] > j[2]):
                pic[i][j] = [j[0]*2,j[1]*0.2,j[2]*0.8]
plt.imshow(pic)
plt.show()

逻辑是这样的,如果像素的绿色值大于红色和蓝色的值,则将颜色替换为[R * 2,G * 0.2。 B * 0.8]

green-haired-girl

1 个答案:

答案 0 :(得分:0)

这是因为您没有遍历所有列。您应该for j in range(len(pic[i]))尝试这样做:

def dye_hair(filename):
    pic = misc.imread(filename)
    output = []
    for i in range(len(pic)):
        for j in range(len(pic[i])):
            if (pic[i][j][1] > pic[i][j][0]) and (pic[i][j][1] > pic[i][j][2]):
                pic[i][j] = [pic[i][j][0]*2,pic[i][j][1]*0.2,pic[i][j][2]*0.8]
    plt.imshow(pic)
    plt.show()