utils.py 文件源码

python
阅读 21 收藏 0 点赞 0 评论 0

项目:Defect-Prediction 作者: Jorba123 项目源码 文件源码
def change_light(image, value, channel="v"):
    """ Change the light intensity of an image."""

    channelDic = {"h": 0, "s":1, "v":2}
    # "translate" image channel to channel index
    if not channel in channelDic:
        raise AttributeError("invalid channel value. Valid values are h, s, or v")

    # which format (ConvNet (3, w, h) vs. Normal (w, h, 3)
    reshape = False
    prevShape = image.shape

    if image.shape[0] == 3 or image.shape[0] == 1:
        reshape = True
        if image.shape[0] == image.shape[1] or (image.shape[0] == 1 and image.shape[1] == 3): # grayscale 1L, 1L, h, w OR color 1L, 3L, h, w
            reshapeVector = (image.shape[2], image.shape[3], image.shape[1])         
        else:                      
            reshapeVector = (image.shape[1], image.shape[2], image.shape[0])                    # single row color or grayscale 1L/3L, h, w
        image = image.reshape(reshapeVector)

    #print "Shape",image.shape
    #print "dtype",image.dtype
    # convert to hsv
    hsv = cv.cvtColor(image, cv.COLOR_BGR2HSV)
    # hsv[:,:,2] += value -  would be way faster but this does not prevent overflow (a high value gets even higher and becomes 0)
    channels = cv.split(hsv)
    for row in xrange(len(channels[channelDic[channel]])):
        for col in xrange(len(channels[channelDic[channel]][0])):
            channels[channelDic[channel]][row][col] = max(min(255, channels[channelDic[channel]][row][col]*value),0)

    image = cv.cvtColor(cv.merge(channels), cv.COLOR_HSV2BGR)

    # reshape back
    if reshape:        
        image = image.reshape(prevShape)
    return image
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号