1. An image processing apparatus comprising: a memory for storing a program; a processor for executing the program stored in the memory to function as: an acquisition unit configured to acquire a boundary between a target area and a non-target area from an image and distances between respective pixels in the image and a reference position; a determination unit configured to determine a width of an undefined area at each boundary point of a plurality of boundary points in the acquired boundary based on distribution of status values of distances for peripheral pixels of the boundary point; a setting unit configured to set the undefined area for which the width is determined at each boundary point of the plurality of boundary points, on the periphery of the boundary; and a generating unit configured to define an area excluding the undefined area from the target area as the foreground area, define an area excluding the undefined area from the non-target area as the background area, and generate area information that specifies the foreground area, the background area, and the undefined area.