Count # of Cookie Chocolate Chips

7

2

Note: this is my first post here.

The Challenge

The challenge is to count the number of chocolate chips in a cookie. A cookie in this case is a circle with dots (the chips) in it. Given any square cartoon image of this cookie, you are to write a bot that counts and outputs the number of chocolate chips.

Rules

  • Has to take in an image type of png.
  • It has to be able to take in any file size, as long as it is square.
  • It has to be able to handle both color and B&W images.
  • Output can be in any human-understandable form.
  • The circle will be filled with color and will have an outline.
  • Chips will all be the same size and color.
  • There is only one cookie in the image
  • Chocolate chips may be irregularly spaced, but are perfect circles.
  • You can use a library.
  • There is nothing in the image except the cookie and the chocolate chips. -All other cookies will look like the example images.

This is ; the shortest code gets accepted.

Examples

Cookie 1 => 8 or 1000 (binary)
Cookie 2 => 8 or 1000 (binary)
Cookie 3 => 10 or 1010 (binary)
Cookie 4 => 7 or 0111 (binary)

Cello Guy

Posted 2017-03-30T01:30:48.530

Reputation: 81

4Hello and welcome to PPCG! Please provide some sample cases and consider simplifying the input so it may be more objectively scored (it may be a bit complex to take any kind of hairbrush image - consider creating a criteria). – cole – 2017-03-30T01:34:11.093

1Please add example images – Digital Trauma – 2017-03-30T02:03:32.837

Hello and welcome to PPCG! Currently this question is lacking quite a few details; image-processing challenges need specifications to be objectively scored. Additionally, you are missing a few details; can the bristles be different sizes? How small can they get? Will there be things other than bristles? I would recommend looking at Wheat Wizard's advice and posting in the Sandbox first to get feedback without the risk of losing reputation or getting the answer closed. So sorry, but I voted this to be closed, which means that it's now on hold. Better luck next time, and check out the Sandbox! – HyperNeutrino – 2017-03-30T02:42:36.330

@DigitalTrauma I am working on them right now. I understand your flag and agree with it. Please take it off when I have posted the images, though... – Cello Guy – 2017-03-30T10:12:59.737

@HyperNeutrino I fixed these things. – Cello Guy – 2017-03-30T10:36:47.570

I still see references to bristles. Your cleanup is not complete. – Mego – 2017-03-30T13:46:55.670

This question has been reopened. – HyperNeutrino – 2017-03-30T18:12:32.343

Are the chocolate chips always the same size? – Anthony Pham – 2017-03-30T21:44:30.160

@AnthonyPham yes – Cello Guy – 2017-03-31T00:11:18.430

Anyone going to try something else? – Cello Guy – 2017-05-26T01:13:11.407

Answers

3

Mathematica 134 Bytes

Max@ComponentMeasurements[ArrayComponents[ImageData@ColorQuantize[RemoveAlphaChannel[#,White],4,Dithering->False],2],"Holes"][[All,2]]&

Usage (applied to .png images)

%/@cookies

Output:

{8,8,10,7}

Here's the code to bring in the cookie examples:

cookieURLs={"https://i.stack.imgur.com/UCV1A.png","https://i.stack.imgur.com/5n7AY.png","https://i.stack.imgur.com/ZbZx6.png","https://i.stack.imgur.com/M8jTo.png"};
cookies=Import/@cookieURLs;

I'm sure this can be shortened, but I think it's fairly bullet-proof as is. The original images have many more colors than the eye can see.

Here are the original cookies:

cookieout.gif

Here are the cookies after cleanup to get them into a 4 component array:

ArrayPlot[ArrayComponents[ImageData@ColorQuantize[RemoveAlphaChannel[#,White],4,Dithering->False],2]]&/@cookies//Row

cookieout2

After that, all is needed is to use ComponentMeasurements to count holes. This will always report at least 1. You could have one really big chocolate chip filling the cookie border, so the 0 or 1 case will always be ambiguous.

Kelly Lowder

Posted 2017-03-30T01:30:48.530

Reputation: 3 225

Awesome! Can you provide a link so anyone can run it online? – Cello Guy – 2017-04-22T15:56:35.187