# calculate second derivative for all three channels, sum of squares for mag

for y in xrange(2,h-1):

for x in xrange(2,w-1):

rdx = inpix[x+1,y][0] + inpix[x-1,y][0] - 2*inpix[x,y][0]

gdx = inpix[x+1,y][1] + inpix[x-1,y][1] - 2*inpix[x,y][1]

bdx = inpix[x+1,y][2] + inpix[x-1,y][2] - 2*inpix[x,y][2]

rdy = inpix[x,y+1][0] + inpix[x,y-1][0] - 2*inpix[x,y][0]

gdy = inpix[x,y+1][1] + inpix[x,y-1][1] - 2*inpix[x,y][1]

bdy = inpix[x,y+1][2] + inpix[x,y-1][2] - 2*inpix[x,y][2]

outpix2[x,y] = math.sqrt( rdx*rdx + gdx*gdx + bdx*bdx + rdy*rdy + gdy*gdy + bdy*bdy )

To perform image segmentation it would probably be smart to use the pixel value, the gradient (mag and direction) and the second derivative, no sense in throwing away useful information. Gray-scale image of the magnitude of the second derivative (edges are really where the second derivative changes sign, but an edge tends to have extrema in the second derivative on either side of it) shown below.

Using Psyco or F2Py has been shown on similar problems to give several orders of magnitude speed up over a straight Python implementation as shown above.

## No comments:

## Post a Comment