7

Do you happen to have a 1D non-maximum suppression algorithm written in Python. I need it for making a Canny edge detector in Python using scipy that takes as input a 1D intensity vector.

I've looked around on the web and there is a lot of information describing the behavior of the Canny edge detector and some examples written in Java but they all describe edge detection in 2D.

However scipy does support the other algorithms needed for the Canny edge detection, namely the Gaussian filtering and differentiation for 1D.

Thanks in advance.

Charles
  • 50,943
  • 13
  • 104
  • 142
citn
  • 1,522
  • 3
  • 18
  • 29

3 Answers3

2

Do you just mean a maximum filter? If so, have a look at scipy.ndimage.maximum_filter1d

As a quick example:

import numpy as np
import scipy.ndimage as ndimage

input = np.sin(np.linspace(0, 4*np.pi, 20))
input = (input * 10).astype(np.int) # Makes it easier to read
output = ndimage.maximum_filter1d(input, 4)

print 'In: ', input
print 'Out:', output

This yields:

In:  [ 0  6  9  9  4 -1 -7 -9 -8 -3  3  8  9  7  1 -4 -9 -9 -6  0]
Out: [ 6  9  9  9  9  9  4 -1 -3  3  8  9  9  9  9  7  1 -4  0  0]
dermen
  • 5,252
  • 4
  • 23
  • 34
Joe Kington
  • 275,208
  • 71
  • 604
  • 463
0

I assume your data is not periodic. Let me give you some pseudocode and hopefully that is enough.

-- diff the data

-- you are looking for sign changes in the diff. For Max that would be going from positive to negative; zero is a special case. numpy.sign() gives you the sign of each value in -1, 0, 1 for negative, zero and positive values.

-- You can diff again -- you are looking for -2 and -1 or for min suppression 2 and 1. Handle the boundaries properly.

Good luck, E

import matplotlib.pyplot as plt
import numpy as np

dat= np.linspace(1, 5, 5)
dat2 = np.concatenate([dat, dat[::-1]+.5, dat,dat[::-1]])
## ignore above.

res = np.diff(np.sign(np.diff(dat2, prepend=[0])), append=0)
plt.plot(dat2)  ## NB end conditions, and you can ignore plotting
plt.plot(res)

Cheers, E

jtlz2
  • 7,700
  • 9
  • 64
  • 114
user96265
  • 510
  • 5
  • 6
-1

If your input data is the variable "signal", then the result you need can be given by:

k = np.sum(np.abs(np.diff(signal>0)))

(using Numpy functions)

jtlz2
  • 7,700
  • 9
  • 64
  • 114
Filipe Pinto
  • 311
  • 3
  • 17