This paper proposes TS-RIL, a two-stage robot imitation learning framework that integrates motion trajectory learning and obstacle avoidance. In the first stage, Region-Constrained Dynamic Motion Primitives (RC-DMPs) are employed to learn robot trajectories within confined spaces by dynamically adjusting learning parameters and introducing a potential field-based coupling term. In the second stage, a Smooth Rapidly-Exploring Random Tree Star (S-RRT*) algorithm is developed to perform local obstacle avoidance and generate smooth, collision-free paths, which are fused with the RC-DMPs trajectory. A continuous RC-DMPs system architecture is also designed to sequentially and alternately execute learning, planning, and motion control. Experimental validations on a ROS-based platform with a 7-DoF robot demonstrate that TS-RIL significantly enhances trajectory learning effectiveness and robustness. Compared with existing approaches, it achieves improved success rate, shorter path length, and higher execution efficiency in complex real-world scenarios.