Demo entry 6349887

C++

   

Submitted by misaki on Mar 01, 2017 at 13:47
Language: C++. Code size: 3.1 kB.

void CWiFiCarDlg::OnTimer(UINT_PTR nIDEvent)
{
	// TODO: 在此添加消息处理程序代码和/或调用默认值
	switch (nIDEvent)
	{
	case 1: {
		m_vcap >> m_carmeraImg;  //把视频帧存入图像矩阵
		m_image = m_carmeraImg;
		recordVedio::gl_image = m_carmeraImg;
		m_CvvImage.CopyOf(&m_image, 1);
		m_CvvImage.DrawToHDC(m_hDC, &m_rect);
		break;
	}
	case 2: {
		if (m_mode == 0x01)
		{
			if (m_isSearching) {
				using namespace std;
				using namespace cv;

				TickMeter tm;
				tm.start();

				Mat img = m_carmeraImg;
				Mat imgHSV, imgThresholded;

				cvtColor(img, imgHSV, CV_BGR2HSV);
				inRange(imgHSV, Scalar(m_lowH, m_lowS, m_lowV), Scalar(m_highH, m_highS, m_highV), imgThresholded);

				//morphological opening (remove small objects from the foreground)
				erode(imgThresholded, imgThresholded, getStructuringElement(MORPH_ELLIPSE, Size(6, 6)));
				dilate(imgThresholded, imgThresholded, getStructuringElement(MORPH_ELLIPSE, Size(6, 6)));

				//morphological closing (fill small holes in the foreground)
				dilate(imgThresholded, imgThresholded, getStructuringElement(MORPH_ELLIPSE, Size(6, 6)));
				erode(imgThresholded, imgThresholded, getStructuringElement(MORPH_ELLIPSE, Size(6, 6)));

				imshow("Thresholded", imgThresholded);

				Moments m_moments = moments(imgThresholded);
				double m_area = m_moments.m00;
				double m_m10 = m_moments.m10;
				double m_m01 = m_moments.m01;
				double m_posX = m_m10 / m_area;
				double m_posY = m_m01 / m_area;
				double m_dX = m_posX / m_imgWidth - 0.5;
				//judge which side to go

				if (m_area > 15000)
				{
					if (abs(m_dX) < 0.1)
					{
						if (m_isRunning)
						{
							cmd[2] = 0x01;
							sendDataToCar(cmd, 5);
						}
						m_carState.SetWindowTextW(_T("前进"));
					}
					else if (m_dX > 0)
					{
						if (m_isRunning)
						{

							if (m_dX > 0.25)
							{
								cmd[2] = 0x06;
							}
							else
							{
								cmd[2] = 0x03;
							}
							sendDataToCar(cmd, 5);
						}
						m_carState.SetWindowTextW(_T("向右"));
					}
					else
					{
						if (m_isRunning)
						{
							if (m_dX < -0.25)
							{
								cmd[2] = 0x08;
							}
							else
							{
								cmd[2] = 0x04;
							}
							sendDataToCar(cmd, 5);
						}
						m_carState.SetWindowTextW(_T("向左"));
					}
				}
				
				//judge whether it's time to stop
				if (m_isRunning)
				{
					if (m_area > m_objSize) {
						m_isFound = true;
						m_isRunning = false;
						cmd[2] = 0x00;
						cmd[3] = 0x00;
						sendDataToCar(cmd, 5);
						m_carState.SetWindowTextW(_T("停止"));
						GetDlgItem(IDC_START)->SetWindowTextW(_T("Found"));
					}
				}
				tm.stop();
				double t = tm.getTimeMilli();
			}
		}
		else
		{
			//当且仅当退出search模式后,清除timer
			m_isRunning = false;
			cmd[2] = 0x00;
			cmd[3] = 0x00;
			sendDataToCar(cmd, 5);

			m_isSearching = false;
			m_isFound = false;
			GetDlgItem(IDC_START)->SetWindowTextW(_T("Run"));
			//退出search模式后清除timer
			KillTimer(2);
		}
	}
	default:
		break;
	}
	CDialogEx::OnTimer(nIDEvent);
}

This snippet took 0.01 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).