Aiming at the subpixle edge detection of speckle in autofocus for micro-machine vision, a novel accurate subpixel edge detection algorithm was proposed. The image of the part to be inspected was binaried by simple threshold algorithm. The noise in image was eliminated by blob area threshold algorithm. The pixel level edge detection was done and the single-pixel width connected pixel level contour was acquired by binary mathematical morphological algorithm. The subpixel level edge detection was completed and the subpixel level contour was obtained by 9×9 pixel rectangular lens algorithm based on cubic spline interpolation. The example result indicate that calculation speed of the algorithm proposed herein is fast, anti-noise performance is high, inspection accuracy is high, and subpixel edge location accuracy can reach to μm level.