Automatically detect - remove shapes on more images (and inpaint them)

Hello David,

Thanks a lot for your reply: you certainly know where the problem lies :slight_smile:

Luckily for me, pretty much all my images have a white background.
Some of them, which I can fix manually, have a black one (because the white would not be fitting)

I suppose that, once the coin in every jpg image is detected, in-painting AND adding automatically the 2 cm graduated scale should be “trivial” :slight_smile:

Btw, many of my images have more perfect “circles” on them (petri dish for plant pathology…) :slight_smile:

Anyway, once again, thanks a lot indeed for your work on G’MIC over the years :slight_smile:

Even if your have other circle-shaped structures in your image, it should not be that hard to avoid detecting them as coins, because of the constraint we can put on the circle radius that should lie within a particular range.

I’m really busy at the moment (I have an audition in Paris tomorrow), but I might try to implement the Circle Hough Transform in G’MIC by the end of May, if I have some time.

I’m pretty sure that it could be a fun challenge for @grosgood or @garagecoder too :wink:

Hello @David_Tschumperle

I’m really busy at the moment (I have an audition in Paris tomorrow), but I might try to implement the Circle Hough Transform in G’MIC by the end of May, if I have some time.

Sure. No problem at all :slight_smile:



It’s a good idea to create this function to detect circles.
I saw that this function existed in the OpenCV library : HoughCircles()
In the examples found on the internet this function is very simple to use.
I await the equivalent in G’MIC with great interest :o)

Hello @samj

I saw that this function existed in the OpenCV library : HoughCircles()

A colleague of mine, told me it could perhaps be available within R as well (

In the long past, 10 years ago or so, at work, we had some petri dish where we need to measure the area of the fungi growing on them.
A researcher wrote a script to detect them from my jpeg pictures and It worked extremely well. Unfortunately, he doesn’t work with us anymore :slight_smile:

CHT - Was reading up on it in the wee hours. Alas! @Silvio_Grosso has shot — what, ten million? — botanicals already with five euro pieces; otherwise suggesting using a square target was on my lips, and the present hough would handle squares in a straightforward way.

Remarks on using matchpatch to follow. Not a total failure, but not 100% success either.

Indeed, matchpatch won’t manage changes in color/intensity/sizes between the actual photograph and the template image very well.
I don’t think it’s a good approach considering the variety of photographs shot by @Silvio_Grosso .

Been a fun evening. Thank you, @Silvio_Grosso !

That was pretty slick.
OOPS! Coin too close to the apple. Can’t do anything about that. Also, had to tweak settings a bit to have success. In the real world, that means manual intervention.
Ah — this was actually a FAIL, because I had to use a lightened version of the “standard euro coin”. In the real world, that also means intervention.
Success. This, of course, was the sweet spot photograph where all the stars align.

This is my script, for the curiosity of it all

matchit : -check ${"is_image_arg\ $1"} -skip ${2=0.25},${3=5},${4=0.25}
   pass$1 1
   =>. patch
   foreach[^patch] {
      =>. unkwn
      pass[-1] 1
      =>. patch
      +matchpatch[unkwn] [patch],{$ps*w#$patch},{$ps*h#$patch},1,20,0,-0.005,1   d ,
      -channels. 2                                                             d ,
      -fill. ">begin(cv=$cutoff*(iM-im);print(cv));i(x,y)<cv?1:0"              d ,
      -erode. $ed
      -dilate. $ed
      -label_fg. 0,1                                                           d ,
      =>. areas
      -repeat {iM#$areas}
	 -eq. {$>+1}
	 -area.. 0,1
	 -if i(#-2,{@0,1})>$ma
      -fill[areas] i(x,y)==$ml
      -circle.. $bc,{1.5*sqrt(w^2+h^2)/2},1,1
      -inpaint_morpho[unkwn] [areas]

And these were the command lines that produced the images above.

 gmic matchit.gmic -input coinplant.jpeg -input coin.png matchit[-2] [-1],0.01,5,0.16   OK
 gmic matchit.gmic -input apple1.jpg     -input coin.png matchit[-2] [-1],0.01,5,0.16   OK
 gmic matchit.gmic -input apple2.jpg     -input coin.png matchit[-2] [-1],0.03,5,0.20   ADJ Coin too close to botanical
 gmic matchit.gmic -input blueberry.jpg  -input coin_light.png matchit[-2] [-1],0.05,5,0.35 ADJ+ADJMSK

So — if I had to intervene to adjust arguments or change the matching patch, I marked it as a ‘FAIL’. So I’m batting about 50% — way too low for the Real World. I’d like to be able to load a hundred images and realize success in about 98% of them. I could tolerate fixing two out of a hundred. But not half. May as well use GIMP. As David could attest, matchpatch plays a statistical game, but matchpatch can’t tolerate modest levels of variance. Loosen restrictions, and bit and pieces of plants look too much like euros. Tighten restrictions, and the euro is never seen. I started looking at CHT at about 11 PM, then dozed off around midnight. I’ll be fine after about 20 ounces of strong coffee…

Onto CHT!!!


Hello @grosgood

@Silvio_Grosso has shot — what, ten million?

Milion not, but thousands is quite likely :slight_smile:

Just to give you an example of a “single” session of a working day:

I always try hard to take only one very best shot per sample in order to reduce the amount of fixing up later (with RawTherapee and GIMP). For instance, I always use a tripod, the lowest amount of ISO etc etc

A former colleague of mine, now retired, told me that “a good photographer must try to learn how to shoot the least amount of pictures per day”.
Usually beginners take a lot of pictures because they are not sure about the final results (and usually have plenty of time to fix them later on…).

Perhaps a simple general rule will cover 98% of cases. For example, “The coin is the smallest circle that is not inside a larger circle, and with a radius that is at least 15 pixels”.

And perhaps another simple rule will cover 98% of the rest.

HA! Economy in expression! How often that crops up in so many endeavours!

The Writer’s Lament: “I am sorry I wrote such a long letter. I did not have time to write a short one.”

Attributed, I believe, to Sun Tzu: “To gain knowledge: add. To gain wisdom: subtract.”

As @David_Tschumperle noted, the better euro coin finder is probably the Circular Hough Transform, a generalization of the linear version already in G’MIC. The major pieces for a generalization are lying around. BUT, even there I foresee difficulties: round petri dishes, round bacterial colonies, round euros. Betwixt Scylla and Charybdis there is but a narrow way…


There is that…

Previous pictures of petri dish were about fungi.

As regards the bacteria, here they are:

For bacteria, which are usually white-yellowish, I usually put a black background.

Erwinia amylovora, in this picture is quite a big problem for plants in the USA too.
Actually, it was “imported”, by accident, in Europe from the USA many years ago and now it is quite established :slight_smile:

That’s why it is good that you are the plant pathologist. me: “bacteria, fungi, whydayawannaknowthediff anyway?”

You probably all found this already but here is a cod e for opencv in C++, java and python :slight_smile:

1 Like

Not to forget Digital Image Processing with C++ written by some guy we see around here and his buddies. Section 6.2, page 128. Hough transform. Go buy the book; the authors have families to feed…

Hahaha so do i… So if the authors agree for a refund i will buy it :wink:

Considering the very low royalties earned on each sale, I’dreally need to sell a lot to be able to support myself!

Perhaps @Silvio_Grosso can help. He seems to have a lot of 0.05 euro coins handy…

…Or maybe just one though.

Hello everyone,

First off, thanks a lot indeed to anyone for your suggestions and input.
In all truth, I have opened this thread mostly out of curiosity because for very important images (e.g. the ones which I e-mail to someone else) I delete the coins manually through GIMP and G’MIC.

Within the mycological group of which I am a member I take several pictures as well.
They are for pleasure. No real work involved :slight_smile:
Over the years, I have adopted a different “setup”: green background and coin of 1 euro :slight_smile:

At present, with G’MIC I am mainly worried about the loss of my Exif data (with the command line interface).
For my personal needs, at work, this is a real deal breaker :frowning:
From what I have gathered, over the years, there is no way to avoid that, right?

I don’t know much about programming (only a tiny bit of SQL) therefore I don’t know whether this loss occurs also with other frameworks (e.g. ImageMagick)

With GIMP and G’MIC (through the GUIs) this data loss does not occur.
All the Exif data are preserved after the G’MIC filter has been applied.