The usual way to do this is to form a calibration plate. One way to form a calibration plate is to place known-shaped "fiducials" in a known-sized array on the surface where you want to do the measurement.
Then, knowing the measurements of the calibration plate, you can count pixels and do a map from pixels in each direction (possibly in each area of the image) to real-world measurements.
You also need to know that your camera will always be in a fixed location relative to the measurement surface, and that the lighting is good enough for you to pick up the external shape of the object.
An example of one possible calibration plate is below.