// 找到了画十字线标注 for (row = 0; row < H; row++) { for (col = 0; col < W; col++) { i = (unsigned long)(row*3*W + 3*col); if ((row == max_row) || (col == max_col)) *(img_d + i) = *(img_d + i + 1) = *(img_d + i + 2) = 255; } } pixels_from_center = max_row - H/2; // 算出距离并打印出来 range = h_cm / tan(pixels_from_center * gain + offset); cout << "W= " << W << ", H= " << H << ", Max Value at x=" << max_col << ", y= " << max_row << ", range= " << range << endl; cvShowImage( "Example", frame ); char c = cvWaitKey(10); if( c == 27 ) break; } cvReleaseCapture( &capture ); cvDestroyWindow( "Example" ); } 实际测试中误差还算可以,在5%以内,最主要是激光点的判断还有很大提高空间,opencv提供了不少API,网上也有一些文章 距离64cm: ![]() 距离152cm: ![]() |