# Counting marks on an image

I know Mathematica can do this but I could not come up with the correct search terms to locate a solution.

I want to mark interesting spots (such as compromised cells) in an image by hand and use Mathematica to count the marks.

If I were to produce an image where the marks are colored-filled circles (picking a color that is pretty different to the main colors of the image), how could I count those marks? Ideally, I would like to be able to use a small number of different colors and count them separately in one image, but obviously that is not realy necessary as I can use copies such as marking different layers in Photoshop and rendering multiple images each with only one marking layer.

If you know an working example or the appropriate search terms I would also appreciate that.

### Sample image

This would actually be perfect for my use case. I could activate all layers with marks for Mathematica counting, and I should get perfect counts (the marks cannot intersect because of the way they have been defined and added by hand). On the other hand, when I activate the background layer containing the original image, I would get a nice images for visualization/ publication.

• Can you give a sample image? – J. M. is in limbo Jul 6 '16 at 13:39
• Maybe this can help – yode Jul 6 '16 at 13:40
• When you say "mark points by hand" - do you mean you want mark them on paper using a (physical) pen, or do you mean you want to click locations on screen using your mouse? The former is an image processing problem, the latter is a UI problem. – Niki Estner Jul 6 '16 at 15:02
• I mean I would like to add the marks in Photoshop or any other image processing software and use Mathematica to count the marks. So I just would like to code counting two numbers, number of red disks and number of green disks from the sample image. Alternatively I could count just disks in two different images. The process being: 1. open image from camera in Photoshop 2. add a counting layer in Photoshop 3. add marks manually in Photoshop 4. render only the marking layer or layers depending if I can seperate by color when counting in Mathematica 5. identify the discs in Mathematica – Erik Itter Jul 6 '16 at 15:06
• And for further analysis it would be ideally to have the disks as data when counting such as a list of geometrical objects beining able to access coordinates to work on the distribution - but that would allready be more than I was looking for initially. – Erik Itter Jul 6 '16 at 15:10

Well,I tried to use the orginal pictrue given by the OP, but the image size being too large resulted in the labels being too small. To give an answer that displays better, I'll change to another picture I have used before.

Getting my picture.

img = Import["http://7xpolf.com1.z0.glb.clouddn.com/guazi.jpg"]


Indexing the objects in the image and highlighting the last count.

Show[
img,
Values[ComponentMeasurements[FillingTransform[ChanVeseBinarize[img]], "Centroid"]]
// Graphics[
{MapIndexed[Inset[Style[First @ #2, 18, Yellow], num = #1] &, #],
Opacity[.6], Red, Disk[num, 20]}]&]


• @m_goldberg Thanks for your edit and I hate that English grammar. :) – yode Jul 7 '16 at 8:50

I'm assuming you want to click locations using your mouse, then show colored points over your image and display the number of clicked points so far:

background = ExampleData[{"TestImage", "Tank"}];
DynamicModule[{pts},
pts = {};
Column[
{
ClickPane[Dynamic[HighlightImage[background, pts]],
AppendTo[pts, #] &],
Dynamic[Length[pts]]
}]]

• No I don't, I just need to count the disks (image recognition). I do not want to click anything in Mathematica. The marked Images will be provided, I only need the image recognition for counting the marks not an UI for creating the marks. – Erik Itter Jul 6 '16 at 15:15

If I isolate one marker in your image, I find that it corresponds to 406 pixels. Therefore, instead of counting the number of markers I can count the number of pixels for each color channel and divide it by 406.

That should be the number of markers:

img = Import["http://i.stack.imgur.com/THUjU.jpg"];
{green, red} =
Floor[1/406 Map[Last@First@ImageLevels[Binarize[#]] &,
ColorSeparate[img][[1 ;; 2]]]]

(* ==> {30, 758} *)

• that works, so thanks first. but... I would like a solution a bit more robust against for example using different settings when saving the image choosing the brush to mark and such, therefore I think I'll need some kind of recognition of the discs. – Erik Itter Jul 6 '16 at 17:26
• I tried to understand why you think this is not robust, but can't really see anything wrong with my method, given the information in the question. You stated yourself that the markers don't overlap, so this pixel-counting should always work. Anything else would be overkill if all you need is the total number of markers. My method is very fast for large images, too. – Jens Jul 6 '16 at 18:15
• It ist not robust against the person doing the marking changing image- or brush size or compression settings (where the outermost pixels of the brush could be changed). nevertheless it works so thanks. – Erik Itter Jul 6 '16 at 19:06