The behavior of prepartum sows in natural or semi-natural environments is primarily characterized by their motivation for nest-building. This behavior includes actions such as rooting, pawing, and foraging. Automatic recognition of these specific actions can be beneficial for identifying this nesting behavior and assessing a sow’s maternal ability, as well as predicting the approach of the delivery time. In this study, an automatic method for rooting action recognition was developed for prepartum sow in opened movable crate farrowing pen using computer vision. An object detection model was trained first using the YOLOv5 network to enable the segmentation of the sow body and the head region, as well as posture estimation simultaneously. Then, the FlowNet2.0 network was implemented to estimate the optical flow of sow’s head movement. A histogram of oriented optical flow with adaptive direction adjustment was proposed to select the frames in which the sow’s head moved in the direction of the animal’s body orientation. Finally, a support vector machine (SVM) classifier was trained to classify rooting and non-rooting actions. The input feature vector for the classifier was constructed from variations in head motion direction, sow posture and head position. The developed method was tested using 324 short video episodes (each lasting at least 4 s) and 10 long videos (each lasting about 47 min) of sows in opened movable crate farrowing pens. The test results indicated that the developed method can identify whether a video episode containing rooting action or not with the accuracy, sensitivity, and precision of 93.2%, 93.8%, and 92.7%, respectively. For long videos, our method achieved a sensitivity of 80.3% and precision of 84.5% in detecting temporal rooting action proposals with a temporal Intersection-over-Union (tIOU) ≥ 0.5. The presented method provides an automatic way to detect prepartum sow’s rooting actions, which can be further utilized to recognize their nest-building behavior and predict the impending delivery time of the sows.