def create_image_by_clustering_euclidean(image, patch_width, cluster_size): """ :param image: :param patch_width: :param cluster_size: :return: """ m = np.size(image, 0) n = np.size(image, 1) row = m - patch_width + 1 col = n - patch_width + 1 im_patches, temp = spatial_patchization(image, patch_width) im_patches_vec = np.reshape(im_patches, (row * col, patch_width ** 2)) im_patches_vec = np.transpose(im_patches_vec) im_patches_vec_norm = np.sum(im_patches_vec ** 2, axis=0) idx = np.zeros((1, np.size(im_patches_vec_norm))) zero_idx = im_patches_vec_norm == 0 idx[:, np.logical_not(zero_idx)] = cluster_euclidean( im_patches_vec[:, np.logical_not(zero_idx)], patch_width, cluster_size) return idx
def learn(I_t, base_num=2, group_size=50, D=None, iter_num=20, cluster_im=None): """ :param I_t: 待测试图像 :param base_num: 第一次稀疏编码时稀疏表示的基的个数 :param group_size: 每个集合的最小尺寸 :param D: 初始字典 :param iter_num: 字典迭代的次数 :param cluster_im: 确定聚类前是否需要去噪 :return I_r: 重建的去噪图像 dictionary: 更新后的字典 I_r_no_learn: 未进行字典更新重建的图像 dict_size: 字典的尺寸 """ # 进行相关参数设置 block_len = 20 # 图像块大小 block_size = block_len ** 2 m = np.size(I_t, 0) # 图像大小 n = np.size(I_t, 1) row = m - block_len + 1 # 图像块个数 col = n - block_len + 1 if cluster_im is None: cluster = 'gauss' # 添加高斯噪声 else: cluster = 'prev' odctndict = overcomplete_dct_dictionary_nd create_image = create_image_by_clustering_euclidean create_noisy_image = create_noisy_image_by_clustering_euclidean reshape = np.reshape # 初始字典为DCT字典 if D is None: D = np.log(np.absolute(odctndict(block_len, block_size))) atom_size = np.size(D, 1) # 进行图像块聚类 if cluster is 'gauss': idx = create_noisy_image(I_t, block_len, group_size) else: idx = create_image(cluster_im, block_len, group_size) # 获取图像块,并转换为列向量的形式 [I_t_patch, patch_count] = spatial_patchization(I_t, block_len) Y = reshape(I_t_patch, (row * col, block_size)) # 进行图像块的稀疏求解 R = np.zeros((block_size, row * col)) clusters_num = np.max(idx) # 聚类的个数 DTY = np.transpose(D) * Y alpha = np.zeros((atom_size, row * col)) for i in range(0, clusters_num): R[:, idx == i], alpha[:, idx == i] = pga(D, DTY[:, idx == i], base_num) # 根据重建图像块,平均得到重建图像cur_im I_r_no_learn = reconstim_from_patch(R, patch_count, block_len, m, n) I_cur = copy.copy(I_r_no_learn) # 字典和系数循环更新过程 I_r = np.zeros((m, n, iter_num)) dict_size = np.zeros((iter_num, 1)) for dict_learn_iter in range(iter_num): # 设置内部循环次数 if dict_learn_iter == 1: dict_inner_loop_num = 2 else: dict_inner_loop_num = 20 # 从图像cur_im中获取图像块,并转换为列向量的形式 I_cur_patch = spatial_patchization(I_cur, block_len) Y_cur = reshape(I_cur_patch, (row * col, block_size)) # 单次字典和系数更新 for dict_inner_loop in range(dict_inner_loop_num): # 梯度下降法更新字典 D, alpha = dict_step_update(D, Y, alpha, R) # 归一化字典 D /= np.tile(np.sqrt(np.sum(D, 0)), (block_size, 1)) atom_size = np.size(D, 1) dict_size[dict_learn_iter] = atom_size DTY = np.dot(np.transpose(D), Y) # 更新系数,共clusters_num个集合,每个集合单独更新,采用梯度下降法 for i in range(0, clusters_num): nnz_loc = np.sum(np.absolute(alpha[:, (idx == i)]), 1) > 1e-3 alpha[:, (idx == i)], null = alpha_step_update(DTY[nnz_loc, (idx == i)], D[:, nnz_loc], alpha[:, (idx == i)]) R[:, idx == i] = np.exp(np.dot(D[:, nnz_loc], alpha[:, (idx == i)])) # 重建图像 I_cur = reconstim_from_patch(np.transpose(R), patch_count, block_len, m, n) alpha = np.zeros((atom_size, row * col)) DTY = np.dot(np.transpose(D), Y) # 贪婪算法,采用自动停止标准 for i in range(clusters_num): R[:, idx == i], alpha[:, idx == i] = pga_oracle(D, DTY[:, idx == i], Y_cur) # 重建图像 I_cur = reconstim_from_patch(np.transpose(R), patch_count, block_len, m, n) I_r[:, :, dict_learn_iter] = I_cur print(dict_learn_iter) return I_r, D, I_r_no_learn, dict_size