-
Notifications
You must be signed in to change notification settings - Fork 0
/
edgefilter.py
executable file
·62 lines (50 loc) · 2.09 KB
/
edgefilter.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# sobel and canny filter
from pyminc.volumes.factory import *
from optparse import OptionParser
from scipy import ndimage
from skimage import filter
if __name__ == "__main__":
usage = "usage: %prog [options] input.mnc output.mnc"
description = "Edge detection - sobel filter"
parser = OptionParser(usage=usage, description=description)
parser.add_option("-s","--sigma", dest="sigma",
help="Canny filter sigma default: %default",
default=3, type='float')
parser.add_option("--sobel", dest="sobel",
help="Use sobel filter",
default=False, action='store_true')
parser.add_option("--nosobel", dest="sobel",
help="Don't use sobel filter [default]",
action='store_false')
parser.add_option("--canny", dest="canny",
help="Use canny filter [default]",
default=True, action='store_true')
parser.add_option("--nocanny", dest="canny",
help="Don't use canny filter",
action='store_false')
(options, args) = parser.parse_args()
if len(args) != 2:
parser.error("Incorrect number of arguments")
inim = volumeFromFile(args[0], dtype='ushort')
outim = volumeFromInstance(inim, args[1])
''' 3D Sobel filter (doesnt work always) '''
if options.sobel:
outim.data[::] = ndimage.generic_gradient_magnitude(inim.data, ndimage.sobel)
options.canny = 0
''' 2D Canny filter '''
# http://scipy-lectures.github.io/advanced/image_processing/auto_examples/plot_canny.html
# canny(image, sigma=1.0, low_threshold=0.1, high_threshold=0.2, mask=None)
if options.canny:
for i in range(inim.sizes[0]):
t = inim.getHyperslab((i,0,0),(1,inim.sizes[1],inim.sizes[2]))
t.shape = (inim.sizes[1], inim.sizes[2])
c = filter.canny(t, sigma=options.sigma)
outim.data[i::] = c
if options.canny or options.sobel:
outim.writeFile()
outim.closeVolume()
else:
print "No filter selected, no filtering performed."
inim.closeVolume()