加为好友
发送私信
在线聊天
发表于:2008-05-11 11:52:14 楼主
[b]检测指针圆盘 //CircleDetect(pDoc, SMALLSTEP, 0, 50, 60, 3, &x1, &y1, 0.2); //CircleDetect(pDoc, SMALLSTEP, 0, 50, 60, 3, &x2, &y2, 0.2); //CircleDetect(pDoc, SMALLSTEP, 0, 50, 60, 3, &x3, &y3, 0.2); CircleDetect(pDoc, SMALLSTEP, 0, 40, 60, 60, 40, 3, &x1, &y1, 0.2); CircleDetect(pDoc, SMALLSTEP, 0, 40, 60, 60, 40, 3, &x2, &y2, 0.2); CircleDetect(pDoc, SMALLSTEP, 0, 40, 60, 60, 40, 3, &x3, &y3, 0.2); } void CProjectView::OnAddTest() { // TODO: Add your command handler code here CPen greenPen(PS_SOLID, 1, RGB(0,255,0)); CPen redPen(PS_SOLID, 1, RGB(255,0,0)); CClientDC pDC(this); COLORREF red = RGB(255, 0, 0); CProjectDoc * pDoc = GetDocument(); ASSERT_VALID(pDoc); //三个指针 pDC.SelectObject(redPen); pDC.MoveTo(x1, pDoc->BmpHeight -1 -y1); pDC.LineTo(x1 + (int)(LEN * cos(nAngle1 * 2 * pi / 360)), pDoc->BmpHeight -1- y1 - (int)(LEN * sin(nAngle1 * 2 * pi / 360))); pDC.MoveTo(x2, pDoc->BmpHeight - y2); pDC.LineTo(x2 + (int)(LEN * cos(nAngle2 * 2 * pi / 360)), pDoc->BmpHeight - y2 -1 - (int)(LEN * sin(nAngle2 * 2 * pi / 360))); pDC.MoveTo(x3, pDoc->BmpHeight - y3); pDC.LineTo(x3 + (int)(LEN * cos(nAngle3 * 2 * pi / 360)), pDoc->BmpHeight - y3 - 1- (int)(LEN * sin(nAngle3 * 2 * pi / 360))); //三个圆心 pDC.SelectObject(greenPen); pDC.MoveTo(x1, pDoc->BmpHeight -1- y1); pDC.LineTo(x2, pDoc->BmpHeight -1- y2); pDC.MoveTo(x2, pDoc->BmpHeight -1- y2); pDC.LineTo(x3, pDoc->BmpHeight -1- y3); pDC.MoveTo(x3, pDoc->BmpHeight -1- y3); pDC.LineTo(x1, pDoc->BmpHeight -1- y1); //pDC.MoveTo(100, 100); //pDC.LineTo(300, 300); pDC.SetPixel(x, pDoc->BmpHeight - 1- y, red); /* int i ; for(i = 0; i < pDoc->nWidth * pDoc->BmpHeight ; i ++) { pDoc->pImage[i] = pDoc->pImageBak[i]; } Invalidate(TRUE); */ } void CProjectView::OnAddFinger() { // TODO: Add your command handler code here CProjectDoc * pDoc = GetDocument(); ASSERT_VALID(pDoc); ((CMainFrame *)AfxGetMainWnd())->SetStatus("检测指针偏转"); // 检测X1, Y1 FingerDetectNew(x1, y1, pDoc, &fx1, &fy1, &nAngle1); // 检测X2, Y2 FingerDetectNew(x2, y2, pDoc, &fx2, &fy2, &nAngle2); // 检测X3, Y3 FingerDetectNew(x3, y3, pDoc, &fx3, &fy3, &nAngle3); ((CMainFrame *)AfxGetMainWnd())->SetStatus("指针偏转检测成功"); AfxMessageBox("指针检测完毕!"); } void CProjectView::FingerDetectNew(int x, int y, CProjectDoc * pDoc, int *fx, int *fy, int *Angle) { int nLen; int i, nX, nY, nR; int nCount[360], nMin, nPos; nLen = LEN; nMin = LEN * 255; for (i = 0; i < 360; i ++) { nCount[i] = 0; nX = x; nY = y; for(nR = 0; nR < nLen; nR ++) { nX = x + (int)(nR * cos(i * 2 * pi / 360)); nY = y + (int)(nR * sin(i * 2 * pi / 360)); nCount[i] += pDoc->pImageBak[nY * pDoc->nWidth + nX]; //nCount[i] += pDoc->pImageBak[(nY+1) * pDoc->nWidth + nX]; //nCount[i] += pDoc->pImageBak[(nY-1) * pDoc->nWidth + nX]; } if (nCount[i] < nMin) { nMin = nCount[i]; nPos = i; } } (*fx) = x + (int)(nLen * cos(nPos * pi * 2 / 360)); (*fy) = y + (int)(nLen * sin(nPos * pi * 2 / 360)); (*Angle) = nPos; //AfxMessageBox("""); } void CProjectView::FingerDetect(int x, int y, CProjectDoc * pDoc, int *fx, int *fy) { //int i, Step; int xStart, yStart, nTempx, nTempy; //int mTempx, mTempy, flag; //以x,y为中心往外面找点,一直找到最近的一个为止StartX, StartY FindStartPoint(x, y, &xStart, &yStart, pDoc); nTempx = xStart; nTempy = yStart; if (xStart - x > 0) { //往上左搜 while((pDoc->pImage[(nTempy + 1) * pDoc->nWidth + nTempx - 1] == 128) ¦ ¦ (pDoc->pImage[(nTempy + 1) * pDoc->nWidth + nTempx] == 128)) { if (pDoc->pImage[(nTempy + 1) * pDoc->nWidth + nTempx - 1] == 128) { nTempy = nTempy + 1; nTempx = nTempx - 1; } else if (pDoc->pImage[(nTempy + 1) * pDoc->nWidth + nTempx] == 128) { nTempy = nTempy + 1; nTempx = nTempx; } } //往下右 } AfxMessageBox("ok"); //以此点为起点进行跟踪,按照两个不同的方向进行跟踪,一个顺时针,一个逆时针 // 跟踪结束条件为:顺时针的不能往前往右算结束;逆时针为不能往前往左算结束 //比较两个跟踪的最终结果,取离圆心远的那个点 return; } void CProjectView::FindStartPoint(int x, int y, int * xStart, int * yStart, CProjectDoc * pDoc) { int i, Step, flag; Step = 1; while (1) { flag = 0; for(i = 0; i < Step; i++) { //左上 if (pDoc->pImage[(y + i) * pDoc->nWidth + x - Step + i] == 128) { (*xStart) = x - Step + i; (*yStart) = y + i; flag = 1; break; } //右上 if (pDoc->pImage[(y + Step - i) * pDoc->nWidth + x + i] == 128) { (*xStart) = x + i; (*yStart) = y + Step - i; flag = 1; break; } //右下 if (pDoc->pImage[(y - i) * pDoc->nWidth + x + Step - i] == 128) { (*xStart) = x + Step - i; (*yStart) = y - i; flag = 1; break; } if (pDoc->pImage[(y - Step + i) * pDoc->nWidth + x - i] == 128) { (*xStart) = x - i; (*yStart) = y - Step + i; flag = 1; break; } } if (flag == 1) break; Step ++; } } void CProjectView::OnAddEnd() { // TODO: Add your command handler code here //现在已知x1,y1, x2,y2, x3,y3, fx1,fy1, fx2,fy2, fx3, fy3 //先确定圆心三角形,对于三个点进行确定 0.01 0.001, 0.0001三个指针 double d_l12, d_l13, d_l23; double nAA1, nAA2, nAA3, nTemp; BOOL IsBelow; double k; int longest, n; //1.确定长边 longest = 0; d_l12 = sqrt((x1-x2)*(x1-x2) + (y1-y2)*(y1-y2)); d_l13 = sqrt((x1-x3)*(x1-x3) + (y1-y3)*(y1-y3)); d_l23 = sqrt((x3-x2)*(x3-x2) + (y3-y2)*(y3-y2)); if (d_l12 > d_l13) { if (d_l12 > d_l23) { longest = 12; } else { longest = 23; } } else { if (d_l13 > d_l23) { longest = 13; } else { longest = 23; } } //2.确定第三点在长边的上边还是下边 //3.确定三点位置 0.01-> 1; 0.001->2; 0.0001->3; switch(longest) { case 13: IsBelow = BelowUpon(x1, y1, x3, y3, x2, y2, nAngle1, nAngle3, nAngle2); break; case 23: IsBelow = BelowUpon(x2, y2, x3, y3, x1, y1, nAngle2, nAngle3, nAngle1); break; case 12: IsBelow = BelowUpon(x1, y1, x2, y2, x3, y3, nAngle1, nAngle2, nAngle3); break; } //输入0指针的偏转角度,以点1为中心,边13顺时针旋转到0指针方向的角度是固定的 //将这个已知角度转化为跟X轴正方向的转角 //计算三根指针的偏转,得到最后读数 k = (double)(y1-y3)/(double)(x1-x3); if (atan(k) < 0) nTemp = (-1) * atan(k) * 360 / (2 * pi); else nTemp = 180 - atan(k) * 360 / (2 * pi); nAA1 = (-1) * nAngle1 - THITA - nTemp; while (nAA1 < 0) { nAA1 += 360; } nAA2 = (-1) * nAngle2 - THITA - nTemp; while (nAA2 < 0) { nAA2 += 360; } nAA3 = (-1) * nAngle3 - THITA - nTemp; while (nAA3 < 0) { nAA3 += 360; } CResultDlg dlgResult; //dlgBaseInfo.SetInfo(pDoc); CString sTemp; sTemp = LongToString(x1); dlgResult.m_X1 = sTemp; sTemp = LongToString(y1); dlgResult.m_Y1 = sTemp; sTemp = LongToString(x2); dlgResult.m_X2 = sTemp; sTemp = LongToString(y2); dlgResult.m_Y2 = sTemp; sTemp = LongToString(x3); dlgResult.m_X3 = sTemp; sTemp = LongToString(y3); dlgResult.m_Y3 = sTemp; sTemp = LongToString(x); dlgResult.m_X = sTemp; sTemp = LongToString(y); dlgResult.m_Y = sTemp; sTemp = LongToString(nAngle1); dlgResult.m_A1 = sTemp; sTemp = LongToString(nAngle2); dlgResult.m_A2 = sTemp; sTemp = LongToString(nAngle3); dlgResult.m_A3 = sTemp; n = (int)(nAA1 / 36); sTemp = LongToString(n); dlgResult.m_R1 = sTemp; n = (int)(nAA2 / 36); sTemp = LongToString(n); dlgResult.m_R2 = sTemp; n = (int)(nAA3 / 36); sTemp = LongToString(n); dlgResult.m_R3 = sTemp; ((CMainFrame *)AfxGetMainWnd())->SetStatus("当前显示最后结果"); dlgResult.DoModal(); ((CMainFrame *)AfxGetMainWnd())->SetStatus("就绪"); }
问题点数: 70 回复次数:11
显示所有回复 显示星级回复 显示楼主回复
修改
删除
举报
引用
回复