For the problems of slow speed, low efficiency, and labor intensity of manual measurement of crankshaft structure size of air compressor, a machine vision-based method for measuring crankshaft size of air compressor was proposed. Firstly, a bilateral retention filter was used to filter the grayscale image to ensure the crankshaft edge while smoothing the image. Then the smoothed image was binarized by an automatic threshold segmentation algorithm to segment the crankshaft and the background region. Finally, the sub-pixel edge contours of the image were extracted using canny as the localization function, and the edge contours of the crankshaft were obtained after segmentation, screening, and combination. Based on the extracted contour lines, the edge line regions were drawn, and the pixel distance between the two linear regions was calculated to obtain the diameter size of the crankshaft. The experimental results show that the accuracy of the diameter dimension calculated by this algorithm can reach 99.898% compared with the measured dimension, and the error was less than 0.03mm.