·wsdragon发表评论于2008-7-11 17:03:05
网上有一个朋友给了激光测距仪的算法,你可以搜一下,希望你早日成功
DIY基于摄像头的激光测距仪 vc++
void CTripodDlg::doMyImageProcessing(LPBITMAPINFOHEADER lpThisBitmapInfoHeader)
{
// doMyImageProcessing: This is where you'd write your own image processing code
// Task: Read a pixel's grayscale value and process accordingly
unsigned int W, H; // Width and Height of current frame [pixels]
unsigned int row, col; // Pixel's row and col positions
unsigned long i; // Dummy variable for row-column vector
unsigned int max_row; // Row of the brightest pixel
unsigned int max_col; // Column of the brightest pixel
BYTE max_val = 0; // Value of the brightest pixel
// Values used for calculating range from captured image data
// these values are only for a specific camera and laser setup
const double gain = 0.0024259348; // Gain Constant used for converting
// pixel offset to angle in radians
const double offset = -0.056514344; // Offset Constant
const double h_cm = 5.842; // Distance between center of camera and laser
double range; // Calculated range
unsigned int pixels_from_center; // Brightest pixel location from center
// not bottom of frame
char str[80]; // To print message
CDC *pDC; // Device context need to print messageRoboticFan
W = lpThisBitmapInfoHeader->biWidth; // biWidth: number of columns
H = lpThisBitmapInfoHeader->biHeight; // biHeight: number of rows
for (row = 0; row < H; row++) {
for (col = 0; col < W; col++) {
// Recall each pixel is composed of 3 bytes
i = (unsigned long)(row*3*W + 3*col);
// If the current pixel value is greater than any other, it is the new max pixel
if (*(m_destinationBmp + i) >= max_val)
{
max_val = *(m_destinationBmp + i);
max_row = row;
max_col = col;
}
}
}
// After each frame, reset max pixel value to zero
max_val = 0;
for (row = 0; row < H; row++) {
for (col = 0; col < W; col++) {
i = (unsigned long)(row*3*W + 3*col);
// Draw a white cross-hair over brightest pixel in the output display
if ((row == max_row) || (col == max_col))
*(m_destinationBmp + i) =
*(m_destinationBmp + i + 1) =
*(m_destinationBmp + i + 2) = 255;
}
}
// Calculate distance of brightest pixel from center rather than bottom of frame
pixels_from_center = 120 - max_row;
// Calculate range in cm based on bright pixel location, and setup specific constants
range = h_cm / tan(pixels_from_center * gain + offset);
// To print message at (row, column) = (75, 580)
pDC = GetDC();
// Display frame coordinates as well as calculated range
sprintf(str, "Max Value at x= %u, y= %u, range= %f cm ",max_col, max_row, range);
pDC->TextOut(75, 580, str);
ReleaseDC(pDC);
}
·个人主页 | 引用 | 返回 | 删除 | 回复