4

My app is supposed to estimate the length (in millimeters) of an object using euro coins as reference. This is a screenshot example:

enter image description here

To get the diameter of the photographed coin I first calculate the equation of a the circle passing through those 3 points of the form

x^2 + y^2 + ax + by + c = 0

and then I have the diameter by

2 * square_root((a/2)^2 + (b/2)^2 -c).

Finally I can perform the following proportion to get the length of the red pen:

/* length_estimated_pen (mm) : distance_green_pins (points) = real_diameter_coin (mm) : diameter_on_screen (points) */

let distanceGreen:Double = Double(sqrt(pow(self.greenLocationA.center.x - self.greenLocationB.center.x, 2.0) + pow(self.greenLocationA.center.y - self.greenLocationB.center.y, 2.0)))

let estimatedMeasure:Double = (distanceGreen * Double(ChosenMeter.moneyDiameter)) / diameter

where in ChosenMeter.moneyDiameter there is stored the real diameter of the chosen coin as reference (by clicking one of the 3 buttons below).

I need to work with Double instead of CGFloat because this tutorial to solve a system of linear equations (to get a,b,c coefficient of circle equation) works with Double.

The problem is the estimated length of the red pen is always overestimated of more than 10 mm. I guess I should apply a correction factor or complicate the calculus taking into consideration other factors, but which? Can you give me some hints? Any help would be useful to me.

Spektre
  • 49,595
  • 11
  • 110
  • 380
SagittariusA
  • 5,289
  • 15
  • 73
  • 127
  • You should probably account for the tilt of the iPhone unless you are sure that it will be perfectly flat when you take the photo. – The Tom Dec 03 '15 at 12:46
  • It was exactly what I was thinking about too but I don't know either to get that tilt or to use it in the calculus... – SagittariusA Dec 03 '15 at 12:55
  • 2
    In order to do that you'd have to represent the space in 3D which would not be easy on iPhones. However you could access the tilt info of the iPhone (http://stackoverflow.com/questions/7135934/ios-gyroscope-api) and only allow the user to take the photo when the iPhone is flat enough (do take some error margin into account). This may improve your measurements – The Tom Dec 03 '15 at 13:01
  • Ok, I will have a look at once at what you've linked, even if I was already trying to take the photos keeping the iphone as flat as possible...thank you :) – SagittariusA Dec 03 '15 at 13:27
  • sorry, I just wanted to be more specify things better – SagittariusA Dec 03 '15 at 13:58
  • @TheTom which method of `CMMotionManager` shall I use to get the tilt of iphone? I can't figure out... `startGyroUpdatesToQueue()` or `startAccelerometerUpdateToQueue()` or `startDeviceMotionUpdatesToQueue` or `startDeviceMotionUpdatesUsingReferenceFrame` or `startMagnetometerUpdatesToQueue`? Maybe this: http://stackoverflow.com/questions/15646433/measuring-tilt-angle-with-cmmotionmanager – SagittariusA Dec 03 '15 at 14:38
  • 1
    Your error seems to large to be attributed to tilt, especially in the example image posted above. I would check your methods. Also try to compute the pixel size from a diameter (just two points) for a comparison. Finally, you should compute the error when the reference points on the coin are off by 1,2,3 pixels and thus know the error range on the computed size of the other object. – john elemans Dec 03 '15 at 18:16
  • Thank you for your comment but I'm afraid I haven't understood well what you mean. Even if I convert the measures from points to pixels (multiplying by 2) the estimated length won't change. Moreover I haven't understood when you talk about 1,2,3 pixels off the coin. I think problem is relared to tilt too and I'm trying to limit the tilt of the camera with CMMotionManager... Excuse me, I'm very new to this kind of app and I feel stupid :( – SagittariusA Dec 03 '15 at 18:24
  • 1
    find the smallest and biggest diameter of the coin those are the basis vectors of your plane from that you should have far better measurements .... using different scale per each basis vector – Spektre Dec 04 '15 at 09:28
  • Thank you @Spektre. Please, would you be more specific? I am not good at understanding this kind of calculus and it's the first time for me. How would I get the smallest and biggest diameter? How should I use them? Sorry... – SagittariusA Dec 04 '15 at 09:32

1 Answers1

3
  1. find the coin (green bounding box rectangle)

    either manually or by some search for specific color,pattern,hough transform,segmentation... This will limit the area to search for next steps

  2. find the boundary (distinct red edge in color intensity)

    so create a list of points that are the coin boundary (be careful with shadows) just scan for high enough intensity bumps.

  3. compute the circle center

    just average of all border points...

  4. test all boundary points for min/max distance to center

    if the tilt is small then you will have many points with min and max radius so take the middle from them. If the |max-min| is very small then you got no tilt. Linebetween min/max distance point and center gives you black basis vectors.

  5. use black basis vectors to measure

    So select 2 points (red line d) to measure and cast green rays from them parallel to basis vectors. Their intersection will create 2 lines a,b. from that it is easy:

    • d = sqrt((a*a)+(b*b))

    where a,b is the size of the lines in units. you can obtain it like:

    • a_size_unit = a_size_pixel * coin_r_unit / rmax_pixel
    • b_size_unit = b_size_pixel * coin_r_unit / rmin_pixel

coin

[note]

This image was selected to emphasize the skew but you should use images of planes almost paralel to chip surface to avoid perspective distortion. This image is not a good example the cube is more distant to camera then coin ...

To account for this see selection criteria for different projections

Community
  • 1
  • 1
Spektre
  • 49,595
  • 11
  • 110
  • 380
  • Thank you for your precious help! I will try to put in practice even if I have never done any image processing so I don't know either where to start from or what you are talking about but this is the only way...I'll try to learn. For now thank you – SagittariusA Dec 04 '15 at 10:00
  • Yes, I know this. In fact I am also limiting the tilt of the camera while the user is taking the photo...see my post here: http://stackoverflow.com/questions/34084956/what-is-the-best-way-to-force-the-user-to-keep-the-iphone-as-horizontal-as-possi – SagittariusA Dec 04 '15 at 10:04
  • @LoryLory look at this [How to find length of upper and lower arc from ellipse image](http://stackoverflow.com/a/34392745/2521214) – Spektre Dec 22 '15 at 08:58