A computer-implemented method of determining an offset for a first milking tool (100, 1001), which offset designates a spatial vector between a tool reference position (TRP, TRP1) and an estimated actual position (TAP) of the first milking tool (100, 1001) in a tool carrier (101) of an automatic milking machine, the method comprising a computer processing three-dimensional image data (Dimg3D) that represent a teat (T) during attachment to and/or detachment from the first milking tool (100, 1001) by:
- searching, within a search zone (SZ), for a predefined part (MP) of the teat (T) while the teat (T) is attached to the first milking tool (100, 1001), which predefined part (MP) fulfills a measurement criterion, the search zone (SZ) comprising a part of the registered three-dimensional image data (Dimg3D) that is estimated to represent a portion of said teat (T) being located above a mouthpiece (210) of the first milking tool (100, 1001), the mouthpiece (210) being configured to receive the teat (T),
- calculating, in response to the predefined part (MP) being found, a distance (doff) between a specific point of the predefined part (MP) and the tool reference position (TRP, TRP1) and
- determining said offset based on said calculated distance (doff).