Created
May 5, 2015 19:49
-
-
Save vighneshbirodkar/fd2547a8e169a7a7c35f to your computer and use it in GitHub Desktop.
edge rag line profile
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Timer unit: 1e-06 s | |
Total time: 11.423 s | |
File: /home/vighnesh/git/scikit-image/skimage/future/graph/rag.py | |
Function: rag_boundary at line 324 | |
Line # Hits Time Per Hit % Time Line Contents | |
============================================================== | |
324 @profile | |
325 def rag_boundary(labels, edge_map, connectivity=2): | |
326 """ Comouter RAG based on region boundaries | |
327 | |
328 Given an image's initial segmentation and its edge map this method | |
329 constructs the corresponding Region Adjacency Graph (RAG). Each node in the | |
330 RAG represents a set of pixels within the image with the same label in | |
331 `labels`. The weight between two adjacent regions is the average value | |
332 in `edge_map` along their boundary. | |
333 | |
334 labels : ndarray | |
335 The labelled image. | |
336 edge_map : ndarray | |
337 This should have the same shape as that of `labels`. For all pixels | |
338 along the boundary between 2 adjacent regions, the average value of the | |
339 corresponding pixels in `edge_map` is the edge weight between them. | |
340 connectivity : int, optional | |
341 Pixels with a squared distance less than `connectivity` from each other | |
342 are considered adjacent. It can range from 1 to `labels.ndim`. Its | |
343 behavior is the same as `connectivity` parameter in | |
344 `scipy.ndimage.filters.generate_binary_structure`. | |
345 | |
346 Examples | |
347 -------- | |
348 >>> from skimage import data, segmentation, filters, color | |
349 >>> from skimage.future import graph | |
350 >>> img = data.chelsea() | |
351 >>> labels = segmentation.slic(img) | |
352 >>> edge_map = filters.sobel(color.rgb2gray(img)) | |
353 >>> rag = graph.rag_mean_color(labels, edge_map) | |
354 | |
355 """ | |
356 | |
357 1 43 43.0 0.0 graph = RAG() | |
358 | |
359 #Computing the relative indices of the neighbors | |
360 1 148 148.0 0.0 nbr_indices = list(np.ndindex(*[2]*labels.ndim)) | |
361 1 3 3.0 0.0 del nbr_indices[0] | |
362 4 100 25.0 0.0 nbr_indices_arr = ([idx for idx in nbr_indices if np.linalg.norm(idx) | |
363 3 11 3.7 0.0 <= connectivity]) | |
364 | |
365 1 12 12.0 0.0 iter_shape = tuple(np.array(labels.shape) - 1) | |
366 | |
367 239002 679521 2.8 5.9 for index in np.ndindex(iter_shape): | |
368 | |
369 239001 697222 2.9 6.1 index_arr = np.array(index) | |
370 239001 284484 1.2 2.5 current = labels[index] | |
371 239001 1414480 5.9 12.4 graph.add_node(current, {'labels': [current]}) | |
372 | |
373 956004 815865 0.9 7.1 for nbr_index in nbr_indices_arr: | |
374 | |
375 717003 4969235 6.9 43.5 adjacent_idx = tuple(index_arr + nbr_index) | |
376 717003 968561 1.4 8.5 adjacent = labels[adjacent_idx] | |
377 | |
378 717003 652203 0.9 5.7 if current == adjacent: | |
379 660989 483934 0.7 4.2 continue | |
380 | |
381 56014 110849 2.0 1.0 if graph.has_edge(current, adjacent): | |
382 54754 113261 2.1 1.0 graph[current][adjacent]['pixel count'] += 2 | |
383 54754 97832 1.8 0.9 intensity = edge_map[index] + edge_map[adjacent_idx] | |
384 54754 112225 2.0 1.0 graph[current][adjacent]['total intensity'] += intensity | |
385 else: | |
386 1260 9483 7.5 0.1 graph.add_edge(current, adjacent) | |
387 1260 2561 2.0 0.0 graph[current][adjacent]['pixel count'] = 2 | |
388 1260 2542 2.0 0.0 intensity = edge_map[index] + edge_map[adjacent_idx] | |
389 1260 1993 1.6 0.0 graph[current][adjacent]['total intensity'] = intensity | |
390 | |
391 1261 3930 3.1 0.0 for (x, y, data) in graph.edges_iter(data=True): | |
392 1260 2484 2.0 0.0 data['weight'] = data['total intensity']/data['pixel count'] | |
393 | |
394 1 1 1.0 0.0 return graph |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment