예제 #1
0
def get_t(J, type, gammaI=1):

    Jadjusted = natural_histogram_matching(J, type=type)**gammaI
    # Jadjusted = natural_histogram_matching(J, type=type)

    texture = Image.open(texture_file_name)
    texture = np.array(texture.convert("L"))
    # texture = np.array(texture)
    texture = texture[99:texture.shape[0] - 100, 99:texture.shape[1] - 100]

    ratio = texture_resize_ratio * min(J.shape[0], J.shape[1]) / float(1024)
    texture_resize = interpolation.zoom(texture, (ratio, ratio))
    texture = im2double(texture_resize)
    htexture = hstitch(texture, J.shape[1])
    Jtexture = vstitch(htexture, J.shape[0])

    size = J.shape[0] * J.shape[1]

    nzmax = 2 * (size - 1)
    i = np.zeros((nzmax, 1))
    j = np.zeros((nzmax, 1))
    s = np.zeros((nzmax, 1))
    for m in range(1, nzmax + 1):
        i[m - 1] = int(math.ceil((m + 0.1) / 2)) - 1
        j[m - 1] = int(math.ceil((m - 0.1) / 2)) - 1
        s[m - 1] = -2 * (m % 2) + 1
    dx = csr_matrix((s.T[0], (i.T[0], j.T[0])), shape=(size, size))

    nzmax = 2 * (size - J.shape[1])
    i = np.zeros((nzmax, 1))
    j = np.zeros((nzmax, 1))
    s = np.zeros((nzmax, 1))
    for m in range(1, nzmax + 1):
        i[m - 1, :] = int(math.ceil((m - 1 + 0.1) / 2) + J.shape[1] *
                          (m % 2)) - 1
        j[m - 1, :] = math.ceil((m - 0.1) / 2) - 1
        s[m - 1, :] = -2 * (m % 2) + 1
    dy = csr_matrix((s.T[0], (i.T[0], j.T[0])), shape=(size, size))

    Jtexture1d = np.log(
        np.reshape(Jtexture.T, (1, Jtexture.size), order="f") + 0.01)
    Jtsparse = spdiags(Jtexture1d, 0, size, size)
    Jadjusted1d = np.log(
        np.reshape(Jadjusted.T, (1, Jadjusted.size), order="f").T + 0.01)

    nat = Jtsparse.T.dot(Jadjusted1d)  # lnJ(x)
    a = np.dot(Jtsparse.T, Jtsparse)
    b = dx.T.dot(dx)
    c = dy.T.dot(dy)
    mat = a + Lambda * (b + c)  # lnH(x)

    # x = spsolve(a,b) <--> a*x = b
    # lnH(x) * beta(x) = lnJ(x) --> beta(x) = spsolve(lnH(x), lnJ(x))

    beta1d = spsolve(mat, nat)  # eq.8
    beta = np.reshape(beta1d, (J.shape[0], J.shape[1]), order="c")

    T = Jtexture**beta  # eq.9
    T = (T - T.min()) / (T.max() - T.min())

    img = Image.fromarray(T * 255)
    # img.show()

    return T
예제 #2
0
def get_t(J, type, gammaI=1):
    '''
    色调渲染(tone rendering):
    Tone Rendering tone drawing focuses more on shapes, shadow, and shading than on the use of lines
    
    铅笔画的直方图有一定的pattern, 因为只是铅笔和白纸的结合
    可以分成三个区域: 1.亮 2.暗 3.居于中间的部分, 于是就有三个用来模拟的模型
    
    亮的部分使用Laplace分布
    中间的部分使用平均分布
    暗的部分使用高斯分布
    
    随后作者列出了从收集到的简笔画图像中学出来对应的参数
    
    铅笔画的色调 颜色等通过用铅笔重复的涂画来体现

    1. 直方图匹配
        运用三种分布计算图片的直方图, 然后匹配一个正常图片的直方图
    2. 纹理渲染(texture rendering):
        计算模拟需要用铅笔重复涂画的次数beta

    :param J:       图片转换成灰度后的矩阵
    :param type:    图片类型
    :param gammaI:  控制参数, 值越大最后的结果颜色越深
    :return:        色调渲染后的图片矩阵T
    '''

    #直方图匹配
    Jadjusted = natural_histogram_matching(J, type=type) ** gammaI
    # Jadjusted = natural_histogram_matching(J, type=type)

    #将铅笔纹理图,转换到和待处理图一样的大小
    texture = Image.open(texture_file_name)
    texture = np.array(texture.convert("L"))
    # texture = np.array(texture)
    texture = texture[99: texture.shape[0]-100, 99: texture.shape[1]-100]

    ratio = texture_resize_ratio * min(J.shape[0], J.shape[1]) / float(1024)
    texture_resize = interpolation.zoom(texture, (ratio, ratio))
    texture = im2double(texture_resize)
    htexture = hstitch(texture, J.shape[1])
    Jtexture = vstitch(htexture, J.shape[0])

    size = J.shape[0] * J.shape[1]

    nzmax = 2 * (size-1)
    i = np.zeros((nzmax, 1))
    j = np.zeros((nzmax, 1))
    s = np.zeros((nzmax, 1))
    for m in range(1, nzmax+1):
        i[m-1] = int(math.ceil((m+0.1) / 2)) - 1
        j[m-1] = int(math.ceil((m-0.1) / 2)) - 1
        s[m-1] = -2 * (m % 2) + 1
    dx = csr_matrix((s.T[0], (i.T[0], j.T[0])), shape=(size, size))

    nzmax = 2 * (size - J.shape[1])
    i = np.zeros((nzmax, 1))
    j = np.zeros((nzmax, 1))
    s = np.zeros((nzmax, 1))
    for m in range(1, nzmax+1):
        i[m-1, :] = int(math.ceil((m-1+0.1)/2) + J.shape[1] * (m % 2)) - 1
        j[m-1, :] = math.ceil((m-0.1)/2) - 1
        s[m-1, :] = -2 * (m % 2) + 1
    dy = csr_matrix((s.T[0], (i.T[0], j.T[0])), shape=(size, size))

    # +0.01是为了避免出现有0被进行log运算的情况, 但对正常值影响可以被忽略
    Jtexture1d = np.log(np.reshape(Jtexture.T, (1, Jtexture.size), order="f") + 0.01)
    Jtsparse = spdiags(Jtexture1d, 0, size, size)
    Jadjusted1d = np.log(np.reshape(Jadjusted.T, (1, Jadjusted.size), order="f").T + 0.01)


    #构建eq 8,使用Ax=b的形式
    nat = Jtsparse.T.dot(Jadjusted1d)   # lnJ(x)
    a = np.dot(Jtsparse.T, Jtsparse)
    b = dx.T.dot(dx)
    c = dy.T.dot(dy)
    mat = a + Lambda * (b + c)     # lnH(x)

    # x = spsolve(a,b) <--> a*x = b
    # lnH(x) * beta(x) = lnJ(x) --> beta(x) = spsolve(lnH(x), lnJ(x))
    # 使用sparse matrix的spsolve 而不是linalg.solve()
    beta1d = spsolve(mat, nat)  # eq.8
    beta = np.reshape(beta1d, (J.shape[0], J.shape[1]), order="c")

    # 模拟素描时通过重复画线来加深阴影, 用pattern Jtexture重复画beta次
    T = Jtexture ** beta    # eq.9
    T = (T - T.min()) / (T.max() - T.min())

    img = Image.fromarray(T * 255)
    # img.show()

    return T