1. 程式人生 > >使用opencv模擬雷達掃描顯示

使用opencv模擬雷達掃描顯示

使用opencv模擬雷達掃描顯示,掃描到的目標點,由rand函式隨機產生,最終用於顯示,目標識別後,顯示目標。

 

// VectorPoint.cpp : 定義控制檯應用程式的入口點。
//

#include "stdafx.h"
#include<opencv2/opencv.hpp>
#include<iostream>
#include"stdio.h"
#include<stdlib.h>
#include<windows.h>
#define random(x) (rand()%x)

using namespace std;
using namespace cv;




class MvTimer
{
public:
	MvTimer() { QueryPerformanceFrequency(&Freq); restart(); }
	void restart() { QueryPerformanceCounter(&Count1); }
	float time() { QueryPerformanceCounter(&Count2); return (float)((Count2.QuadPart - Count1.QuadPart) / (double)Freq.QuadPart * 1000.0f); }
private:
	LARGE_INTEGER Count1, Count2, Freq;
};

//void CBasicDlg::object_location(cv::Mat mat_img, std::vector<bbox_t> result_vec, cv::Mat *mat_imgout)    //視場角為5°,雷達影象解析度1000*1000
//object_location(newframe1, result_vec, &copyRadar);
int main()
{
	Mat mat_img = imread("test.bmp");
	Mat radar = imread("radar.png");
	cv::Point center;
	double theta, beta, rho;
	center.x = mat_img.cols / 2;
	center.y = mat_img.rows / 2;	
	vector<vector<Point>> circlecenter(72);


	int num = 10;
	while (num--)
	{
		MvTimer t;  		  
		for (int index = 0; index < 72; index++)	//index屬於0-71
		{			
			Mat mat_imgcopy = radar.clone();
			t.restart();
			Mat *mat_imgout = &mat_imgcopy;			
			theta = 3.1415926 * (2 * center.x - mat_img.cols + 2 * mat_img.cols * index) / (72 * mat_img.cols);
			circlecenter[index].clear();
			for (int t = 0; t < random(5); t++)
			{
				center.y = random(250);
				rho = 0.31 * (*mat_imgout).rows - (0.12 * (*mat_imgout).rows * center.y) / mat_img.rows;				
				float x = (*mat_imgout).cols / 2 + rho*sin(theta);
				float y = (*mat_imgout).rows / 2 - rho*cos(theta);
				circlecenter[index].push_back(Point(x, y));		
			}

			for (int i = 0; i < 72; i++)
			{
				for (int j = 0; j< circlecenter[i].size(); j++)
				{
					circle(*mat_imgout, circlecenter[i][j], 10, Scalar(0, 0, 255), -1, 8, 1);  //在Mat型別上畫圓
				}
			}

			//顯示雷達掃描指示直線
			double beta, beta0;
			//	int rhomax = 1125;
			double rhomax = (*mat_imgout).cols / 2.67;
			beta = 3.1415926 * index / 36;
			beta0 = beta - 1.134;
			Point start, end, end0;
			start.x = (*mat_imgout).cols / 2;
			start.y = (*mat_imgout).rows / 2;
			end.x = start.x + rhomax * sin(beta);
			end.y = start.y - rhomax * cos(beta);
			end0.x = start.x + rhomax * sin(beta0);
			end0.y = start.y - rhomax * cos(beta0);
			//line(*mat_imgout, start, end0, Scalar(0, 255, 0), 3);
			line(*mat_imgout, start, end, Scalar(0, 255, 0), 3);
			cout << t.time() << endl;
			imshow("test", *mat_imgout);			
			waitKey(100);
		}		
	}	
	return 0;
}