Motion artifacts deteriorate the quality of magnetic resonance (MR) images. This study proposes a new method to detect phase-encoding (PE) lines corrupted by motion and remove motion artifacts in MR images. 67 cases containing 8710 slices of axial T2-weighted images from the IXI public dataset were split into three datasets, i.e., training (50 cases/6500 slices), validation (5/650), and test (12/1560) sets. First, motion-corrupted k-spaces and images were simulated using a pseudo-random sampling order and random motion tracks. A convolutional neural network (CNN) model was trained to filter the motion-corrupted images. Then, the k-space of the filtered image was compared with the motion-corrupted k-space line-by-line, to detect the PE lines affected by motion. Finally, the unaffected PE lines were used to reconstruct the final image using compressed sensing (CS). For the simulated images with 35%, 40%, 45%, and 50% unaffected PE lines, the mean peak signal-to-noise ratio (PSNRs) of resulting images (mean±standard deviation) were 36.129±3.678, 38.646±3.526, 40.426±3.223, and 41.510±3.167, respectively, and the mean structural similarity (SSIMs) were 0.950±0.046, 0.964±0.035, 0.975±0.025, and 0.979±0.023, respectively. For images with more than 35% PE lines unaffected by motion, images reconstructed with proposed algorithm exhibited better quality than those images reconstructed with CS using 35% under-sampled data (PSNR 37.678±3.261, SSIM 0.964±0.028). It was proved that deep learning and k-space analysis can detect the k-space PE lines affected by motion and CS can be used to reconstruct images from unaffected data, effectively alleviating the motion artifacts.