Skip to content

Commit 2da1815

Browse files
authored
Fixed a bug with source image in wx UI
Also cleaned the code to be PEP8
1 parent febe272 commit 2da1815

File tree

1 file changed

+37
-57
lines changed

1 file changed

+37
-57
lines changed

Recipes/SuperpixelPainter.py

+37-57
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from pathlib import Path
66
parentFolder = str(Path(__file__).parent.parent)
77
activate_path = parentFolder + '\\env\\Scripts\\activate_this.py'
8+
89
if os.path.exists(activate_path):
910
exec(open(activate_path).read(), {'__file__': activate_path})
1011
print(f'Aivia virtual environment activated\nUsing python: {activate_path}')
@@ -51,12 +52,6 @@
5152
PIL
5253
wxPython
5354
54-
For Aivia 10.x with embedded python, the guide to install non-standard packages (windows and macOS):
55-
56-
* Open terminal
57-
* Change directory: cd "path/to/Aivia/Python/directory/" (For e.g. C:\Program Files\Leica Microsystems\Aivia 10.5.0\Python)
58-
* Run: python -m pip install <name_of_the_module>
59-
6055
Parameters
6156
----------
6257
Input Image : Aivia channel
@@ -85,19 +80,17 @@ def run(params):
8580
print('read')
8681
if not os.path.exists(image_location):
8782
print(f'Error: {image_location} does not exist')
88-
return;
83+
return
8984

9085
if zCount > 1 or tCount > 1:
9186
print('Currently this only supports 2D images with no time dimension.')
9287
return
9388

94-
95-
image_data=imread(image_location)
89+
image_data = imread(image_location)
9690
mask_data = paint_superpixels(image_location)
9791

98-
9992
mask_data = resize(mask_data, image_data.shape, anti_aliasing=False)
100-
mask_data=img_as_ubyte(mask_data)
93+
mask_data = img_as_ubyte(mask_data)
10194

10295
imsave(result_mask_location, mask_data)
10396
imsave(result_object_location, mask_data)
@@ -121,13 +114,10 @@ def paint_superpixels(image_location):
121114
(N, M) array
122115
Binary mask based on the user's painting.
123116
"""
124-
125-
126117
app = wx.App()
127118
frame = MyFrame(image_location)
128119
app.MainLoop()
129120

130-
131121
return frame.mask[:, :, 0]
132122

133123

@@ -157,67 +147,61 @@ class MyFrame(wx.Frame):
157147
"""
158148

159149
def __init__(self, image_location):
160-
super().__init__(parent=None, title='Superpixel Painter', size = wx.Size( 800,800 ))
161-
150+
super().__init__(parent=None, title='Superpixel Painter', size=wx.Size(800, 800))
162151

163-
164-
image_np=imread(image_location)
152+
image_np = imread(image_location)
165153
frame_size = self.GetSize()
166-
frame_h = (frame_size[0]) *0.7
167-
frame_w = (frame_size[1]) *0.7
168-
image_np_sc = resize(image_np, (frame_h,frame_w), anti_aliasing=False)
154+
frame_h = (frame_size[0]) * 0.7
155+
frame_w = (frame_size[1]) * 0.7
156+
image_np_sc = resize(image_np, (frame_h, frame_w), anti_aliasing=False)
169157
self.W = image_np_sc.shape[0]
170158
self.H = image_np_sc.shape[1]
171159

172160
image = wx.Image(self.W, self.H)
173-
image.SetData(image_np.tobytes())
161+
image.SetData(image_np_sc.tobytes())
174162
wxBitmap = image.ConvertToBitmap()
175163

176-
177164
max_markers = int(0.01 * (self.W*self.H))
178165
self.marker_color = [1.00, 0.73, 0.03]
179166

180167
self.panel = wx.Panel(self)
181168
self.panel.SetBackgroundColour("gray")
182169

183-
184170
self.mainSizer = wx.BoxSizer(wx.VERTICAL)
185171

186172
user_instructions = 'Click left mouse button to paint superpixels. Click right mouse button to erase. ' \
187173
'Middle click to fill a contour. Close the app when finished.'
188174
self.instruction = wx.StaticText(self.panel, label=user_instructions)
189-
self.instruction.SetForegroundColour(wx.Colour(255,255,255))
190-
self.mainSizer.Add(self.instruction, 0,wx.ALL | wx.CENTER, 5)
175+
self.instruction.SetForegroundColour(wx.Colour(255, 255, 255))
176+
self.mainSizer.Add(self.instruction, 0, wx.ALL | wx.CENTER, 5)
191177

192178
self.imageCtrl = wx.StaticBitmap(self.panel, wx.ID_ANY,
193179
wxBitmap)
194180

195181
self.mainSizer.Add(self.imageCtrl, 0, wx.ALL, 5)
196-
self.imageCtrl.SetBitmap(wxBitmap)
197-
182+
self.imageCtrl.SetBitmap(wxBitmap)
198183

199184
self.marker = wx.StaticText(self.panel, label='Marker')
200-
self.marker.SetForegroundColour(wx.Colour(255,255,255))
201-
self.mainSizer.Add(self.marker, 0, wx.ALL , 5)
185+
self.marker.SetForegroundColour(wx.Colour(255, 255, 255))
186+
self.mainSizer.Add(self.marker, 0, wx.ALL, 5)
202187

203-
self.marker_sld = wx.Slider(self.panel, value = 1, minValue = 10, maxValue = max_markers,
204-
style = wx.SL_HORIZONTAL|wx.SL_LABELS)
205-
self.mainSizer.Add(self.marker_sld,1,flag = wx.EXPAND , border = 20)
188+
self.marker_sld = wx.Slider(self.panel, value=1, minValue=10, maxValue=max_markers,
189+
style=wx.SL_HORIZONTAL|wx.SL_LABELS)
190+
self.mainSizer.Add(self.marker_sld, 1, flag=wx.EXPAND, border=20)
206191

207192
self.compactness = wx.StaticText(self.panel, label='Compactness')
208-
self.compactness.SetForegroundColour(wx.Colour(255,255,255))
209-
self.mainSizer.Add(self.compactness, 0, wx.ALL , 5)
210-
self.compactness_float=0.001
193+
self.compactness.SetForegroundColour(wx.Colour(255, 255, 255))
194+
self.mainSizer.Add(self.compactness, 0, wx.ALL, 5)
195+
self.compactness_float = 0.001
211196

212-
213-
self.compactness_sld = wx.Slider(self.panel, value = 1, minValue = 1, maxValue = 10,
214-
style = wx.SL_HORIZONTAL|wx.SL_LABELS)
215-
self.mainSizer.Add(self.compactness_sld,1,flag = wx.EXPAND , border = 20)
197+
self.compactness_sld = wx.Slider(self.panel, value=1, minValue=1, maxValue=10,
198+
style=wx.SL_HORIZONTAL|wx.SL_LABELS)
199+
self.mainSizer.Add(self.compactness_sld, 1, flag=wx.EXPAND, border=20)
216200

217201
self.clear_button = wx.Button(self.panel, wx.ID_ANY, 'Clear', (10, 10))
218-
self.mainSizer.Add(self.clear_button, 0, wx.ALL , 5)
202+
self.mainSizer.Add(self.clear_button, 0, wx.ALL, 5)
219203

220-
self.input_image_np=image_np_sc
204+
self.input_image_np = image_np_sc
221205
self.superpixels = watershed(sobel(self.input_image_np), markers=self.marker_sld.GetValue(),
222206
compactness=self.compactness_sld.GetValue()*self.compactness_float)
223207
self.boundaries = img_as_ubyte(mark_boundaries(self.input_image_np, self.superpixels, color=self.marker_color))
@@ -240,10 +224,8 @@ def __init__(self, image_location):
240224
def wximage_to_numpy(self, image):
241225

242226
arr = np.asarray(image.GetDataBuffer())
243-
image_np = np.copy(np.reshape(arr, (image.GetWidth(), image.GetHeight(),3)))
227+
image_np = np.copy(np.reshape(arr, (image.GetWidth(), image.GetHeight(), 3)))
244228
return image_np
245-
246-
247229

248230
def clear_mask(self, event):
249231
"""
@@ -258,9 +240,8 @@ def update_image(self):
258240
Updates the app's displayed image.
259241
"""
260242

261-
wxBitmap= wx.Image(self.W,self.H, self.display_image)
243+
wxBitmap = wx.Image(self.W, self.H, self.display_image)
262244
self.imageCtrl.SetBitmap(wx.Bitmap(wxBitmap))
263-
264245

265246
def update_superpixels(self, event):
266247
"""
@@ -304,20 +285,19 @@ def flood_fill(self, event):
304285

305286
for i in range(3):
306287
self.mask[:, :, i] = flood_fill(self.mask[:, :, i], seed_point=(event.y, event.x), new_value=255,
307-
tolerance=1)
288+
tolerance=1)
308289
self.display_image[:, :, i] = np.where(self.mask[:, :, i] == 255, 255, self.display_image[:, :, i])
309290
self.update_image()
310-
311291

312292

313-
314-
315-
316293
if __name__ == '__main__':
317294
params = {}
318-
params['inputImagePath'] = '2D_PigSkin.tif'
319-
params['resultMaskPath'] = '2D_PigSkinMask.tif'
320-
params['resultObjectPath'] = '2D_PigSkinResult.tif'
321-
params['TCount']=1
322-
params['ZCount']=1
295+
params['inputImagePath'] = 'test_8b.tif'
296+
params['resultMaskPath'] = 'test_8b_res.tif'
297+
params['resultObjectPath'] = 'test_8b_objres.tif'
298+
params['TCount'] = 1
299+
params['ZCount'] = 1
323300
run(params)
301+
302+
# CHANGELOG:
303+
# v1.01: - Bug fixed with wxPython.Image.SetData (ValueError: Invalid data buffer size.) >> was not using rescaled img.

0 commit comments

Comments
 (0)