gimp  2.8.14
About: GIMP - The GNU Image Manipulation Program.
  Fossies Dox: gimp-2.8.14.tar.gz  ("inofficial" and yet experimental doxygen-generated source code documentation)  

 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
benchmark-foreground-extract.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 
3 # Foreground Extraction Benchmark
4 # Copyright 2005 Sven Neumann <sven@gimp.org>
5 #
6 # This is a from-scratch implementation of the benchmark proposed in
7 # "GrabCut": interactive foreground extraction using iterated graph
8 # cuts published in the Proceedings of the 2004 SIGGRAPH Conference.
9 #
10 # No guarantee is made that this benchmark produces the same results
11 # as the cited benchmark but the goal is that it does. So if you find
12 # any bugs or inaccuracies in this code, please let us know.
13 #
14 # The benchmark has been adapted work with the SIOX algorithm
15 # (http://www.siox.org). which is (currently) the only
16 # implementation of gimp_drawable_foreground_extract(). If other
17 # implementations are being added, this benchmark should be changed
18 # accordingly.
19 #
20 # You will need a set of test images to run this benchmark, preferably
21 # the original set of 50 images. Some of these images are from the
22 # Berkeley Segmentation Dataset
23 # (http://www.cs.berkeley.edu/projects/vision/grouping/segbench/).
24 # See also http://www.siox.org/details.html for trimaps.
25 #
26 # This program is free software: you can redistribute it and/or modify
27 # it under the terms of the GNU General Public License as published by
28 # the Free Software Foundation; either version 3 of the License, or
29 # (at your option) any later version.
30 #
31 # This program is distributed in the hope that it will be useful,
32 # but WITHOUT ANY WARRANTY; without even the implied warranty of
33 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
34 # GNU General Public License for more details.
35 #
36 # You should have received a copy of the GNU General Public License
37 # along with this program. If not, see <http://www.gnu.org/licenses/>.
38 
39 
40 import os, re, struct, sys, time
41 
42 from gimpfu import *
43 
44 
45 def benchmark (folder, save_output):
46  folder = os.path.abspath (folder)
47  if not os.path.exists (folder):
48  gimp.message("Folder '" + folder + "' doesn't exist.\n")
49  return;
50 
51  total_unclassified = 0
52  total_misclassified = 0
53  total_time = 0.0
54 
55  images = os.path.join (folder, "images")
56  for name in os.listdir (images):
57 
58  try:
59  gimp.delete (image_display)
60  gimp.delete (mask_display)
61  except UnboundLocalError:
62  pass
63 
64  image_name = os.path.join (images, name)
65 
66  # FIXME: improve this!
67  name = re.sub (r'\.jpg$', '', name)
68  name = re.sub (r'\.JPG$', '', name)
69  name = re.sub (r'\.bmp$', '', name)
70 
71  mask_name = os.path.join (folder, "cm_bmp", name + '.png')
72  truth_name = os.path.join (folder, "truth", name + '.bmp')
73 
74  image = pdb.gimp_file_load (image_name, image_name)
75  image_layer = image.active_layer;
76 
77  mask = pdb.gimp_file_load (mask_name, mask_name)
78  convert_grayscale (mask)
79  mask_layer = mask.active_layer;
80 
81  truth = pdb.gimp_file_load (truth_name, truth_name)
82  convert_grayscale (truth)
83  truth_layer = truth.active_layer;
84 
85  unclassified = unclassified_pixels (mask_layer, truth_layer)
86 
87  sys.stderr.write (os.path.basename (image_name))
88 
89  start = time.time ()
90  pdb.gimp_drawable_foreground_extract (image_layer,
91  FOREGROUND_EXTRACT_SIOX,
92  mask_layer)
93  end = time.time ()
94 
95  sys.stderr.write (" ")
96 
97  mask_layer.flush ()
98 
99  # Ignore errors when creating image displays;
100  # allows us to be used without a display.
101  try:
102  image_display = pdb.gimp_display_new (image)
103  mask_display = pdb.gimp_display_new (mask)
104 
105  gimp.displays_flush ()
106  time.sleep (1.0)
107  except:
108  pass
109 
110  gimp.delete (image)
111 
112  misclassified = misclassified_pixels (mask_layer, truth_layer)
113 
114  sys.stderr.write ("%d %d %.2f%% %.3fs\n" %
115  (unclassified, misclassified,
116  (misclassified * 100.0 / unclassified),
117  end - start))
118 
119  total_unclassified += unclassified
120  total_misclassified += misclassified
121  total_time += end - start
122 
123  gimp.delete (truth)
124 
125  if save_output:
126  filename = os.path.join (folder, "output", name + '.png')
127  pdb.gimp_file_save (mask, mask_layer, filename, filename)
128 
129  gimp.delete (mask)
130 
131  # for loop ends
132 
133  try:
134  gimp.delete (image_display)
135  gimp.delete (mask_display)
136  except UnboundLocalError:
137  pass
138 
139  sys.stderr.write ("Total: %d %d %.2f%% %.3fs\n" %
140  (total_unclassified, total_misclassified,
141  (total_misclassified * 100.0 / total_unclassified),
142  total_time))
143 
144 def convert_grayscale (image):
145  if image.base_type != GRAY:
146  pdb.gimp_image_convert_grayscale (image)
147 
148 
149 def unclassified_pixels (mask, truth):
150  (mean, std_dev, median, pixels,
151  count, percentile) = pdb.gimp_histogram (mask, HISTOGRAM_VALUE, 1, 254)
152 
153  return count
154 
155 
156 def misclassified_pixels (mask, truth):
157  image = truth.image
158 
159  copy = pdb.gimp_layer_new_from_drawable (mask, image)
160  copy.name = "Difference"
161  copy.mode = DIFFERENCE_MODE
162 
163  image.insert_layer (copy)
164 
165  # The assumption made here is that the output of
166  # foreground_extract is a strict black and white mask. The truth
167  # however may contain unclassified pixels. These are considered
168  # unknown, a strict segmentation isn't possible here.
169  #
170  # The result of using the Difference mode as done here is that
171  # pure black pixels in the result can be considered correct.
172  # White pixels are wrong. Gray values were unknown in the truth
173  # and thus are not counted as wrong.
174 
175  (mean, std_dev, median, pixels,
176  count, percentile) = pdb.gimp_histogram (image.flatten (),
177  HISTOGRAM_VALUE, 255, 255)
178 
179  return count
180 
181 
182 register (
183  "python-fu-benchmark-foreground-extract",
184  "Benchmark and regression test for the SIOX algorithm",
185  "",
186  "Sven Neumann",
187  "Sven Neumann",
188  "2005",
189  "Foreground Extraction",
190  "",
191  [ (PF_FILE, "image-folder", "Image folder",
192  "~/segmentation/msbench/imagedata"),
193  (PF_TOGGLE, "save-output", "Save output images", False) ],
194  [],
195  benchmark, menu="<Image>/Filters/Extensions/Benchmark")
196 
197 main ()