After an evaluation, GNOME has moved from Bugzilla to GitLab. Learn more about GitLab.
No new issues can be reported in GNOME Bugzilla anymore.
To report an issue in a GNOME project, go to GNOME GitLab.
Do not go to GNOME Gitlab for: Bluefish, Doxygen, GnuCash, GStreamer, java-gnome, LDTP, NetworkManager, Tomboy.
Bug 679622 - Possible wrong Otsu (auto) threshold value for binarisation
Possible wrong Otsu (auto) threshold value for binarisation
Status: RESOLVED OBSOLETE
Product: GIMP
Classification: Other
Component: General
2.8.8
Other All
: Normal enhancement
: ---
Assigned To: GIMP Bugs
GIMP Bugs
Depends on:
Blocks:
 
 
Reported: 2012-07-09 11:55 UTC by Vincent
Modified: 2018-05-24 13:15 UTC
See Also:
GNOME target: ---
GNOME version: ---


Attachments
Picture-test to compute Otsu Thresholding (1.14 KB, image/tiff)
2012-07-09 11:55 UTC, Vincent
Details

Description Vincent 2012-07-09 11:55:11 UTC
Created attachment 218327 [details]
Picture-test to compute Otsu Thresholding

Hello,

Gimp use the Otsu method to compute binarization threshold.

If you compute the auto-threshold for the attached picture with Gimp 2.8 (menu "Colors / Threshold - Auto" or "Couleurs / Seuil - Auto in french), Gimp find 185.

If you compute the auto-threshold for the attached picture with ImageJ (menu "Image / Adjust / Threshold - Otsu"), ImageJ find 195.

I think these two threshold (195 and 185) can be improved.

+ Gimp return the threshold "s1" for maximum (GREATER) histogram standard variation.
+ ImageJ return the threshold "s2" for maximum (GREATER OR EQUAL) histogram standard variation.

I think you must compute s1 and s2 and return s = ( s1 + s2 ) / 2

Otsu threshold must be ( 185 + 195 ) / 2 = 190 for the attached picture. Look at the histogram, it is the middle...

(In most cases, s1 = s2 = s...)

Best regards,

Vincent
Comment 1 Vincent 2012-07-09 12:29:43 UTC
Read "standard deviation" ("écart-type au carré" or "variance" in french) instead "standard variation", sorry...
Comment 2 Michael Natterer 2012-07-28 09:47:01 UTC
What is "Otsu"? Are you using 3rd party plugins?
Comment 3 Vincent 2012-08-09 14:06:30 UTC
> Michael Natterer > What is "Otsu"? Are you using 3rd party plugins?

I have read that Otsu was the algorithm used by Gimp to compute binary threshold. This is not a plugin !

Look here for example : http://en.wikipedia.org/wiki/Otsu%27s_method

(I suggest this improvement for this Wikipedia page and for ImageJ too)
Comment 4 Michael Natterer 2012-08-09 14:54:34 UTC
I see. Needs to be checked then.
Comment 5 Vincent 2014-03-11 13:54:22 UTC
Hello,

I'm looking in Gimp 2.8.8 source code and I think I've find the Otsu threshold fonction and a solution :

I'v opened "\gimp-2.8.8\app\base\gimphistogram.c" file.

At the end of the function "gdouble gimp_histogram_get_threshold ( ...", line 404, you can read :
  ...
  for (i = 0; i < maxval; ++i)
    if (chist[i] > 0 && chist[i] < chist_max)
    {
      gdouble bvar;

      bvar = (gdouble) cmom[i] / chist[i];
      bvar -= (cmom_max - cmom[i]) / (chist_max - chist[i]);
      bvar *= bvar;
      bvar *= chist[i];
      bvar *= chist_max - chist[i];

      if (bvar > bvar_max)
      {
        bvar_max = bvar;
	threshold = start + i;
      }
    }

  return threshold;
}

I suggest this following improvement :

	gint     threshold;
	gint     threshold_begin = 127;
	gint     threshold_end = 127;

	...

	bvar = (gdouble) cmom[i] / chist[i];
	bvar -= (cmom_max - cmom[i]) / (chist_max - chist[i]);
	bvar *= bvar;
	bvar *= chist[i];
	bvar *= chist_max - chist[i];

	if (bvar >= bvar_max) 
	{
		threshold_end = start + i;
		if (bvar > bvar_max) 
		{
			threshold_begin = start + i;
		{
		bvar_max = bvar;
	}
  }
  threshold = ( threshold_begin + threshold_end ) / 2;
  return threshold;
}// gimp_histogram_get_threshold
Comment 6 Michael Natterer 2014-04-15 15:33:29 UTC
Can you please turn this into a patch?
Comment 7 Vincent 2014-04-29 14:53:11 UTC
(In reply to comment #6)
> Can you please turn this into a patch?

How can I do a "patch" ?
Do you have a tutorial or some indications for this, please ?
Comment 8 Vincent 2014-04-29 16:05:19 UTC
I've find the official tutorial. I'll try to do it. Thank's.
http://www.gimp.org/bugs/howtos/submit-patch.html
Comment 9 Michael Natterer 2014-04-30 07:14:04 UTC
If you are using git, you simply edit the file and say

git diff >foo.patch

if you use a tarball, you keep the .c file's original around, edit the
file and say

diff -u original.c modified.c >foo.patch
Comment 10 Vincent 2014-05-04 13:20:11 UTC
I've do a patch with Git and your advice.
"git diff" produce the following text-file between the two "------------" lines.

-------------------------------------------------------------------------
diff --git a/app/core/gimphistogram.c b/app/core/gimphistogram.c
index 5143e0e..5913805 100644
--- a/app/core/gimphistogram.c
+++ b/app/core/gimphistogram.c
@@ -781,7 +781,9 @@ gimp_histogram_get_threshold (GimpHistogram        *histogram,
   gdouble              chist_max = 0.0;
   gdouble              cmom_max  = 0.0;
   gdouble              bvar_max  = 0.0;
-  gint                 threshold = 127;
+  gint                 threshold;
+  gint                 threshold_begin = 127;
+  gint                 threshold_end = 127;
 
   g_return_val_if_fail (GIMP_IS_HISTOGRAM (histogram), -1);
 
@@ -848,14 +850,18 @@ gimp_histogram_get_threshold (GimpHistogram        *histogram,
           bvar *= chist[i];
           bvar *= chist_max - chist[i];
 
-          if (bvar > bvar_max)
+          if (bvar >= bvar_max)
             {
+              threshold_end = start + i;
+              if (bvar > bvar_max)
+                {
+                  threshold_begin = start + i;
+                }
               bvar_max = bvar;
-              threshold = start + i;
             }
         }
     }
-
+  threshold = (threshold_begin + threshold_end) / 2;
   return threshold;
 }
-------------------------------------------------------------------------
Comment 11 Michael Natterer 2016-11-01 21:32:35 UTC
Can somebody with a clue please check if that patch makes sense?
Comment 12 GNOME Infrastructure Team 2018-05-24 13:15:45 UTC
-- GitLab Migration Automatic Message --

This bug has been migrated to GNOME's GitLab instance and has been closed from further activity.

You can subscribe and participate further through the new bug through this link to our GitLab instance: https://gitlab.gnome.org/GNOME/gimp/issues/412.