2017年的数学建模比赛已经结束了

A题代码贴出来吧。
收获挺大的。因为,通过这次比赛,我开始接触了Python…
现学现写,各种搜doc、demo、stackoverflow也是没谁了。哈哈。
对了好多库例如skimage等,我用pip怎么装都装不上,cl.exe编译失败,折腾的没辙了,又下了一个好大的Anaconda。心疼/(ㄒoㄒ)/~~
为什么会编译失败,装不上呢?而且,用Conda就可以??

import matplotlib.pyplot as plt
from skimage.transform import iradon, rescale, rotate
import numpy as np
import pyexcel as xls

theta = range(0, 180)
sheet = xls.get_book(file_name="A题附件.xls").to_dict()
projection_data = np.array(sheet.get("附件2"))

# Figure 1, Sinogram
plt.subplot(221)
plt.title("Radon transform\n(Sinogram)")
plt.xlabel("Projection angle (deg)")
plt.ylabel("Projection position (pixels)")
plt.imshow(projection_data, cmap=plt.cm.Greys_r,
           extent=(0, 180, 0, 512), aspect='auto')

# Filtered Back Projection
reconstruction_fbp = iradon(projection_data, theta=theta, circle=True)

# Correction
reconstruction_fbp_corr = rotate(reconstruction_fbp, 29.7338)
xoffset = int(9.1674 * 512 / 100 * 0.5643)
yoffset = int(6.3485 * 512 / 100 * 0.5643)
for x, y in np.ndindex(reconstruction_fbp_corr.shape):
    if x < 512 - xoffset and y < 512 - yoffset:
        reconstruction_fbp_corr[x, y] = reconstruction_fbp_corr[x + xoffset, y + yoffset]
        reconstruction_fbp_corr[x + xoffset, y + yoffset] = 0

# Compress to 256*256
reconstruction_fbp_256 = rescale(reconstruction_fbp_corr, 0.5, mode='reflect')
# np.savetxt("reconstruction_Corr_2.txt", reconstruction_fbp_256)

# Figure 2, No corr recon
plt.subplot(223)
plt.title("Reconstruction\nFiltered back projection")
plt.imshow(reconstruction_fbp, cmap=plt.cm.Greys_r)

# Figure 3, Corr recon
plt.subplot(224)
plt.title("Reconstruction\nCorrection")
plt.imshow(reconstruction_fbp_corr, cmap=plt.cm.Greys_r)

plt.show()

你可能感兴趣的:(Python基础)