def get_s(J, gammaS=1): h, w = J.shape line_len_double = float(min(h, w)) / line_len_divisor line_len = int(line_len_double) line_len += line_len % 2 half_line_len = line_len / 2 # compute the image gradient 'Imag' dJ = im2double(J) Ix = np.column_stack((abs(dJ[:, 0:-1] - dJ[:, 1:]), np.zeros((h, 1)))) Iy = np.row_stack((abs(dJ[0:-1, :] - dJ[1:, :]), np.zeros((1, w)))) # eq.1 Imag = np.sqrt(Ix * Ix + Iy * Iy) # Image.fromarray((1 - Imag) * 255).show() # create the 8 directional line segments L L = np.zeros((line_len, line_len, 8)) for n in range(8): if n == 0 or n == 1 or n == 2 or n == 7: for x in range(0, line_len): y = round( ((x + 1) - half_line_len) * math.tan(math.pi / 8 * n)) y = half_line_len - y if 0 < y <= line_len: L[int(y - 1), x, n] = 1 if n < 7: L[:, :, n + 4] = rot90c(L[:, :, n]) L[:, :, 3] = rot90(L[:, :, 7]) G = np.zeros((J.shape[0], J.shape[1], 8)) for n in range(8): G[:, :, n] = signal.convolve2d(Imag, L[:, :, n], "same") # eq.2 Gindex = G.argmax(axis=2) # C is map set C = np.zeros((J.shape[0], J.shape[1], 8)) for n in range(8): C[:, :, n] = Imag * (1 * (Gindex == n)) # line shaping # generate lines at each pixel Spn = np.zeros((J.shape[0], J.shape[1], 8)) for n in range(8): Spn[:, :, n] = signal.convolve2d(C[:, :, n], L[:, :, n], "same") Sp = Spn.sum(axis=2) Sp = (Sp - Sp[:].min()) / (Sp[:].max() - Sp[:].min()) S = (1 - Sp)**gammaS img = Image.fromarray(S * 255) # img.show() return S
def vertConstLoss(ypred, y, net_type='small'): if net_type == 'small': yp_top = ypred[:, 0:32, :, :] yp_bot = ypred[:, 64:96, :, :] yt1 = l1Loss(yp_top[:, :, 32:64, :], util.rot180(yp_top[:, :, 96:128, :])) yt2 = l1Loss(yp_top[:, :, 32:64, :], util.rot90(yp_top[:, :, 0:32, :])) yt3 = l1Loss(yp_top[:, :, 32:64, :], util.rot270(yp_top[:, :, 64:96, :])) yb1 = l1Loss(yp_bot[:, :, 32:64, :], util.rot180(yp_bot[:, :, 96:128, :])) yb2 = l1Loss(yp_bot[:, :, 32:64, :], util.rot270(yp_bot[:, :, 0:32, :])) yb3 = l1Loss(yp_bot[:, :, 32:64, :], util.rot90(yp_bot[:, :, 64:96, :])) if net_type == 'medium': yp_top = ypred[:, 0:64, :, :] yp_bot = ypred[:, 128:192, :, :] yt1 = l1Loss(yp_top[:, :, 64:128, :], util.rot180(yp_top[:, :, 192:256, :])) yt2 = l1Loss(yp_top[:, :, 64:128, :], util.rot90(yp_top[:, :, 0:64, :])) yt3 = l1Loss(yp_top[:, :, 64:128, :], util.rot270(yp_top[:, :, 128:192, :])) yb1 = l1Loss(yp_bot[:, :, 64:128, :], util.rot180(yp_bot[:, :, 192:256, :])) yb2 = l1Loss(yp_bot[:, :, 64:128, :], util.rot270(yp_bot[:, :, 0:64, :])) yb3 = l1Loss(yp_bot[:, :, 64:128, :], util.rot90(yp_bot[:, :, 128:192, :])) if net_type == 'high': yp_top = ypred[:, 0:128, :, :] yp_mid = ypred[:, 128:256, :, :] yp_bot = ypred[:, 256:384, :, :] yt1 = l1Loss(yp_top[:, :, 128:256, :], util.rot180(yp_top[:, :, 384:512, :])) yt2 = l1Loss(yp_top[:, :, 128:256, :], util.rot270(yp_top[:, :, 0:128, :])) yt3 = l1Loss(yp_top[:, :, 128:256, :], util.rot90(yp_top[:, :, 256:384, :])) yb1 = l1Loss(yp_bot[:, :, 128:256, :], util.rot180(yp_bot[:, :, 384:512, :])) yb2 = l1Loss(yp_bot[:, :, 128:256, :], util.rot90(yp_bot[:, :, 0:128, :])) yb3 = l1Loss(yp_bot[:, :, 128:256, :], util.rot270(yp_bot[:, :, 256:384, :])) # ypred = util.makeCubePano(ypred) # y = util.makeCubePano(y) # loss0 = l1Loss(ypred, y) loss = yt1 + yt2 + yt3 + yb1 + yb2 + yb3 return loss
def get_stroke(J, gammaS=1): ''' 得到stroke structure Args: J: 灰度图片(矩阵) gammaS: 线条粗细控制参数 Returns S: 输入图片的笔画结构,类型为矩阵 ''' h, w = J.shape line_len_double = float(min(h, w)) / line_len_divisor line_len = round(line_len_double) line_len += line_len % 2 half_line_len = line_len / 2 # 计算梯度 dJ = im2double(J) Ix = np.column_stack((abs(dJ[:, 0:-1] - dJ[:, 1:]), np.zeros((h, 1)))) Iy = np.row_stack((abs(dJ[0:-1, :] - dJ[1:, :]), np.zeros((1, w)))) Imag = np.sqrt(Ix * Ix + Iy * Iy) # 分8个方向,L[:, :, index]是用来表示第index+1个方向的线段,作为卷积核 L = np.zeros((line_len, line_len, 8)) for n in range(8): if n == 0 or n == 1 or n == 2 or n == 7: for x in range(0, line_len): y = round( ((x + 1) - half_line_len) * math.tan(math.pi / 8 * n)) y = half_line_len - y if 0 < y <= line_len: L[round(y - 1), x, n] = 1 if n < 7: L[:, :, n + 4] = rot90c(L[:, :, n]) L[:, :, 3] = rot90(L[:, :, 7]) G = np.zeros((J.shape[0], J.shape[1], 8)) for n in range(8): # 选取最大值所在的方向 G[:, :, n] = signal.convolve2d(Imag, L[:, :, n], "same") Gindex = G.argmax(axis=2) C = np.zeros((J.shape[0], J.shape[1], 8)) for n in range(8): C[:, :, n] = Imag * (1 * (Gindex == n)) # 将map set C 与方向向量L卷积,在每一个像素上生成线条 Spn = np.zeros((J.shape[0], J.shape[1], 8)) for n in range(8): Spn[:, :, n] = signal.convolve2d(C[:, :, n], L[:, :, n], "same") # 八个方向求和,归一化 Sp = Spn.sum(axis=2) Sp = (Sp - Sp[:].min()) / (Sp[:].max() - Sp[:].min()) S = (1 - Sp)**gammaS img = Image.fromarray(S * 255) return S
def get_s(J, gammaS=1): ''' 产生笔画结构(Stroke Structure Generation ) stroke drawing aims at expressing general structures of the scene 1. classification: 首先计算图片的梯度, 分别在x和y两个方向上进行计算, 然后在相应位置求平方和 开根号 因为存在噪声的干扰, 直接使用计算梯度生成的轮廓效果不好 更重要的是,该方法期望可以模拟人类画画的那种效果,比如人类画一条很长的直线,因为无法一次性画成,所以是分段画的, 该论文希望模拟这种效果。 论文采用的方式是通过预测每个像素点的方向的方式生成线条 一共分成8个方向, 每个45度, 计算得到8个方向向量, 同时作为卷积核 每个像素点的方向是卷积过后8个值里面最大的那个表示的方向, 获得一个map set c, 用1表示方向, 其余为0 2. line shaping: 生成轮廓线条的过程 通过map set c与方向向量做卷积, 将同一个方向上的像素点聚合起来 同时可以将原本梯度图中的边缘像素点连接到线段中 :param J: 图片转换成灰度后的矩阵 :gammaS: 控制参数, 值越大线条越粗 :return: 图片的笔画结构, 轮廓线S ''' h, w = J.shape #卷积核大小 line_len_double = float(min(h, w)) / line_len_divisor #使得其为偶数 line_len = int(line_len_double) line_len += line_len % 2 half_line_len = line_len / 2 # 计算梯度 # compute the image gradient 'Imag' dJ = im2double(J) Ix = np.column_stack((abs(dJ[:, 0:-1] - dJ[:, 1:]), np.zeros((h, 1)))) Iy = np.row_stack((abs(dJ[0:-1, :] - dJ[1:, :]), np.zeros((1, w)))) # eq.1 Imag = np.sqrt(Ix*Ix + Iy*Iy) # 注释下面一行代码可以看到通过简单求梯度的方式进行轮廓结构的产生方式, 容易被噪声影响 # Image.fromarray((1 - Imag) * 255).show() # create the 8 directional line segments L # L[:, :, index]是一个用来表示第index+1个方向的线段 # 是一个卷积核 #由于这个卷积核方向上是对称的,所以可以只赋值一半。 L = np.zeros((line_len, line_len, 8)) for n in range(8): if n == 0 or n == 1 or n == 2 or n == 7: for x in range(0, line_len): y = round(((x+1) - half_line_len) * math.tan(math.pi/8*n)) y = half_line_len - y if 0 < y <= line_len: L[int(y-1), x, n] = 1 if n < 7: L[:, :, n+4] = rot90c(L[:, :, n]) L[:, :, 3] = rot90(L[:, :, 7]) # 生成G矩阵 G = np.zeros((J.shape[0], J.shape[1], 8)) for n in range(8): #做卷积 G[:, :, n] = signal.convolve2d(Imag, L[:, :, n], "same") # eq.2 Gindex = G.argmax(axis=2) # 获取最大值元素所在的下标 axis表示维度 # C is map set C = np.zeros((J.shape[0], J.shape[1], 8)) for n in range(8): # 八个方向 选取最大值所在的方向 # eq.3 论文中公式与解释有出入 选取的应该是最大值方向 C[:, :, n] = Imag * (1 * (Gindex == n)) # line shaping # generate lines at each pixel Spn = np.zeros((J.shape[0], J.shape[1], 8)) for n in range(8): Spn[:, :, n] = signal.convolve2d(C[:, :, n], L[:, :, n], "same") # 八个方向的求和, 并执行归一化操作 Sp = Spn.sum(axis=2) Sp = (Sp - Sp[:].min()) / (Sp[:].max() - Sp[:].min()) S = (1 - Sp) ** gammaS img = Image.fromarray(S * 255) # img.show() return S