text:
Counting Grains of Rice
Consider these 10 images of various amounts of uncooked grains of white rice.
THESE ARE ONLY THUMBNAILS. Click an image to view it at full size.
A:
B:
C:
D:
E:
F:
G:
H:
I:
J:
Grain Counts: A: 3, B: 5, C: 12, D: 25, E: 50, F: 83, G: 120, H:150, I: 151, J: 200
Notice that...

The grains may touch each other but they never overlap. The layout of grains is never more than one grain high.
The images have different dimensions but the scale of the rice in all of them is consistent because the camera and background were stationary.
The grains never go out of bounds or touch the image boundaries.
The background is always the same consistent shade of yellowish-white.
Small and large grains are counted alike as one grain each.

These 5 points are guarantees for all images of this sort.
Challenge
Write a program that takes in such images and, as accurately as possible, counts the number of grains of rice.
Your program should take the filename of the image and print the number of grains it calculates. Your program must work for at least one of these image file formats: JPEG, Bitmap, PNG, GIF, TIFF (right now the images are all JPEGs).
You may use image processing and computer vision libraries.
You may not hardcode the outputs of the 10 example images. Your algorithm should be applicable to all similar rice-grain images. It should be able to run in less than 5 minutes on a decent modern computer if the image area is less than 2000*2000 pixels and there are fewer than 300 grains of rice.
Scoring
For each of the 10 images take the absolute value of the actual number of grains minus the number of grains your program predicts. Sum these absolute values to get your score. The lowest score wins. A score of 0 is perfect.
In case of ties the highest voted answer wins. I may test your program on additional images to verify its validity and accuracy.

Surely someone has to try scikit-learn!

Great contest! :) Btw - could tell us something about end date of this challenge?

@cyriel Probably sometime next week when there are no more additional submissions.

I think this question deserves a bounty first!  The best score is down to 9 now.

@Lembik Down to 7 :)

One day, a rice scientist is going to come along and be head over heels happy that this question exists.

@Nit Just tell them http://www.ncbi.nlm.nih.gov/pmc/articles/PMC3510117/ :)

Mathematica, score: 7
i = {"https://i.stack.imgur.com/8T6W2.jpg",  "https://i.stack.imgur.com/pgWt1.jpg",
     "https://i.stack.imgur.com/M0K5w.jpg",  "https://i.stack.imgur.com/eUFNo.jpg",
     "https://i.stack.imgur.com/2TFdi.jpg",  "https://i.stack.imgur.com/wX48v.jpg",
     "https://i.stack.imgur.com/eXCGt.jpg",  "https://i.stack.imgur.com/9na4J.jpg",
     "https://i.stack.imgur.com/UMP9V.jpg",  "https://i.stack.imgur.com/nP3Hr.jpg"};

im = Import /@ i;

I think the functions' names are descriptive enough:
getSatHSVChannelAndBinarize[i_Image]             := Binarize@ColorSeparate[i, "HSB"][[2]]
removeSmallNoise[i_Image]                        := DeleteSmallComponents[i, 100]
fillSmallHoles[i_Image]                          := Closing[i, 1]
getMorphologicalComponentsAreas[i_Image]         := ComponentMeasurements[i, "Area"][[All, 2]]
roundAreaSizeToGrainCount[areaSize_, grainSize_] := Round[areaSize/grainSize]

Processing all the pictures at once:
counts = Plus @@@
  (roundAreaSizeToGrainCount[#, 2900] & /@
      (getMorphologicalComponentsAreas@
        fillSmallHoles@
         removeSmallNoise@
          getSatHSVChannelAndBinarize@#) & /@ im)

(* Output {3, 5, 12, 25, 49, 83, 118, 149, 152, 202} *)

The score is:
counts - {3, 5, 12, 25, 50, 83, 120, 150, 151, 200} // Abs // Total
(* 7 *)

Here you can see the score sensitivity wrt the grain size used:

Much clearer, thanks!

Can this exact procedure be copied in python or is there something special Mathematica is doing here that python libraries can't do?

@Lembik No idea. No python here. Sorry. (However, I doubt  the exact same algorithms for EdgeDetect[], DeleteSmallComponents[] and Dilation[] are implemented elsewhere)

Python, Score: 24 16
This solution, like Falko's one, is based on measuring the "foreground" area and dividing it by the average grain area.
In fact, what this program tries to detect is the background, not so much as the foreground.
Using the fact that rice grains never touch the image boundary, the program starts by flood-filling white at the top-left corner.
The flood-fill algorithm paints adjacent pixels if the difference between theirs and the current pixel's brightness is within a certain threshold, thus adjusting to gradual change in the background color.
At the end of this stage, the image might look something like this:

As you can see, it does a pretty good job at detecting the background, but it leaves out any areas that are "trapped" between the grains.
We handle these areas by estimating the background brightness at each pixel and painting all equal-or-brighter pixels.
This estimation works like so:
during the flood-fill stage, we calculate the average background brightness for each row and each column.
The estimated background brightness at each pixel is the average of the row and column brightness at that pixel.
This produces something like this:

EDIT:
Finally, the area of each continuous foreground (i.e. non-white) region is divided by the average, precalculated, grain area, giving us an estimate of the grain count in the said region. The sum of these quantities is the result.
Initially, we did the same thing for the entire foreground area as a whole, but this approach is, literally, more fine-grained.

from sys import argv; from PIL import Image

# Init
I = Image.open(argv[1]); W, H = I.size; A = W * H
D = [sum(c) for c in I.getdata()]
Bh = [0] * H; Ch = [0] * H
Bv = [0] * W; Cv = [0] * W

# Flood-fill
Background = 3 * 255 + 1; S = [0]
while S:
    i = S.pop(); c = D[i]
    if c != Background:
        D[i] = Background
        Bh[i / W] += c; Ch[i / W] += 1
        Bv[i % W] += c; Cv[i % W] += 1
        S += [(i + o) % A for o in [1, -1, W, -W] if abs(D[(i + o) % A] - c) < 10]

# Eliminate "trapped" areas
for i in xrange(H): Bh[i] /= float(max(Ch[i], 1))
for i in xrange(W): Bv[i] /= float(max(Cv[i], 1))
for i in xrange(A):
    a = (Bh[i / W] + Bv[i % W]) / 2
    if D[i] >= a: D[i] = Background

# Estimate grain count
Foreground = -1; avg_grain_area = 3038.38; grain_count = 0
for i in xrange(A):
    if Foreground < D[i] < Background:
        S = [i]; area = 0
        while S:
            j = S.pop() % A
            if Foreground < D[j] < Background:
                D[j] = Foreground; area += 1
                S += [j - 1, j + 1, j - W, j + W]
        grain_count += int(round(area / avg_grain_area))

# Output
print grain_count

Takes the input filename through the comand line.
Results
      Actual  Estimate  Abs. Error
A         3         3           0
B         5         5           0
C        12        12           0
D        25        25           0
E        50        48           2
F        83        83           0
G       120       116           4
H       150       145           5
I       151       156           5
J       200       200           0
                        ----------
                Total:         16

This is a really clever solution, nice work!

where does avg_grain_area = 3038.38; come from?

@njzk2 It's precalculated from the foreground area and actual grain count of the test images.

so you need the actual number of grains before measuring them?

@njzk2 No, only their average area :)

doesn't that count as hardcoding the result?

@njzk2 No.  Given the rule The images have different dimensions but the scale of the rice in all of them is consistent because the camera and background were stationary. This is merely a value that represents that rule.  The result, however, changes according to the input.  If you change the rule, then this value will change, but the result will be the same - based on the input.

Note that this, and all the other solutions using area, fail to follow this rule: Small and large grains are counted alike as one grain each. The results, however, speak well of this method.

I'm fine with the average area thing. Grain area is (roughly) constant across images.

 Python + OpenCV : Score 27
 Horizontal line scanning
Idea : scan the image, one row at a time. For each line, count the number rice grains encountered (by checking if pixel turns black to white or the opposite). If number of grains for the line increase (compared to previous line), it means we encountered a new grain. If that number decrease, it means we passed over a grain. In this case, add +1 to the total result.

Number in red = rice grains encountered for that line
Number in gray = total amount of grains encountered (what we are looking for)

Because of the way algorithm works, it is important to have a clean, b/w image. Lot of noise produce bad results. First main background is cleaned using floodfill (solution similar to Ell answer) then threshold is applied to produce black and white result.

It is far from perfect, but it produce good results regarding simplicity. There is probably many way to improve it (by providing better b/w image, scanning in other directions (eg : vertical, diagonal) taking the average etc...)
import cv2
import numpy
import sys

filename = sys.argv[1]
I = cv2.imread(filename, 0)
h,w = I.shape[:2]
diff = (3,3,3)
mask = numpy.zeros((h+2,w+2),numpy.uint8)
cv2.floodFill(I,mask,(0,0), (255,255,255),diff,diff)
T,I = cv2.threshold(I,180,255,cv2.THRESH_BINARY)
I = cv2.medianBlur(I, 7)

totalrice = 0
oldlinecount = 0
for y in range(0, h):
    oldc = 0
    linecount = 0
    start = 0
    for x in range(0, w):
        c = I[y,x] < 128;
        if c == 1 and oldc == 0:
            start = x
        if c == 0 and oldc == 1 and (x - start) > 10:
            linecount += 1
        oldc = c
    if oldlinecount != linecount:
        if linecount < oldlinecount:
            totalrice += oldlinecount - linecount
        oldlinecount = linecount
print totalrice

The errors per image : 0, 0, 0, 3, 0, 12, 4, 0, 7, 1

Python + OpenCV: Score 84
Here is a first naive attempt. It applies an adaptive threshold with manually tuned parameters, closes some holes with subsequent erosion and dilution and derives the number of grains from the foreground area.
import cv2
import numpy as np

filename = raw_input()

I = cv2.imread(filename, 0)
I = cv2.medianBlur(I, 3)
bw = cv2.adaptiveThreshold(I, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 101, 1)

kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (17, 17))
bw = cv2.dilate(cv2.erode(bw, kernel), kernel)

print np.round_(np.sum(bw == 0) / 3015.0)

Here you can see the intermediate binary images (black is foreground):

The errors per image are 0, 0, 2, 2, 4, 0, 27, 42, 0 and 7 grains.

C# + OpenCvSharp, Score: 2
This is my second attempt. It is quite different from my first attempt, which is a lot simpler, so I am posting it as a separate solution.
The basic idea is to identify and label each individual grain by an iterative ellipse fit. Then remove the pixels for this grain from the source, and try to find the next grain, until every pixel has been labeled.
This is not the most pretty solution. It is a giant hog with 600 lines of code.
It needs 1.5 minutes for the biggest image. And I really apologize for the messy code.
There are so many parameters and ways to thinker in this thing that I am quite afraid of overfitting my program for the 10 sample images. The final score of 2 is almost definitely a case of overfitting: I have two parameters, average grain size in pixel, and minimum ratio of pixel / elipse_area, and at the end I simply exhausted all combinations of these two parameters until I got the lowest score. I am not sure whether this is all that kosher with the rules of this challenge.
average_grain_size_in_pixel = 2530
pixel / elipse_area >= 0.73
But even without these overfitting clutches, the results are quite nice.
Without a fixed grain size or pixel-ratio, simply by estimating the average grain size from the training images, the score is still 27.
And I get as the output not only the number, but the actual position, orientation and shape of each grain. there are a small number of mislabeled grains, but overall most of the labels accurately match the real grains:
A
B
C
D
E
F
G
H
I
J
(click on each image for the full-sized version)
After this labeling step, my program looks at each individual grain, and estimates based on the number of pixels and the pixel/ellipse-area ratio, whether this is

a single grain (+1)
multiple grains mislabeled as one (+X)
a blob too small to be a grain (+0)

The error scores for each image are
A:0; B:0; C:0; D:0; E:2; F:0; G:0 ; H:0; I:0, J:0
However the actual error is probably a little higher. Some errors within the same image cancel each other out. Image H in particular has some badly mislabeled grains, whereas in image E the labels are mostly correct
The concept is a little contrived:

First the foreground is separated via otsu-thresholding on the saturation channel (see my previous answer for details)
repeat until no more pixels left:

select the largest blob
choose 10 random edge pixels on this blob as starting positions for a grain
for each starting point

assume a grain with height and width 10 pixels at this position.
repeat until convergence

go radially outwards from this point, at different angles, until you encounter an edge pixel (white-to-black)
the found pixels should hopefully be the edge pixels of a single grain. Try to separate inliers from outliers, by discarding pixels that are a more distant from the assumed ellipse than the others
repeatedly try to fit an ellipse through a subset of the inliers, keep the best ellipse (RANSACK)
update the grain position, orientation, width and height with the found elipse
if the grain position does not change significantly, stop

among the 10 fitted grains, choose the best grain according to shape, number of edge pixels. Discard the others
remove all pixels for this grain from the source image, then repeat
finally, go through the list of found grains, and count each grain either as 1 grain, 0 grains (too small) or 2 grains (too big)

One of my main problems was that I did not want to implement a full ellipse-point distance metric, since calculating that in itself is a complicated iterative process. So I used various workarounds using the OpenCV functions Ellipse2Poly and FitEllipse, and the results are not too pretty.
Apparently i also broke the size limit for codegolf.
An answer is limited to 30000 characters, i am currently at 34000. So I'll have to shorten the code below somewhat.
The full code can be seen at http://pastebin.com/RgM7hMxq
Sorry for this, I was not aware that there was a size limit.
class Program
{
    static void Main(string[] args)
    {

                // Due to size constraints, I removed the inital part of my program that does background separation. For the full source, check the link, or see my previous program.

                // list of recognized grains
                List<Grain> grains = new List<Grain>();

                Random rand = new Random(4); // determined by fair dice throw, guaranteed to be random

                // repeat until we have found all grains (to a maximum of 10000)
                for (int numIterations = 0; numIterations < 10000; numIterations++ )
                {
                    // erode the image of the remaining foreground pixels, only big blobs can be grains
                    foreground.Erode(erodedForeground,null,7);

                    // pick a number of starting points to fit grains
                    List<CvPoint> startPoints = new List<CvPoint>();
                    using (CvMemStorage storage = new CvMemStorage())
                    using (CvContourScanner scanner = new CvContourScanner(erodedForeground, storage, CvContour.SizeOf, ContourRetrieval.List, ContourChain.ApproxNone))
                    {
                        if (!scanner.Any()) break; // no grains left, finished!

                        // search for grains within the biggest blob first (this is arbitrary)
                        var biggestBlob = scanner.OrderByDescending(c => c.Count()).First();

                        // pick 10 random edge pixels
                        for (int i = 0; i < 10; i++)
                        {
                            startPoints.Add(biggestBlob.ElementAt(rand.Next(biggestBlob.Count())).Value);
                        }
                    }

                    // for each starting point, try to fit a grain there
                    ConcurrentBag<Grain> candidates = new ConcurrentBag<Grain>();
                    Parallel.ForEach(startPoints, point =>
                    {
                        Grain candidate = new Grain(point);
                        candidate.Fit(foreground);
                        candidates.Add(candidate);
                    });

                    Grain grain = candidates
                        .OrderByDescending(g=>g.Converged) // we don't want grains where the iterative fit did not finish
                        .ThenBy(g=>g.IsTooSmall) // we don't want tiny grains
                        .ThenByDescending(g => g.CircumferenceRatio) // we want grains that have many edge pixels close to the fitted elipse
                        .ThenBy(g => g.MeanSquaredError)
                        .First(); // we only want the best fit among the 10 candidates

                    // count the number of foreground pixels this grain has
                    grain.CountPixel(foreground);

                    // remove the grain from the foreground
                    grain.Draw(foreground,CvColor.Black);

                    // add the grain to the colection fo found grains
                    grains.Add(grain);
                    grain.Index = grains.Count;

                    // draw the grain for visualisation
                    grain.Draw(display, CvColor.Random());
                    grain.DrawContour(display, CvColor.Random());
                    grain.DrawEllipse(display, CvColor.Random());

                    //display.SaveImage("10-foundGrains.png");
                }

                // throw away really bad grains
                grains = grains.Where(g => g.PixelRatio >= 0.73).ToList();

                // estimate the average grain size, ignoring outliers
                double avgGrainSize =
                    grains.OrderBy(g => g.NumPixel).Skip(grains.Count/10).Take(grains.Count*9/10).Average(g => g.NumPixel);

                //ignore the estimated grain size, use a fixed size
                avgGrainSize = 2530;

                // count the number of grains, using the average grain size
                double numGrains = grains.Sum(g => Math.Round(g.NumPixel * 1.0 / avgGrainSize));

                // get some statistics
                double avgWidth = grains.Where(g => Math.Round(g.NumPixel * 1.0 / avgGrainSize) == 1).Average(g => g.Width);
                double avgHeight = grains.Where(g => Math.Round(g.NumPixel * 1.0 / avgGrainSize) == 1).Average(g => g.Height);
                double avgPixelRatio = grains.Where(g => Math.Round(g.NumPixel * 1.0 / avgGrainSize) == 1).Average(g => g.PixelRatio);

                int numUndersized = grains.Count(g => Math.Round(g.NumPixel * 1.0 / avgGrainSize) < 1);
                int numOversized = grains.Count(g => Math.Round(g.NumPixel * 1.0 / avgGrainSize) > 1);

                double avgWidthUndersized = grains.Where(g => Math.Round(g.NumPixel * 1.0 / avgGrainSize) < 1).Select(g=>g.Width).DefaultIfEmpty(0).Average();
                double avgHeightUndersized = grains.Where(g => Math.Round(g.NumPixel * 1.0 / avgGrainSize) < 1).Select(g => g.Height).DefaultIfEmpty(0).Average();
                double avgGrainSizeUndersized = grains.Where(g => Math.Round(g.NumPixel * 1.0 / avgGrainSize) < 1).Select(g => g.NumPixel).DefaultIfEmpty(0).Average();
                double avgPixelRatioUndersized = grains.Where(g => Math.Round(g.NumPixel * 1.0 / avgGrainSize) < 1).Select(g => g.PixelRatio).DefaultIfEmpty(0).Average();

                double avgWidthOversized = grains.Where(g => Math.Round(g.NumPixel * 1.0 / avgGrainSize) > 1).Select(g => g.Width).DefaultIfEmpty(0).Average();
                double avgHeightOversized = grains.Where(g => Math.Round(g.NumPixel * 1.0 / avgGrainSize) > 1).Select(g => g.Height).DefaultIfEmpty(0).Average();
                double avgGrainSizeOversized = grains.Where(g => Math.Round(g.NumPixel * 1.0 / avgGrainSize) > 1).Select(g => g.NumPixel).DefaultIfEmpty(0).Average();
                double avgPixelRatioOversized = grains.Where(g => Math.Round(g.NumPixel * 1.0 / avgGrainSize) > 1).Select(g => g.PixelRatio).DefaultIfEmpty(0).Average();

                Console.WriteLine("===============================");
                Console.WriteLine("Grains: {0}|{1:0.} of {2} (e{3}), size {4:0.}px, {5:0.}x{6:0.}  {7:0.000}  undersized:{8}  oversized:{9}   {10:0.0} minutes  {11:0.0} s per grain",grains.Count,numGrains,expectedGrains[fileNo],expectedGrains[fileNo]-numGrains,avgGrainSize,avgWidth,avgHeight, avgPixelRatio,numUndersized,numOversized,watch.Elapsed.TotalMinutes, watch.Elapsed.TotalSeconds/grains.Count);

                // draw the description for each grain
                foreach (Grain grain in grains)
                {
                    grain.DrawText(avgGrainSize, display, CvColor.Black);
                }

                display.SaveImage("10-foundGrains.png");
                display.SaveImage("X-" + file + "-foundgrains.png");
            }
        }
    }
}

public class Grain
{
    private const int MIN_WIDTH = 70;
    private const int MAX_WIDTH = 130;
    private const int MIN_HEIGHT = 20;
    private const int MAX_HEIGHT = 35;

    private static CvFont font01 = new CvFont(FontFace.HersheyPlain, 0.5, 1);
    private Random random = new Random(4); // determined by fair dice throw; guaranteed to be random

    /// <summary> center of grain </summary>
    public CvPoint2D32f Position { get; private set; }
    /// <summary> Width of grain (always bigger than height)</summary>
    public float Width { get; private set; }
    /// <summary> Height of grain (always smaller than width)</summary>
    public float Height { get; private set; }

    public float MinorRadius { get { return this.Height / 2; } }
    public float MajorRadius { get { return this.Width / 2; } }
    public double Angle { get; private set; }
    public double AngleRad { get { return this.Angle * Math.PI / 180; } }

    public int Index { get; set; }
    public bool Converged { get; private set; }
    public int NumIterations { get; private set; }
    public double CircumferenceRatio { get; private set; }
    public int NumPixel { get; private set; }
    public List<EllipsePoint> EdgePoints { get; private set; }
    public double MeanSquaredError { get; private set; }
    public double PixelRatio { get { return this.NumPixel / (Math.PI * this.MajorRadius * this.MinorRadius); } }
    public bool IsTooSmall { get { return this.Width < MIN_WIDTH || this.Height < MIN_HEIGHT; } }

    public Grain(CvPoint2D32f position)
    {
        this.Position = position;
        this.Angle = 0;
        this.Width = 10;
        this.Height = 10;
        this.MeanSquaredError = double.MaxValue;
    }

    /// <summary>  fit a single rice grain of elipsoid shape </summary>
    public void Fit(CvMat img)
    {
        // distance between the sampled points on the elipse circumference in degree
        int angularResolution = 1;

        // how many times did the fitted ellipse not change significantly?
        int numConverged = 0;

        // number of iterations for this fit
        int numIterations;

        // repeat until the fitted ellipse does not change anymore, or the maximum number of iterations is reached
        for (numIterations = 0; numIterations < 100 && !this.Converged; numIterations++)
        {
            // points on an ideal ellipse
            CvPoint[] points;
            Cv.Ellipse2Poly(this.Position, new CvSize2D32f(MajorRadius, MinorRadius), Convert.ToInt32(this.Angle), 0, 359, out points,
                            angularResolution);

            // points on the edge of foregroudn to background, that are close to the elipse
            CvPoint?[] edgePoints = new CvPoint?[points.Length];

            // remeber if the previous pixel in a given direction was foreground or background
            bool[] prevPixelWasForeground = new bool[points.Length];

            // when the first edge pixel is found, this value is updated
            double firstEdgePixelOffset = 200;

            // from the center of the elipse towards the outside:
            for (float offset = -this.MajorRadius + 1; offset < firstEdgePixelOffset + 20; offset++)
            {
                // draw an ellipse with the given offset
                Cv.Ellipse2Poly(this.Position, new CvSize2D32f(MajorRadius + offset, MinorRadius + (offset > 0 ? offset : MinorRadius / MajorRadius * offset)), Convert.ToInt32(this.Angle), 0,
                                359, out points, angularResolution);

                // for each angle
                Parallel.For(0, points.Length, i =>
                {
                    if (edgePoints[i].HasValue) return; // edge for this angle already found

                    // check if the current pixel is foreground
                    bool foreground = points[i].X < 0 || points[i].Y < 0 || points[i].X >= img.Cols || points[i].Y >= img.Rows
                                          ? false // pixel outside of image borders is always background
                                          : img.Get2D(points[i].Y, points[i].X).Val0 > 0;

                    if (prevPixelWasForeground[i] && !foreground)
                    {
                        // found edge pixel!
                        edgePoints[i] = points[i];

                        // if this is the first edge pixel we found, remember its offset. the other pixels cannot be too far away, so we can stop searching soon
                        if (offset < firstEdgePixelOffset && offset > 0) firstEdgePixelOffset = offset;
                    }

                    prevPixelWasForeground[i] = foreground;
                });
            }

            // estimate the distance of each found edge pixel from the ideal elipse
            // this is a hack, since the actual equations for estimating point-ellipse distnaces are complicated
            Cv.Ellipse2Poly(this.Position, new CvSize2D32f(MajorRadius, MinorRadius), Convert.ToInt32(this.Angle), 0, 360,
                            out points, angularResolution);
            var pointswithDistance =
                edgePoints.Select((p, i) => p.HasValue ? new EllipsePoint(p.Value, points[i], this.Position) : null)
                          .Where(p => p != null).ToList();

            if (pointswithDistance.Count == 0)
            {
                Console.WriteLine("no points found! should never happen! ");
                break;
            }

            // throw away all outliers that are too far outside the current ellipse
            double medianSignedDistance = pointswithDistance.OrderBy(p => p.SignedDistance).ElementAt(pointswithDistance.Count / 2).SignedDistance;
            var goodPoints = pointswithDistance.Where(p => p.SignedDistance < medianSignedDistance + 15).ToList();

            // do a sort of ransack fit with the inlier points to find a new better ellipse
            CvBox2D bestfit = ellipseRansack(goodPoints);

            // check if the fit has converged
            if (Math.Abs(this.Angle - bestfit.Angle) < 3 && // angle has not changed much (<3°)
                Math.Abs(this.Position.X - bestfit.Center.X) < 3 && // position has not changed much (<3 pixel)
                Math.Abs(this.Position.Y - bestfit.Center.Y) < 3)
            {
                numConverged++;
            }
            else
            {
                numConverged = 0;
            }

            if (numConverged > 2)
            {
                this.Converged = true;
            }

            //Console.WriteLine("Iteration {0}, delta {1:0.000} {2:0.000} {3:0.000}    {4:0.000}-{5:0.000} {6:0.000}-{7:0.000} {8:0.000}-{9:0.000}",
            //  numIterations, Math.Abs(this.Angle - bestfit.Angle), Math.Abs(this.Position.X - bestfit.Center.X), Math.Abs(this.Position.Y - bestfit.Center.Y), this.Angle, bestfit.Angle, this.Position.X, bestfit.Center.X, this.Position.Y, bestfit.Center.Y);

            double msr = goodPoints.Sum(p => p.Distance * p.Distance) / goodPoints.Count;

            // for drawing the polygon, filter the edge points more strongly
            if (goodPoints.Count(p => p.SignedDistance < 5) > goodPoints.Count / 2)
                goodPoints = goodPoints.Where(p => p.SignedDistance < 5).ToList();
            double cutoff = goodPoints.Select(p => p.Distance).OrderBy(d => d).ElementAt(goodPoints.Count * 9 / 10);
            goodPoints = goodPoints.Where(p => p.SignedDistance <= cutoff + 1).ToList();

            int numCertainEdgePoints = goodPoints.Count(p => p.SignedDistance > -2);
            this.CircumferenceRatio = numCertainEdgePoints * 1.0 / points.Count();

            this.Angle = bestfit.Angle;
            this.Position = bestfit.Center;
            this.Width = bestfit.Size.Width;
            this.Height = bestfit.Size.Height;
            this.EdgePoints = goodPoints;
            this.MeanSquaredError = msr;

        }
        this.NumIterations = numIterations;
        //Console.WriteLine("Grain found after {0,3} iterations, size={1,3:0.}x{2,3:0.}   pixel={3,5}    edgePoints={4,3}   msr={5,2:0.00000}", numIterations, this.Width,
        //                        this.Height, this.NumPixel, this.EdgePoints.Count, this.MeanSquaredError);
    }

    /// <summary> a sort of ransakc fit to find the best ellipse for the given points </summary>
    private CvBox2D ellipseRansack(List<EllipsePoint> points)
    {
        using (CvMemStorage storage = new CvMemStorage(0))
        {
            // calculate minimum bounding rectangle
            CvSeq<CvPoint> fullPointSeq = CvSeq<CvPoint>.FromArray(points.Select(p => p.Point), SeqType.EltypePoint, storage);
            var boundingRect = fullPointSeq.MinAreaRect2();

            // the initial candidate is the previously found ellipse
            CvBox2D bestEllipse = new CvBox2D(this.Position, new CvSize2D32f(this.Width, this.Height), (float)this.Angle);
            double bestError = calculateEllipseError(points, bestEllipse);

            Queue<EllipsePoint> permutation = new Queue<EllipsePoint>();
            if (points.Count >= 5) for (int i = -2; i < 20; i++)
                {
                    CvBox2D ellipse;
                    if (i == -2)
                    {
                        // first, try the ellipse described by the boundingg rect
                        ellipse = boundingRect;
                    }
                    else if (i == -1)
                    {
                        // then, try the best-fit ellipsethrough all points
                        ellipse = fullPointSeq.FitEllipse2();
                    }
                    else
                    {
                        // then, repeatedly fit an ellipse through a random sample of points

                        // pick some random points
                        if (permutation.Count < 5) permutation = new Queue<EllipsePoint>(permutation.Concat(points.OrderBy(p => random.Next())));
                        CvSeq<CvPoint> pointSeq = CvSeq<CvPoint>.FromArray(permutation.Take(10).Select(p => p.Point), SeqType.EltypePoint, storage);
                        for (int j = 0; j < pointSeq.Count(); j++) permutation.Dequeue();

                        // fit an ellipse through these points
                        ellipse = pointSeq.FitEllipse2();
                    }

                    // assure that the width is greater than the height
                    ellipse = NormalizeEllipse(ellipse);

                    // if the ellipse is too big for agrain, shrink it
                    ellipse = rightSize(ellipse, points.Where(p => isOnEllipse(p.Point, ellipse, 10, 10)).ToList());

                    // sometimes the ellipse given by FitEllipse2 is totally off
                    if (boundingRect.Center.DistanceTo(ellipse.Center) > Math.Max(boundingRect.Size.Width, boundingRect.Size.Height) * 2)
                    {
                        // ignore this bad fit
                        continue;
                    }

                    // estimate the error
                    double error = calculateEllipseError(points, ellipse);

                    if (error < bestError)
                    {
                        // found a better ellipse!
                        bestError = error;
                        bestEllipse = ellipse;
                    }
                }

            return bestEllipse;
        }
    }

    /// <summary> The proper thing to do would be to use the actual distance of each point to the elipse.
    /// However that formula is complicated, so ...  </summary>
    private double calculateEllipseError(List<EllipsePoint> points, CvBox2D ellipse)
    {
        const double toleranceInner = 5;
        const double toleranceOuter = 10;
        int numWrongPoints = points.Count(p => !isOnEllipse(p.Point, ellipse, toleranceInner, toleranceOuter));
        double ratioWrongPoints = numWrongPoints * 1.0 / points.Count;

        int numTotallyWrongPoints = points.Count(p => !isOnEllipse(p.Point, ellipse, 10, 20));
        double ratioTotallyWrongPoints = numTotallyWrongPoints * 1.0 / points.Count;

        // this pseudo-distance is biased towards deviations on the major axis
        double pseudoDistance = Math.Sqrt(points.Sum(p => Math.Abs(1 - ellipseMetric(p.Point, ellipse))) / points.Count);

        // primarily take the number of points far from the elipse border as an error metric.
        // use pseudo-distance to break ties between elipses with the same number of wrong points
        return ratioWrongPoints * 1000  + ratioTotallyWrongPoints+ pseudoDistance / 1000;
    }

    /// <summary> shrink an ellipse if it is larger than the maximum grain dimensions </summary>
    private static CvBox2D rightSize(CvBox2D ellipse, List<EllipsePoint> points)
    {
        if (ellipse.Size.Width < MAX_WIDTH && ellipse.Size.Height < MAX_HEIGHT) return ellipse;

        // elipse is bigger than the maximum grain size
        // resize it so it fits, while keeping one edge of the bounding rectangle constant

        double desiredWidth = Math.Max(10, Math.Min(MAX_WIDTH, ellipse.Size.Width));
        double desiredHeight = Math.Max(10, Math.Min(MAX_HEIGHT, ellipse.Size.Height));

        CvPoint2D32f average = points.Average();

        // get the corners of the surrounding bounding box
        var corners = ellipse.BoxPoints().ToList();

        // find the corner that is closest to the center of mass of the points
        int i0 = ellipse.BoxPoints().Select((point, index) => new { point, index }).OrderBy(p => p.point.DistanceTo(average)).First().index;
        CvPoint p0 = corners[i0];

        // find the two corners that are neighbouring this one
        CvPoint p1 = corners[(i0 + 1) % 4];
        CvPoint p2 = corners[(i0 + 3) % 4];

        // p1 is the next corner along the major axis (widht), p2 is the next corner along the minor axis (height)
        if (p0.DistanceTo(p1) < p0.DistanceTo(p2))
        {
            CvPoint swap = p1;
            p1 = p2;
            p2 = swap;
        }

        // calculate the three other corners with the desired widht and height

        CvPoint2D32f edge1 = (p1 - p0);
        CvPoint2D32f edge2 = p2 - p0;
        double edge1Length = Math.Max(0.0001, p0.DistanceTo(p1));
        double edge2Length = Math.Max(0.0001, p0.DistanceTo(p2));

        CvPoint2D32f newCenter = (CvPoint2D32f)p0 + edge1 * (desiredWidth / edge1Length) + edge2 * (desiredHeight / edge2Length);

        CvBox2D smallEllipse = new CvBox2D(newCenter, new CvSize2D32f((float)desiredWidth, (float)desiredHeight), ellipse.Angle);

        return smallEllipse;
    }

    /// <summary> assure that the width of the elipse is the major axis, and the height is the minor axis.
    /// Swap widht/height and rotate by 90° otherwise  </summary>
    private static CvBox2D NormalizeEllipse(CvBox2D ellipse)
    {
        if (ellipse.Size.Width < ellipse.Size.Height)
        {
            ellipse = new CvBox2D(ellipse.Center, new CvSize2D32f(ellipse.Size.Height, ellipse.Size.Width), (ellipse.Angle + 90 + 360) % 360);
        }
        return ellipse;
    }

    /// <summary> greater than 1 for points outside ellipse, smaller than 1 for points inside ellipse </summary>
    private static double ellipseMetric(CvPoint p, CvBox2D ellipse)
    {
        double theta = ellipse.Angle * Math.PI / 180;
        double u = Math.Cos(theta) * (p.X - ellipse.Center.X) + Math.Sin(theta) * (p.Y - ellipse.Center.Y);
        double v = -Math.Sin(theta) * (p.X - ellipse.Center.X) + Math.Cos(theta) * (p.Y - ellipse.Center.Y);

        return u * u / (ellipse.Size.Width * ellipse.Size.Width / 4) + v * v / (ellipse.Size.Height * ellipse.Size.Height / 4);
    }

    /// <summary> Is the point on the ellipseBorder, within a certain tolerance </summary>
    private static bool isOnEllipse(CvPoint p, CvBox2D ellipse, double toleranceInner, double toleranceOuter)
    {
        double theta = ellipse.Angle * Math.PI / 180;
        double u = Math.Cos(theta) * (p.X - ellipse.Center.X) + Math.Sin(theta) * (p.Y - ellipse.Center.Y);
        double v = -Math.Sin(theta) * (p.X - ellipse.Center.X) + Math.Cos(theta) * (p.Y - ellipse.Center.Y);

        double innerEllipseMajor = (ellipse.Size.Width - toleranceInner) / 2;
        double innerEllipseMinor = (ellipse.Size.Height - toleranceInner) / 2;
        double outerEllipseMajor = (ellipse.Size.Width + toleranceOuter) / 2;
        double outerEllipseMinor = (ellipse.Size.Height + toleranceOuter) / 2;

        double inside = u * u / (innerEllipseMajor * innerEllipseMajor) + v * v / (innerEllipseMinor * innerEllipseMinor);
        double outside = u * u / (outerEllipseMajor * outerEllipseMajor) + v * v / (outerEllipseMinor * outerEllipseMinor);
        return inside >= 1 && outside <= 1;
    }

    /// <summary> count the number of foreground pixels for this grain </summary>
    public int CountPixel(CvMat img)
    {
        // todo: this is an incredibly inefficient way to count, allocating a new image with the size of the input each time
        using (CvMat mask = new CvMat(img.Rows, img.Cols, MatrixType.U8C1))
        {
            mask.SetZero();
            mask.FillPoly(new CvPoint[][] { this.EdgePoints.Select(p => p.Point).ToArray() }, CvColor.White);
            mask.And(img, mask);
            this.NumPixel = mask.CountNonZero();
        }
        return this.NumPixel;
    }

    /// <summary> draw the recognized shape of the grain </summary>
    public void Draw(CvMat img, CvColor color)
    {
        img.FillPoly(new CvPoint[][] { this.EdgePoints.Select(p => p.Point).ToArray() }, color);
    }

    /// <summary> draw the contours of the grain </summary>
    public void DrawContour(CvMat img, CvColor color)
    {
        img.DrawPolyLine(new CvPoint[][] { this.EdgePoints.Select(p => p.Point).ToArray() }, true, color);
    }

    /// <summary> draw the best-fit ellipse of the grain </summary>
    public void DrawEllipse(CvMat img, CvColor color)
    {
        img.DrawEllipse(this.Position, new CvSize2D32f(this.MajorRadius, this.MinorRadius), this.Angle, 0, 360, color, 1);
    }

    /// <summary> print the grain index and the number of pixels divided by the average grain size</summary>
    public void DrawText(double averageGrainSize, CvMat img, CvColor color)
    {
        img.PutText(String.Format("{0}|{1:0.0}", this.Index, this.NumPixel / averageGrainSize), this.Position + new CvPoint2D32f(-5, 10), font01, color);
    }

}

I am a little embarrassed with this solution because a) I am not sure whether it is within the spirit of this challenge, and b) it is too big for a codegolf answer and lacks the elegance of the other solutions.
On the other hand, I am quite happy with the progress I achieved in labeling the grains, not merely counting them, so there is that.

You know you can reduce that code length by magnitudes by using smaller names and applying some other golfing techniques ;)

Probably, but I did not want to further obfuscate this solution. It is too obfuscated for my tastes as it is :)

+1 for the effort and because you are the only one which find a way to display individually each grain. Unfortunately code is a little bloated and rely on lot on hardcoded constants. I would be curious to see how the scanline algorithm i wrote perform on this (on the invidual colored grains).

I really think that this is the right approach for this type of problem(+1 for you), but one thing I wonder, why do you "choose 10 random edge pixels", I would think that you would get better performance if you picked the edge points with the lowest number of nearby edge points(ie parts that stick out), I would think(theoretically) this would eliminate the "easiest" grains first, have you considered this?

I have thought of it, but not tried it yet. The '10 random starting position' was a late addition, which was easy to add and easy to parallelize. Before that, 'one random starting position' was much better than 'always the top left corner'. The danger of choosing the starting positions with the same strategy each time is that when I remove the best fit, the other 9 will probably be chosen again next time, and over time the worst of those starting positions will stay behind and get chosen again and again. A part that sticks out may just be the remains of an incompletely removed previous grain.

I like your answer but you are right it overfits. That's the problem of the challenge though and not your fault.  I don't know how much stomach PPCG has for turning into a mini kaggle type forum.

C++, OpenCV, score: 9
Basic idea of my method is quite simple - try to erase single grains(and "double grains" - 2 (but not more!)  grains, close to each other) from image and then count rest using method based on area (like Falko, Ell and belisarius). Using this approach is a bit better than standard "area method", because it is easier to find good averagePixelsPerObject value.
(1st step)First of all we need to use Otsu binarization on S channel of image in HSV. The next step is using dilate operator to improve quality of extracted foreground. Than we need to find contours. Of course some contours are not grains of rice - we need to delete too small contours (with area smaller then averagePixelsPerObject/4. averagePixelsPerObject is 2855 in my situation). Now finally we can start counting grains :) (2nd step) Finding single and double grains is quite simple - just look in contours list for contours with area within specific ranges - if the contour area is in range, delete it from list and add 1 (or 2 if it was "double" grain) to grains counter. (3rd step) The last step is of course dividing area of remaining contours by averagePixelsPerObject value and add result to grains counter.
Images (for image F.jpg) should show this idea better than words:
1st step (without small contours(noise)):

2nd step - only simple contours:

3rd step - remaining contours:

Here is the code, it's rather ugly, but should work without any problem. Of course OpenCV is required.
#include "stdafx.h"

#include <cv.hpp>
#include <cxcore.h>
#include <highgui.h>
#include <vector>

using namespace cv;
using namespace std;

//A: 3, B: 5, C: 12, D: 25, E: 50, F: 83, G: 120, H:150, I: 151, J: 200
const int goodResults[] = {3, 5, 12, 25, 50, 83, 120, 150, 151, 200};
const float averagePixelsPerObject = 2855.0;

const int singleObjectPixelsCountMin = 2320;
const int singleObjectPixelsCountMax = 4060;

const int doubleObjectPixelsCountMin = 5000;
const int doubleObjectPixelsCountMax = 8000;

float round(float x)
{
    return x >= 0.0f ? floorf(x + 0.5f) : ceilf(x - 0.5f);
}

Mat processImage(Mat m, int imageIndex, int &error)
{
    int objectsCount = 0;
    Mat output, thresholded;
    cvtColor(m, output, CV_BGR2HSV);
    vector<Mat> channels;
    split(output, channels);
    threshold(channels[1], thresholded, 0, 255, CV_THRESH_OTSU | CV_THRESH_BINARY);
    dilate(thresholded, output, Mat()); //dilate to imporove quality of binary image
    imshow("thresholded", thresholded);
    int nonZero = countNonZero(output); //not realy important - just for tests
    if (imageIndex != -1)
        cout << "non zero: " << nonZero << ", average pixels per object: " << nonZero/goodResults[imageIndex] << endl;
    else
        cout << "non zero: " << nonZero << endl;

    vector<vector<Point>> contours, contoursOnlyBig, contoursWithoutSimpleObjects, contoursSimple;
    findContours(output, contours, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_NONE); //find only external contours
    for (int i=0; i<contours.size(); i++)
        if (contourArea(contours[i]) > averagePixelsPerObject/4.0)
            contoursOnlyBig.push_back(contours[i]); //add only contours with area > averagePixelsPerObject/4 ---> skip small contours (noise)

    Mat bigContoursOnly = Mat::zeros(output.size(), output.type());
    Mat allContours = bigContoursOnly.clone();
    drawContours(allContours, contours, -1, CV_RGB(255, 255, 255), -1);
    drawContours(bigContoursOnly, contoursOnlyBig, -1, CV_RGB(255, 255, 255), -1);
    //imshow("all contours", allContours);
    output = bigContoursOnly;

    nonZero = countNonZero(output); //not realy important - just for tests
    if (imageIndex != -1)
        cout << "non zero: " << nonZero << ", average pixels per object: " << nonZero/goodResults[imageIndex] << " objects: "  << goodResults[imageIndex] << endl;
    else
        cout << "non zero: " << nonZero << endl;

    for (int i=0; i<contoursOnlyBig.size(); i++)
    {
        double area = contourArea(contoursOnlyBig[i]);
        if (area >= singleObjectPixelsCountMin && area <= singleObjectPixelsCountMax) //is this contours a single grain ?
        {
            contoursSimple.push_back(contoursOnlyBig[i]);
            objectsCount++;
        }
        else
        {
            if (area >= doubleObjectPixelsCountMin && area <= doubleObjectPixelsCountMax) //is this contours a double grain ?
            {
                contoursSimple.push_back(contoursOnlyBig[i]);
                objectsCount+=2;
            }
            else
                contoursWithoutSimpleObjects.push_back(contoursOnlyBig[i]); //group of grainss
        }
    }

    cout << "founded single objects: " << objectsCount << endl;
    Mat thresholdedImageMask = Mat::zeros(output.size(), output.type()), simpleContoursMat = Mat::zeros(output.size(), output.type());
    drawContours(simpleContoursMat, contoursSimple, -1, CV_RGB(255, 255, 255), -1);
    if (contoursWithoutSimpleObjects.size())
        drawContours(thresholdedImageMask, contoursWithoutSimpleObjects, -1, CV_RGB(255, 255, 255), -1); //draw only contours of groups of grains
    imshow("simpleContoursMat", simpleContoursMat);
    imshow("thresholded image mask", thresholdedImageMask);
    Mat finalResult;
    thresholded.copyTo(finalResult, thresholdedImageMask); //copy using mask - only pixels whc=ich belongs to groups of grains will be copied
    //imshow("finalResult", finalResult);
    nonZero = countNonZero(finalResult); // count number of pixels in all gropus of grains (of course without single or double grains)
    int goodObjectsLeft = goodResults[imageIndex]-objectsCount;
    if (imageIndex != -1)
        cout << "non zero: " << nonZero << ", average pixels per object: " << (goodObjectsLeft ? (nonZero/goodObjectsLeft) : 0) << " objects left: " << goodObjectsLeft <<  endl;
    else
        cout << "non zero: " << nonZero << endl;
    objectsCount += round((float)nonZero/(float)averagePixelsPerObject);

    if (imageIndex != -1)
    {
        error = objectsCount-goodResults[imageIndex];
        cout << "final objects count: " << objectsCount << ", should be: " << goodResults[imageIndex] << ", error is: " << error <<  endl;
    }
    else
        cout << "final objects count: " << objectsCount << endl;
    return output;
}

int main(int argc, char* argv[])
{
    string fileName = "A";
    int totalError = 0, error;
    bool fastProcessing = true;
    vector<int> errors;

    if (argc > 1)
    {
        Mat m = imread(argv[1]);
        imshow("image", m);
        processImage(m, -1, error);
        waitKey(-1);
        return 0;
    }

    while(true)
    {
        Mat m = imread("images\\" + fileName + ".jpg");
        cout << "Processing image: " << fileName << endl;
        imshow("image", m);
        processImage(m, fileName[0] - 'A', error);
        totalError += abs(error);
        errors.push_back(error);
        if (!fastProcessing && waitKey(-1) == 'q')
            break;
        fileName[0] += 1;
        if (fileName[0] > 'J')
        {
            if (fastProcessing)
                break;
            else
                fileName[0] = 'A';
        }
    }
    cout << "Total error: " << totalError << endl;
    cout << "Errors: " << (Mat)errors << endl;
    cout << "averagePixelsPerObject:" << averagePixelsPerObject << endl;

    return 0;
}

If you want to see results of all steps, uncomment all imshow(..,..) function calls and set fastProcessing variable to false. Images(A.jpg, B.jpg,...) should be in directory images. Alternatively course you can give name of one image as a parameter from command line.
Of course if something is unclear i can explain it and/or provide some images/informations.

C# + OpenCvSharp, score: 71
This is most vexing, I tried to get a solution that actually identifies each grain using watershed, but I just. can't. get. it. to. work.
I settled for a solution that at least separates some individual grains and then uses those grains to estimate the average grain size. However so far I cannot beat the solutions with hardcoded grain size.
So, the main highlight of this solution: it does not presume a fixed pixel size for grains, and should work even if the camera is moved or the type of rice is changed.

A.jpg; number of grains:    3; expected    3; error    0; pixels per grain: 2525,0;
B.jpg; number of grains:    7; expected    5; error    2; pixels per grain: 1920,0;
C.jpg; number of grains:    6; expected   12; error    6; pixels per grain: 4242,5;
D.jpg; number of grains:   23; expected   25; error    2; pixels per grain: 2415,5;
E.jpg; number of grains:   47; expected   50; error    3; pixels per grain: 2729,9;
F.jpg; number of grains:   65; expected   83; error   18; pixels per grain: 2860,5;
G.jpg; number of grains:  120; expected  120; error    0; pixels per grain: 2552,3;
H.jpg; number of grains:  159; expected  150; error    9; pixels per grain: 2624,7;
I.jpg; number of grains:  141; expected  151; error   10; pixels per grain: 2697,4;
J.jpg; number of grains:  179; expected  200; error   21; pixels per grain: 2847,1;
total error: 71

My solution works like this:
Separate the foreground by transforming the image to HSV and applying Otsu thresholding on the saturation channel. This is very simply, works extremely well, and I would recommend this for everyone else who wants to try this challenge:
saturation channel                -->         Otsu thresholding

 -->
This will cleanly remove the background.
I then additionally removed the grain shadows from the foreground, by applying a fixed threshold to the value-channel. (Not sure if that actually helps much, but it was simple enough to add)

Then I apply a distance transform on the foreground image.

and find all local maxima in this distance transform.
This is where my idea breaks down. to avoid getting mutiple local maxima within the same grain, I have to filter a lot. Currently I keep only the strongest maximum within a 45 pixel radius, which means not every grain has a local maximum. And I don't really have a justification for the 45 pixel radius, it was just a value that worked.

(as you can see, those are not nearly enough seeds to account for each grain)
Then I use those maxima as seeds for the watershed algorithm:

The results are meh. I was hoping for mostly individual grains, but the clumps are still too big.
Now I identify the smallest blobs, count their average pixel size, and then estimate the number of grains from that. This is not what I planned to do at the start, but this was the only way to salvage this.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using OpenCvSharp;

namespace GrainTest2
{
    class Program
    {
        static void Main(string[] args)
        {
            string[] files = new[]
                               {
                                   "sourceA.jpg", "sourceB.jpg", "sourceC.jpg",
                                   "sourceD.jpg", "sourceE.jpg", "sourceF.jpg",
                                   "sourceG.jpg", "sourceH.jpg", "sourceI.jpg", "sourceJ.jpg",
                               };
            int[] expectedGrains = new[]{3, 5, 12, 25, 50, 83, 120, 150, 151, 200,};

            int totalError = 0;
            int totalPixels = 0;

            for(int fileNo = 0;fileNo markers = new List();
                    using (CvMemStorage storage = new CvMemStorage())
                    using (CvContourScanner scanner = new CvContourScanner(localMaxima, storage, CvContour.SizeOf, ContourRetrieval.External,ContourChain.ApproxNone))
                    {
                        // set each local maximum as seed number 25, 35, 45,...
                        // (actual numbers do not matter, chosen for better visibility in the png)
                        int markerNo = 20;
                        foreach (CvSeq c in scanner)
                        {
                            markerNo += 5;
                            markers.Add(markerNo);
                            waterShedMarkers.DrawContours(c, new CvScalar(markerNo), new CvScalar(markerNo), 0, -1);
                        }
                    }
                    waterShedMarkers.SaveImage("08-watershed-seeds.png");

                    source.Watershed(waterShedMarkers);
                    waterShedMarkers.SaveImage("09-watershed-result.png");

                    List pixelsPerBlob = new List();

                    // Terrible hack because I could not get Cv2.ConnectedComponents to work with this openCv wrapper
                    // So I made a workaround to count the number of pixels per blob
                    waterShedMarkers.ConvertScale(waterShedThreshold);
                    foreach (int markerNo in markers)
                    {
                        using (CvMat tmp = new CvMat(waterShedMarkers.Rows, waterShedThreshold.Cols, MatrixType.U8C1))
                        {
                            waterShedMarkers.CmpS(markerNo, tmp, ArrComparison.EQ);
                            pixelsPerBlob.Add(tmp.CountNonZero());

                        }
                    }

                    // estimate the size of a single grain
                    // step 1: assume that the 10% smallest blob is a whole grain;
                    double singleGrain = pixelsPerBlob.OrderBy(p => p).ElementAt(pixelsPerBlob.Count/15);

                    // step2: take all blobs that are not much bigger than the currently estimated singel grain size
                    //        average their size
                    //        repeat until convergence (too lazy to check for convergence)
                    for (int i = 0; i  p  Math.Round(p/singleGrain)).Sum());

                    Console.WriteLine("input: {0}; number of grains: {1,4:0.}; expected {2,4}; error {3,4}; pixels per grain: {4:0.0}; better: {5:0.}", file, numGrains, expectedGrains[fileNo], Math.Abs(numGrains - expectedGrains[fileNo]), singleGrain, pixelsPerBlob.Sum() / 1434.9);

                    totalError += Math.Abs(numGrains - expectedGrains[fileNo]);
                    totalPixels += pixelsPerBlob.Sum();

                    // this is a terrible hack to visualise the estimated number of grains per blob.
                    // i'm too tired to clean it up
                    #region please ignore
                    using (CvMemStorage storage = new CvMemStorage())
                    using (CvMat tmp = waterShedThreshold.Clone())
                    using (CvMat tmpvisu = new CvMat(source.Rows, source.Cols, MatrixType.S8C3))
                    {
                        foreach (int markerNo in markers)
                        {
                            tmp.SetZero();
                            waterShedMarkers.CmpS(markerNo, tmp, ArrComparison.EQ);
                            double curGrains = tmp.CountNonZero() * 1.0 / singleGrain;
                            using (
                                CvContourScanner scanner = new CvContourScanner(tmp, storage, CvContour.SizeOf, ContourRetrieval.External,
                                                                                ContourChain.ApproxNone))
                            {
                                tmpvisu.Set(CvColor.Random(), tmp);
                                foreach (CvSeq c in scanner)
                                {
                                    //tmpvisu.DrawContours(c, CvColor.Random(), CvColor.DarkGreen, 0, -1);
                                    tmpvisu.PutText("" + Math.Round(curGrains, 1), c.First().Value, new CvFont(FontFace.HersheyPlain, 2, 2),
                                                    CvColor.Red);
                                }

                            }

                        }
                        tmpvisu.SaveImage("10-visu.png");
                        tmpvisu.SaveImage("10-visu" + file + ".png");
                    }
                    #endregion

                }

            }
            Console.WriteLine("total error: {0}, ideal Pixel per Grain: {1:0.0}", totalError, totalPixels*1.0/expectedGrains.Sum());

        }
    }
}

A small test using a hard-coded pixel-per-grain size of  2544.4 showed a total error of 36, which is still bigger than most other solutions.

I think you can use threshold (erode operation might be usefull too) with some small value on result of distance transform - this should split some groups of grains into smaller groups (preferably - with only 1 or 2 grains). Than it should be easier to count those lonely grains. Big groups you can count as most of people here - dividing area by average area of single grain.

HTML + Javascript: Score 39
The exact values are:
Estimated | Actual
        3 |      3
        5 |      5
       12 |     12
       23 |     25
       51 |     50
       82 |     83
      125 |    120
      161 |    150
      167 |    151
      223 |    200

It breaks down (isn't accurate) on the larger values.

window.onload = function() {
  var $ = document.querySelector.bind(document);
  var canvas = $("canvas"),
    ctx = canvas.getContext("2d");

  function handleFileSelect(evt) {
    evt.preventDefault();
    var file = evt.target.files[0],
      reader = new FileReader();
    if (!file) return;
    reader.onload = function(e) {
      var img = new Image();
      img.onload = function() {
        canvas.width = this.width;
        canvas.height = this.height;
        ctx.drawImage(this, 0, 0);
        start();
      };
      img.src = e.target.result;
    };
    reader.readAsDataURL(file);
  }

  function start() {
    var imgdata = ctx.getImageData(0, 0, canvas.width, canvas.height);
    var data = imgdata.data;
    var background = 0;
    var totalPixels = data.length / 4;
    for (var i = 0; i < data.length; i += 4) {
      var red = data[i],
        green = data[i + 1],
        blue = data[i + 2];
      if (Math.abs(red - 197) < 40 && Math.abs(green - 176) < 40 && Math.abs(blue - 133) < 40) {
        ++background;
        data[i] = 1;
        data[i + 1] = 1;
        data[i + 2] = 1;
      }
    }
    ctx.putImageData(imgdata, 0, 0);
    console.log("Pixels of rice", (totalPixels - background));
    // console.log("Total pixels", totalPixels);
    $("output").innerHTML = "Approximately " + Math.round((totalPixels - background) / 2670) + " grains of rice.";
  }

  $("input").onchange = handleFileSelect;
}
<input type="file" id="f" />
<canvas></canvas>
<output></output>

Explanation: Basically, counts the number of rice pixels and divides it by the average pixels per grain.

Using the 3-rice image, it estimated 0 for me... :/

@Kroltan Not when you use the full size image.

@Calvin'sHobbies FF36 on Windows gets 0, on Ubuntu gets 3, with the full size image.

I'm suspicious of '2670'. Is that the average pixel size of a grain of rice? Seems a bit restricted ...

@BobbyJack The rice is guaranteed to be at more or less the same scale across images. I see no problems with it.

An explanation would be welcome.

@githubphagocyte - an explanation is quite obvious - if you count all white pixels on result of binarization of image  and divide this number by number of grains in image you will get this result. Of course exact result may differ, because of used binarization method and other stuff (like operations performed after binarization), but as you can see in other answers, it will be in range 2500-3500.

@cyriel the explanation for most answers is obvious to someone. I just meant that an explanation included in the answer would be welcome for those of us for whom it isn't obvious :)

An attempt with php, Not the lowest scoring answer but its fairly simple code
SCORE : 31
<?php
for($c = 1; $c <= 10; $c++) {
  $a = imagecreatefromjpeg("/tmp/$c.jpg");
  list($width, $height) = getimagesize("/tmp/$c.jpg");
  $rice = 0;
  for($i = 0; $i < $width; $i++) {
    for($j = 0; $j < $height; $j++) {
      $colour = imagecolorat($a, $i, $j);
      if (($colour & 0xFF) < 95) $rice++;
    }
  }
  echo ceil($rice/2966);
}

Self scoring
95 is a blue value which seemed to work when testing with GIMP 2966 is average grain size

all_licenses: Creative Commons - Attribution Share-Alike - https://creativecommons.org/licenses/by-sa/4.0/


text:
Python advice: Portability of introspective function call
In Python, you can use the dir function on any object to get a list of the names of its instance functions:
>>> dir('abc')
['__add__', '__class__', '__contains__', '__delattr__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__getslice__', '__gt__', '__hash__', '__init__', '__le__', '__len__', '__lt__', '__mod__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmod__', '__rmul__', '__setattr__','__sizeof__', '__str__', '__subclasshook__', '_formatter_field_name_split', '_formatter_parser', 'capitalize', 'center', 'count', 'decode', 'encode', 'endswith', 'expandtabs', 'find', 'format', 'index', 'isalnum', 'isalpha', 'isdigit', 'islower', 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip', 'partition', 'replace', 'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill']

I'm wondering if this could be a useful golfing technique in a program than calls several lengthily named functions. In such a case, I could create a function selection function F:
F=lambda o,i:eval('o.'+dir(o)[i])

Now suppose I have a string s and I want store the result of capitalizing its first letter in the variable c. Then instead of c=s.capitalize(), I could note that capitalize is at position 33 in the above list and do the following:
s='abc'
c=G(s,33)()

which assigns 'Abc' to c.
My question is whether this is likely to work most of the time. In particular,

Can I always count on the list being lexicographically sorted by ASCII values?
Are there many changes to list of available between minor versions?
Do differences exist between implementations?

Also, has anyone used this before on PPCG?

I've seen similar things done with JavaScript and C#.

You can also do this with builtins: dir(__builtins__). And here's an alternative function: F=lambda o,i:getattr(o,dir(o)[i]).

Also note that depending on which functions you plan on using, you could add the () to the end of F like so: F=lambda o,i:eval('o.'+dir(o)[i])() Then c=F('abc',33) will assign 'Abc' to c.

From the documentation:

The resulting list is sorted alphabetically

As for the differences, I think you will have to check (and specifying in your answer is probably a good idea). There are clear differences between python 2 and 3, for example, __nonzero__ was renamed to __bool__.
I have never heard of any differences between implementations, but I can't find any references on this.
I don't think this has been used before in part because it will rarely save you any characters over doing something like:
F=str.capitalize
s='abc'
c=F(s)

You would need to use several different functions from dir() in order for this to be worth it.

source: Stack Exchange

license: Creative Commons - Attribution Share-Alike - https://creativecommons.org/licenses/by-sa/3.0/

sort: votes


text:
Keep my Trip Cool!
Challenge
Walking around Marks and Spencers, I noticed that they had air conditioning units placed randomly around the shop. Wanting to keep cool, I wondered what was the easiest way to move around the whole shop without being away from an air conditioning unit for too long.
Given a map, you must find a way to travel around the whole map keeping the distance from an air conditioning unit as short as possible (even if the AC unit is on the other side of a wall).
Map
The map may be supplied in any way you like and uses the following symbols:
+ is a corner of a wall
| is a east/west facing wall
- is a north/south facing wall
X is an air conditioning unit
S is the start and end point

An example map would be:
+------S---+
|   X      |
| ---+-+ X |
|    |X|   |
| ---+ +---+
|   X      |
+----------+

or
+---+--+
| X |  |
|   |  +-----+------+
|   | X      | X    |
|     ---+       |  S
|   |    |  X    |  |
|   |  +-+-------+--+
| X    |
+------+

Travelling around the whole map means passing through every empty space and air conditioner. You cannot travel through a wall and may only travel orthogonally. A map may not always be rectangular.
Keeping the distance as short as possible from an AC unit is the sum over all time steps.
Passing through means entering and leaving.
You may output the path in any way you like. Examples include:

Outputting the map with the path included
Outputting the path as a succession of compass points (e.g. NNSESW)

What does "as short as possible" mean? That we have to find the optimal solution?

@IngoBürk Yes, you do

@BetaDecay And how is that calculated? The maximum distance at any point in time? The sum/average of the distance over all time steps?

It's impossible to understand what the goal of this problem is. If you must visit every square, the maximum distance is a constant.

@feersum Why is that? Couldn't the map layout make it necessary to revisit certain squares and thus giving multiple possibilites for the pathing?

@InvisiblePanda, the maximum is independent of ordering and duplication. I read it as asking for the sum over all time steps, which makes it a non-trivial optimisation problem.

That makes a lot of sense. Can I just claim my question was a trick question? ;)

Is this an optimization problem? If not, there should be some test cases with correct outputs.

Note: |, - and + can really just be one char. They are all really just walls.

Is the start point guaranteed to be at the map's edge or can there be U-shaped maps with the entrance in the center?

@JonathanFrech Yes, it will be at the edge

Could u give us the distance for the only two possible traveling way for the first example?

PowerShell for Windows, 376 367 bytes
Like a lazy man, I don't go to every shelf, I move from air conditioner to air conditioner in a store. I believe I traveled all over the store visiting every air conditioner in it.
$f={param($m,$d,$o=@{})$w=(($l=$m-split"
")|% le*|sort)[-1]
$m=($l|% *ht $w)-join"
"
if(!$o.$m-or$o.$m-ge$d-and$m-match'(?s)X.*S|S.*X'){$o.$m=$d++
$n=-split")X(S )X(.{$w}S S)X( S.{$w})X("|%{sls "(?s)^(.*$_.*)$" -inp $m -a|% m*|%{($_.Groups-replace'S',' ')[1,2]-join'S'}}
$d=(($n+,($m-replace"(?s)(?<=S(.{$w})?) | (?=(.{$w})?S)",'S')*!$n)|%{&$f $_ $d $o}|sort)[0]}+$d}

Try it online!
Unrolled:
$f={
    param($map,$distance,$o=@{})
    $lines = $map-split"`n"
    $width = ($lines|% length|sort)[-1]
    $map = ($lines|% padRight $width)-join"`n"                              # to rectangle

    if(!$o.$map -or $o.$map-ge$distance -and $map-match'(?s)X.*S|S.*X'){
        $o.$map = $distance++                                               # store a map to avoid recalculations
        $n = -split")X(S )X(.{$width}S S)X( S.{$width})X("|%{               # search a nearest X in 4 directions
            select-string "(?s)^(.*$_.*)$" -InputObject $map -AllMatches|% Matches|%{
                ($_.Groups-replace'S',' ')[1,2]-join'S'                     # start a new segment (reset all S and replace the nearest X by S)
            }
        }
        $stepMore = $map-replace"(?s)(?<=S(.{$w})?) | (?=(.{$w})?S)",'S'
        $n += ,$stepMore*!$n                                                # add a step if X was not found
        $distance=($n|%{&$f $_ $distance $o}|sort)[0]                       # recursive repeat
    }

    +$distance
}

all_licenses: Creative Commons - Attribution Share-Alike - https://creativecommons.org/licenses/by-sa/3.0/


text:
How Many Guineas in a Gross of Threepennies?
Until decimalization in 1971, British money was based on dividing the pound into 240 pennies. A shilling was 12 pennies so 20 shillings made a pound. The smallest denomination was the farthing at one fourth of a penny. There were many other denominations and nicknames for coins, which can get quite confusing if you're not used to the system.
Challenge
Write a program or function that can convert (almost) any denomination of old English money to any other. To make it easier for the user you need to support plurals and nicknames.
These are the denominations and their synonymous terms you must support. For convenience their value in farthings leads each line.
1: farthing, farthings
2: halfpence, halfpenny, halfpennies
4: penny, pennies, pence, copper, coppers
8: twopenny, twopennies, twopence, tuppence, half groat, half groats
12: threepence, threepenny, threepennies, threepenny bit, threepenny bits, thruppence, thrupenny, thrupennies, thrupenny bit, thrupenny bits
16: groat, groats
24: sixpence, sixpenny, sixpennies, sixpenny bit, sixpenny bits, tanner, tanners
48: shilling, shillings, bob
96: florin, florins, two bob bit, two bob bits
120: half crown, half crowns
240: crown, crowns
480: half sovereign, half sovereigns
504: half guinea, half guineas
960: pound, pounds, pounds sterling, sovereign, sovereigns, quid, quids
1008: guinea, guineas

(I am not British, this list is by no means authoritative but it will suffice for the challenge.)
Via stdin or function argument you should take in a string of the form
[value to convert] [denomination 1] in [denomination 2]

and return or print
[value to convert] [denomination 1] is [converted value] [denomination 2]

where [converted value] is [value to convert] units of denomination 1 converted to denomination 2.
The [value to convert] and [converted value] are positive floats. In the output both should be rounded or truncated to 4 decimal places. If desired you may assume [value to convert] always has a decimal point and zero when input (e.g. 1.0 instead of 1).
Denominations 1 and 2 may be any two terms from the list above. Don't worry about whether they're plural or not, treat all denominations and synonyms the same. You may assume the input format and denominations are always valid.
Examples
1 pounds in shilling → 1 pounds is 20 shilling
(1.0000 pounds is 20.0000 shilling would be fine)
0.6 tuppence in tanner → 0.6 tuppence is 0.2 tanner
24 two bob bits in pounds sterling → 24 two bob bits is 2.4 pounds sterling
144 threepennies in guineas → 144 threepennies is 1.7143 guineas
Scoring
The shortest code in bytes wins.

"pennies" is only ever used to refer to a number of coins, not an amount of money.

Nit-pick:  Post decimalisation, the plural of quid is quid.  Most likely this would have been the same with the old money.  Example: Five quid a pint! Cor blimey guvnor.  Exception: quids-in

I would probably mess alot of people up to require them to include "ha'penny".

I've never heard a ha'penny called anything but a ha'penny, @kaine. As in http://en.wikipedia.org/wiki/Ha%27penny_Bridge. Of course, I'm too young meself to have heard it too often in speech, but the apostrophe seems standard in writing.

Pyth, 146 145
K4J24L?*y>b-5>b\t?2>b\t.5}<b2"hatw"@[1K8K12K16J48J1008*JT96*2J960)xc"fapetucothengrsishtagucrflbo"2<b2AGHcz" in"++G%" is %0.4f"*vhcGdcyjdtcGdytHH

More readable (newlines and indents must be removed to run):
K4J24
L?*y>b-5>b\t?2>b\t.5
  }<b2"hatw"
  @[1K8K12K16J48J1008*JT96*2J960)
   xc"fapetucothengrsishtagucrflbo"2<b2
AGHcz" in"
++G
  %" is %0.4f"
   *vhcGdcyjdtcGdytH
 H

Update: Turns out, it's 1 character shorter (no space needed) to chop up the string into a list of 2 character strings before running the string index operation. /x"string"<b2 2 -> xc"string"2<b2. Nothing else needs to be changed.
How it works:

This uses @xnor's approach of looking up the value of currency using its first two letters, as well as the trick of detecting the initial half or two, removing it and calling the function again.

To lookup the value of the first two characters, it finds the location of the first two letters of the currency in a string, then divides by 2 and takes the value at that index in the list. This is much shorter than an dict in pyth.

Uses the fact that x (find within string) returns -1 on failure to avoid putting po (pounds) qu (quid) or so (sovereigns) in the string, and simply return the last element of the list, 960, by default.

By rearranging the order of currencies in the lookup system and initializing carefully, with K4 and J24, all spaces that would have been needed to separate numbers in the list were removed.

Uses pyth's dual assignment operator, A, on the input split on  in to get the beginning and end of the input in separate variables.

Does essentially the same lookup at the end, though pyth has no .split(_,1), so it is somewhat more cumbersome.

Examples:
$ pyth programs/currency.pyth <<< '5 florins in half guineas'
5 florins is 0.9524 half guineas

$ pyth programs/currency.pyth <<< '0.4 quid in sixpenny bits'
0.4 quid is 16.0000 sixpenny bits

I give up... ;)

I didn't know < and > worked as string/list slice operators; that's much, much better than taking the head or end of a chop :)

@FryAmTheEggman Looks like that was missing from the documentation as well - I've added it.

I should probably read through macros.py more carefully :)

Ruby, 345 306 302 288 287 278 273 253 252 242 232 221 202 190 bytes
f=->s{" !#+/7OďǿȗϟЏ'"[%w{fa fp ^pe|co r..p ^gr x|ta sh|^b fl|b f.c ^c f.s .gu d|v ^g .}.index{|k|s[/#{k}/]}].ord-31}
$><<gets.sub(/ (.+ i)n /){" #{r=$1}s %0.4f ".%$`.to_f/f[$']*f[r]}

Takes input from STDIN and prints to STDOUT.
I'm using short regular expressions to match only the desired denominations for each value. There are two arrays, one with regexes and one with values, at corresponding indices. The regex array is a space delimited array literal, and the value array is packed into a string of UTF-8 characters.
I'm selecting the index into the values by searching for a regex that matches each denomination. I'm also defaulting to the tuppence/half-groat case (value 8), because that required the longest regex. Likewise, some of the patterns assume that other values have already been matched by earlier patterns, so each regex only distinguishes the desired value only from the remaining ones. Using this, I could probably shave off another couple of bytes by rearranging the order of the denominations.
Thanks to Ventero for helping me beat Pyth making it shorter!

It's the regex matching (s[k]) that overwrites $1 etc. You can save a few characters by moving the map block into a lambda and calling that directly in the last line (which also allows you to drop the assignments for $1 and $2). Also .index is shorter than .find_index.

@Ventero Ah, that makes sense. Thank you!

Regexp.new k → /#{k}/ and $><<gets.sub(/foo/){a=$3;...} → gets[/foo/];a=$3;puts... for a total of 221. And you can of course use the old trick of packing the int array into a string (using .pack("U*")) and then indexing into that string. Should get you down to 195 chars/200 bytes.

Even better: a=gets[/foo/,3]

@Ventero Thanks a lot. I ended up with 196/202, because I added an offset to the char codes to avoid unprintable ASCII. Still shorter than Pyth. ;)

Turns out .sub is actually shorter when utilizing the fact that we don't have to include the full input string in the match: $><<gets.sub(/ (.+ i)n /){" #{r=$1}s %0.4f ".%$`.to_f/f[$']*f[r]} (This now also uses %0.4f instead of .round 4, as most other answers do)

@Ventero Ah, I keep forgetting about $\`` and $'`. Will test and update later.

Python 3: 264 239 chars
f=lambda c:c[:2]in"hatw"and f(c[5-(c>'t'):])*2/4**(c<'t')or[1,4,4,4,8,12,16,24,24,48,48,96,240,1008,960]['fapecoentuthgrsitashboflcrgu'.find(c[:2])//2]
a,b=input().split(" in ")
x,c=a.split(" ",1)
print(a,"is %0.4f"%(eval(x)*f(c)/f(b)),b)

The function f gets the shilling value of the currency-string c by fingerprinting the first two letters using the dictionary by finding them in a string. The prefixes "half" and "two" are detected and accounted for by chopping the prefix and the space and applying a multiplier. Since "halfpenny" lacks a space after "half", this results in "enny", but that's handled with a fictional "en" entry.
Thanks to @isaacg and @grc for lots of improvement on the dictionary lookup.

I knew it could be done :) I'm also extremely embarrassed that I didn't know you could define a dictionary like that... :S

@FryAmTheEggman I didn't you could define dictionaries via keywords either until I saw it used in an answer on this site. The things you learn golfing...

I made a Pyth version of this and got 207 chars. Would you rather I post it here for you to add, or post a community wiki answer?

+1 for that 2/4**(c<'t') part.

You can save 13 characters by using .get(c[:2],960) to lookup the value from the dictionary and omitting the po=960,so=960,qu=960, entries from the dictionary.

This is a little shorter again: [1,4,4,4,8,12,16,24,24,48,48,96,240,1008,960]['fapecoentuthgrsitashboflcrgu'.find(c[:2])//2]

@xnor You appear to be missing the paratheses after input.

@isaacg Thanks, I had forgotten to change that back after making it a variable for testing.

Python 2 - 345 358
s=str.startswith
h='half'
u,v=raw_input().split(' in ')
a,b=u.split(' ',1)
C=dict(fa=1,pe=4,twop=8,tu=8,thr=12,gr=16,si=24,ta=24,sh=48,b=48,fl=96,c=240,po=960,so=960,q=960,gu=1008)
C.update({h+'p':2,h+' gr':8,'two ':96,h+' c':120,h+' s':480,h+' gu':504})
for c in iter(C):
 if s(b,c):k=C[c]
 if s(v,c):f=C[c]
print u+' is %0.4f '%(eval(a)*k/f)+v

Requires input number to be a float in python i.e. 144.1
I think this could be shortened in python 3...
...Confirmed thanks to @xnor. Also confirmed that having a better algorithm matters a lot ;)

I would replace q=raw_input().split(' in ') by q,b=raw_input().split(' in ')

@njzk2 Quite right... I've also used this for the next line, now :)

I think there is a conflict between h+' gr':8 and h+' g':504 depending on who is evaluated first for half groats

@njzk2 that's true... added u to the guinea one...

Haskell - 315 bytes
w x=f(u x)*v(u x)
f=maybe 1 id.l"ha tw tu th si"[0.5,2,2,3,6]
v x@(_:xs)|Just w<-l"bo cr gr gu so co fa fl pe po qu sh ta"[12,60,4,252,240,1,0.25,24,1,240,240,12,6]x=w|True=v xs
l k v x=take 2 x`lookup`zip(words k)v
u=unwords
i s|(n:x,_:t)<-span(/="in")$words s=u$n:x++["is",show$read n*w x/w t]++t
main=interact i

JavaScript (ES5), 344
I=prompt()
n=I.match(/[\d.]+ /)[0]
A=I.slice(n.length).split(" in ")
function m(x){return{fi:1,he:2,p:4,pe:4,cr:4,tn:8,hg:8,tp:12,te:12,g:16,gs:16,sn:24,tr:24,si:48,b:48,fn:96,to:96,hc:120,c:240,cs:240,hs:480,hgtrue:504,ps:960,se:960,q:960,ga:1008}[x[0]+(x[5]||"")+(x[10]=="a"||"")]}
alert(n+A[0]+" is "+(n*m(A[0])/m(A[1])).toFixed(4)+" "+A[1])

I went with a hash function approach... I think I underestimated (relatively) how complex the input processing would be (over the regex approach, that wouldn't care about the number).

Based on @FryAmTheEggMan's answer, with a different way of testing str.startwith:
Python 2: 317
h='half'
C=dict(fa=1,pe=4,twop=8,tu=8,thr=12,gr=16,si=24,ta=24,sh=48,b=48,fl=96,c=240,po=960,so=960,q=960,gu=1008)
C.update({h+'p':2,h+' gr':8,'two ':96,h+' c':120,h+' s':480,h+' gu':504})
u,v=raw_input().split(' in ')
a,b=u.split(' ',1)
s=lambda x:x and C.get(x, s(x[:-1]))
print u+' is %0.4f '%(eval(a)*s(b)/s(v))+v

I think you need to add a trailing space to the print and the formatted string. You can also rewrite the lambda as s=lambda x:x and C.get(x,s(x[:-1]))or 0 to save a character (along with the spaces). This is a pretty neat idea, btw :)

thanks, I fiddled for a while with this ternary notation, which I always find verbose, but did not though of the and/or thing.

Yeah, I learned it here :) I also think you need to make u.split(' ') say u.split(' ',1) for currencies that have spaces, like "half sovereign".

so that's the reason for the , 1!

The ternary x and y or 0 can be shortened in general to x and y, since both evaluate to 0 or equivalently False when x is Falsey.

JavaScript ES6, 264 273
f=t=>{s=t.split(x=' in')
c=d=>{'t0sh|bo0^p|co0f0fp0fl|b b0gu0d|v0wn0gr0f g|t..?p0f s0f gu0f c0x|an'.split(0).map((e,i)=>{v=s[d].match(e)?[12,48,4,1,2,96,1008,960,240,16,8,480,504,120,24][i]:v})
return v}
return s.join(' is '+~~(1e4*t.split(' ')[0]*c(0)/c(1))/1e4)}

t=prompt()
s=t.split(x=' in')
c=function(d){'t0sh|bo0^p|co0f0fp0fl|b b0gu0d|v0wn0gr0f g|t..?p0f s0f gu0f c0x|an'.split(0).map(function(e,i){v=s[d].match(e)?[12,48,4,1,2,96,1008,960,240,16,8,480,504,120,24][i]:v})
return v}
alert(s.join(' is '+~~(1e4*t.split(' ')[0]*c(0)/c(1))/1e4))

This gets the value of each currency by checking it against various regexes, starting with the broadest /t/; the value is overwritten if another match is encountered. There might be a way to shave off a couple bytes by reordering the regex string. You can test it using the snippet above (It is formatted only to use dialog boxes and remove ES6 arrow functions so everyone can test the code easily). Thanks to Alconja for the suggestions.

You can trim 2 chars by using 't0sh|bo0^p....'.split(0), 4 more by using .map instead of .forEach and 3 more by calling c(0) and c(1) and doing s[d].match


text:
A Good Time to Refuse
The Setup
Suppose you're given n fuses, with 1 ≤ n ≤ 5, each of which is a meter long, and where each fuse has an associated burn rate of N meters per D hours.
A fuse can be lit at one or both ends, subsequently extinguished at one or both ends, relit, re-extinguished, etc., as many times as needed until the fuse is fully consumed. You are able to light and extinguish fuses instantaneously, and you can observe the exact instant a fuse is fully consumed (burnt up).
A fuse cannot be cut nor can it be lit anywhere except at its ends.
Such a setup allows for an infinitely accurate timing system, by measuring the time between any two fuse lighting/consumption events. For example, given two fuses with a burn rate of 1 meter per hour, you can measure exactly 45 minutes (3/4 hours) by

simultaneously: lighting the first fuse at both ends, lighting the second fuse at one end, and marking the start of your time interval
lighting the second end of the second fuse at the instant the first fuse is consumed (30 minutes later)
marking the end of your time interval at the instant the second fuse is consumed (15 minutes later)

The Challenge
Given a fractional number of hours t, and a set of n fractions representing exact burn rates of n fuses, write a program or function that outputs/returns a truthy value if t hours can be precisely measured through systematic burning of the fuses, or a falsy value otherwise.
The input to the program can be any of the following:

command-line arguments of the form TN/TD N1/D1 N2/D2 N3/D3 ...
a string of the form TN/TD N1/D1 N2/D2 N3/D3 ... read from stdin or equivalent
a string of the form TN/TD N1/D1 N2/D2 N3/D3 ... passed as a function argument
an array of strings ["TN/TD", "N1/D1", "N2/D2", "N3/D3", ...] passed as a function argument

In all cases t = TN/TD, where TN,TD ∈ [1,10000].
Likewise, in all cases: burn rate for fuse i = Ni /Di = N<i>/D<i>, where N<i>,D<i> ∈ [1,10] ∀ i.
You may assume there will always be between 1 and 5 fuses (inclusive), and that all inputs are valid and in-range. You may also assume that all input fractions are given in lowest terms.
You may not use floating point numbers with fractional components for this challenge. That is, if you use floating point numbers anywhere in your application, they may only take on integral values with zero fractional component.
Scoring
This is a code-golf challenge, hence the shortest compliant submission in bytes will be awarded the win.

Example Inputs/Outputs
input:  29/6 3/2 2/3 3/5 3/7 7/5
output: true

One solution:
  - light both ends of fuse 1, mark start of interval
  - on fuse 1 consumption: light both ends of fuse 2, light one end of fuse 5
  - on fuse 5 consumption: extinguish one end of fuse 2, light both ends of fuse 3,
    light both ends of fuse 4
  - on fuse 2 consumption: extinguish one end of fuse 3, extinguish both ends of
    fuse 4
  - on fuse 3 consumption: relight one end of fuse 4
  - on consumption of fuse 4: mark end of interval (29/6 hours)

input:  2/1 3/1 5/1 7/1
output: false

input:  5/1 6/1 1/6 9/1 1/9
output: true

One solution:
  - light fuse 1 at one end, light fuse 2 at both ends, light fuse 4 at both ends
  - on fuse 1 consumption: extinguish one end of fuse 2, mark start of interval
  - on fuse 4 consumption: relight one end of fuse 2
  - on fuse 2 consumption: mark end of interval (5 hours)

Happy fusing! :)

@MartinBüttner I would guess it would be the floating point number restriction.

@MartinBüttner I agree it isn't a restriction on the source code. I don't think [restricted-source] fits this question as it currently stands.

@chilemagic: I wanted to draw attention to the fact that floating point logic couldn't be used, but if the consensus is that it's not a proper use of the tag, I'll strip it.

The test cases are too big :)

@feersum: How so? I programmed a Java-based solver before posting the challenge to make sure there were no obvious loopholes, and to come up with some interesting test cases. The solver runs in a matter of milliseconds for the given test cases. Would limiting n <= 4 help?

Lol, I'm using an O( (n!)^3 ) algorithm for golfing purposes.

@feersum: lol Um... yeah. Tell you what: if it can run the three test inputs in a few hours or less, submit it and it meets the spec as far as I'm concerned.

Python 2, 305
This is the golfed version. It is practically unusable for n > 3, as the time (and space) complexity is like 3n2... actually that may be way too low for the time. Anyway, the function accepts a list of strings.
def f(i):
 Z=range;r=map(__import__('fractions').Fraction,i);R=r[1:];n=len(R);L=[[[1]*n,[0]]];g=0
 for m,p in L:
  for d in([v/3**i%3for i in Z(n)]for v in Z(3**n)):
    try:x=min(m[i]/R[i]/d[i]for i in Z(n)if m[i]*d[i]>0);L+=[[[m[i]-x*R[i]*d[i]for i in Z(n)],[p[0]+x]+p]]
    except:g|=p[0]-r[0]in p
 return g

A slightly optimized version can finish the test cases in a couple of minutes. It might still be slow for an impossible n=5 case though.
def fLessslow(i):
 Z=range
 r=map(__import__('fractions').Fraction,i)
 R=r[1:]
 n=len(R)
 L=[((1,)*n,(0,))]
 ls = set(L)
 for m,p in L:
  if p[0]-r[0]in p: return 1
  for d in([v/3**i%3 for i in Z(n)]for v in Z(3**n)):
   if any(d[i] and m[i]<=0 for i in Z(n)):continue
   try:
    x=min(m[i]/R[i]/d[i]for i in Z(n)if m[i]*d[i]>0)
    thing = (tuple(m[i]-x*R[i]*d[i]for i in Z(n)),(p[0]+x,)+p)
    if thing not in ls:L+=[thing];ls.add(thing)
   except:5
 return 0

print fLessslow('5/1 6/1 1/6 9/1 1/9'.split())
print fLessslow('29/6 3/2 2/3 3/5 3/7 7/5'.split())

Nice, 8 upvotes for a buggy code: calling the function with the example in the description:
print f('3/4 1/1 1/1'.split())
returns 0, though as the description says, it is solvable.

@Jakube Thanks for testing...it is very rare on this site! It is fixed now; I forgot in one place to divide by the factor of 1 or 2 depending on how many ends of the rope are lit.

Python 2, 331
It's a little bit longer than feersum's version, but it's much faster. All testcases together take about 3 seconds on my laptop. A complete search for n=5 takes abount 10 minutes though.
Some of the code is similar to feersum's version, but I didn't deliberately copy any code.
from fractions import*
f=Fraction
r=range
g=lambda x:h(f(x[0]),[1/f(i)for i in x[1:]],[])
def h(t,x,y):
 for i in r(1,3**len(x)):
  c=[[],[],[]]
  for j in r(len(x)):c[i/3**j%3]+=[x[j]]
  n,b,w=c
  m=min(b+[i/2 for i in w])
  if h(t,[i for i in n+[j-m for j in b]+[j-2*m for j in w]if i],[i+m for i in y]+[m]):return True
 return t in y

Usage:
print g('3/4 1/1 1/1'.split())
print g('29/6 3/2 2/3 3/5 3/7 7/5'.split())
print g('2/1 3/1 5/1 7/1'.split())
print g('5/1 6/1 1/6 9/1 1/9'.split())

Explanation:
The lambda expression g does some preprocessing of the input, like converting strings into fractions, seperating goal time from burn rates and calculating the burn times (=1/burn rate).
The function h divides all burn times x into 3 sets n, b and w (n stand for non_burning, b for one_end_burning, and w for both_ends_burning). It iterates over all those arrangements (except the arrangement n=x, b=[], w=[]), determines the fuse with the shortest burn rate (save time in m), and recursively call h with updated burn times. In y I save all possible times someone can measure using the fuses. In the recursive call these values also get updated.
As soon as I find the value, it terminates alle calls with True.

You young Python programmers are spoiled with all your built-in fractions and big integers. Back when I was a young'un, all we had was 1's and 0's, which we had to type in one-at-a-time at a console without a monitor. Sometimes we didn't have 1s.

authors: พงษ์พันธุ์ สอนทุย