I'm implementing a version of the mean shift image processing algorithm for color segmentation in Python/NumPy.
I've written a pure NumPy version of the actual mean shifting per pixel (which I imagine is where the majority of time is taking). It slices an array of RGB values to work on out of the parent image, then creates lower bound and higher bound RGB reference arrays and generates a boolean masking array for the pixels to use for averaging then averages.
Any further optimizations? I suspect vectorizing the x/y for loops might give a speed up but for the life of me, but I haven't figured out how. (Some how generating an array of each pixel grid to work on and then generalizing the mean shift to take array input?) gL is grid length. gS is gL squared - the number of pixels in grid.
for itr in xrange(itrs): if itr != 0: img = imgNew for x in xrange(gL,height-gL): for y in xrange(gL,width-gL): cGrid = img[x-gSmp:(x+gSmp+1),y-gSmp:(y+gSmp+1)] cLow,cUp = np.empty((gL,gL,3)),np.empty((gL,gL,3)) cLow[:] = [img[x,y]-tol,img[x,y]-tol,img[x,y]-tol] cUp[:] = [img[x,y]+tol,img[x,y]+tol,img[x,y]+tol] cBool = np.any(((cLow < cGrid) & (cUp > cGrid)),axis=2) imgNew[x,y] = np.sum(cGrid[cBool],axis=0)/cBool.sum()