压缩感知_断层重建

翻译者:@Loopy
校验者:@barrycg

这个示例展示了从一组沿不同角度获得的平行投影来重建图像的过程。这样的数据集是在CT(计算机断层扫描)中获得的。

在没有任何样本得先验信息的情况下,重建图像所需的投影数与图像的线性大小l(以像素为单位)相同。为了简单起见,我们在这里考虑稀疏图像,其中只有对象边界上的像素具有非零值(例如:这些数据可以对应于细胞材料)。但是请注意,大多数图像在不同的基(basis)上是稀疏的,比如Haar小波 。只获得了l/7的投影,因此有必要利用关于样品的现有信息(稀疏性):这是压缩感知的一个示例。

层析投影操作是一种线性变换。除了线性回归对应的数据保真项外,我们还对图像的L1范数进行了惩罚,以解释其稀疏性。由此产生的优化问题称为Lasso。我们使用类sklearn.linear_model.Lasso,它是使用坐标下降算法实现的。重要的是,这种在稀疏阵上算法的计算效率比这里投影算子更高。

即使在投影中添加了噪声,L1罚项重建得到的结果也会是零误差(所有像素都被成功地标记为0或1)。相比之下,L2罚项(sklearn.linear_model.Ridge)会产生大量标记错误,也就是在重构图像上会观察到伪影,这与L1罚项相反。特别要注意的是,角落里分隔像素的圆形伪影所形成的投影比中央部分少。

  1. import numpy as np
  2. from scipy import sparse
  3. from scipy import ndimage
  4. from sklearn.linear_model import Lasso
  5. from sklearn.linear_model import Ridge
  6. import matplotlib.pyplot as plt
  1. def _weights(x, dx=1, orig=0):
  2. x = np.ravel(x)
  3. floor_x = np.floor((x - orig) / dx).astype(np.int64)
  4. alpha = (x - orig - floor_x * dx) / dx
  5. return np.hstack((floor_x, floor_x + 1)), np.hstack((1 - alpha, alpha))
  1. def _generate_center_coordinates(l_x):
  2. X, Y = np.mgrid[:l_x, :l_x].astype(np.float64)
  3. center = l_x / 2.
  4. X += 0.5 - center
  5. Y += 0.5 - center
  6. return X, Y
  1. def build_projection_operator(l_x, n_dir):
  2. """ 计算层析矩阵
  3. 参数
  4. ----------
  5. l_x : int
  6. 图像阵列的线性大小
  7. n_dir : int
  8. 投影的角度数
  9. Returns
  10. -------
  11. p : shape为(n_dir l_x, l_x**2)的稀疏矩阵
  12. """
  13. X, Y = _generate_center_coordinates(l_x)
  14. angles = np.linspace(0, np.pi, n_dir, endpoint=False)
  15. data_inds, weights, camera_inds = [], [], []
  16. data_unravel_indices = np.arange(l_x ** 2)
  17. data_unravel_indices = np.hstack((data_unravel_indices,
  18. data_unravel_indices))
  19. for i, angle in enumerate(angles):
  20. Xrot = np.cos(angle) * X - np.sin(angle) * Y
  21. inds, w = _weights(Xrot, dx=1, orig=X.min())
  22. mask = np.logical_and(inds >= 0, inds < l_x)
  23. weights += list(w[mask])
  24. camera_inds += list(inds[mask] + i * l_x)
  25. data_inds += list(data_unravel_indices[mask])
  26. proj_operator = sparse.coo_matrix((weights, (camera_inds, data_inds)))
  27. return proj_operator
  1. def generate_synthetic_data():
  2. """ 合成二进制数据 """
  3. rs = np.random.RandomState(0)
  4. n_pts = 36
  5. x, y = np.ogrid[0:l, 0:l]
  6. mask_outer = (x - l / 2.) ** 2 + (y - l / 2.) ** 2 < (l / 2.) ** 2
  7. mask = np.zeros((l, l))
  8. points = l * rs.rand(2, n_pts)
  9. mask[(points[0]).astype(np.int), (points[1]).astype(np.int)] = 1
  10. mask = ndimage.gaussian_filter(mask, sigma=l / n_pts)
  11. res = np.logical_and(mask > mask.mean(), mask_outer)
  12. return np.logical_xor(res, ndimage.binary_erosion(res))
  1. # 生成合成图像和投影
  2. l = 128
  3. proj_operator = build_projection_operator(l, l // 7)
  4. data = generate_synthetic_data()
  5. proj = proj_operator * data.ravel()[:, np.newaxis]
  6. proj += 0.15 * np.random.randn(*proj.shape)
  1. # 用L2(岭)罚项重建
  2. rgr_ridge = Ridge(alpha=0.2)
  3. rgr_ridge.fit(proj_operator, proj.ravel())
  4. rec_l2 = rgr_ridge.coef_.reshape(l, l)
  1. # 用L1(Lasso)罚项重建
  2. # 采用交叉验证法确定最佳值
  3. rgr_lasso = Lasso(alpha=0.001)
  4. rgr_lasso.fit(proj_operator, proj.ravel())
  5. rec_l1 = rgr_lasso.coef_.reshape(l, l)
  1. #画图
  2. plt.figure(figsize=(8, 3.3))
  3. plt.subplot(131)
  4. plt.imshow(data, cmap=plt.cm.gray, interpolation='nearest')
  5. plt.axis('off')
  6. plt.title('原始图像')
  7. plt.subplot(132)
  8. plt.imshow(rec_l2, cmap=plt.cm.gray, interpolation='nearest')
  9. plt.title('L2罚项')
  10. plt.axis('off')
  11. plt.subplot(133)
  12. plt.imshow(rec_l1, cmap=plt.cm.gray, interpolation='nearest')
  13. plt.title('L1罚项')
  14. plt.axis('off')
  15. plt.subplots_adjust(hspace=0.01, wspace=0.01, top=1, bottom=0, left=0,
  16. right=1)
  17. plt.show()

png