问题描述:
1.随机生成平面中的二维点;
2.探测点间距小于距离阈值的异常点;
3.显示并输出结果。
#include "stdafx.h"
#include "iostream"
#include "string"
#include "fstream"
#include "sstream"
#include "stdlib.h"
#include "time.h"
#include "vector"
#include <opencv\highgui.h>
#include <opencv\cv.h>
#include <opencv\ml.h>
#include <opencv\cxcore.h>
#include <opencv2\core\core.hpp>
#include <opencv2\highgui\highgui.hpp>
#include "opencv2\nonfree\features2d.hpp"
#include <opencv2\flann\miniflann.hpp>
#include <opencv2\flann\flann.hpp>
#include <opencv2\flann\kdtree_index.h>
using namespace std;
using namespace cv;
int _tmain(int argc, _TCHAR* argv[])
{
//*****①生成随机点(0-1000之内)*****//
vector<Point2i> random_point;
Point2i temp_p;
int number_random_point=1000;
cv::Mat_<float> source_p(number_random_point, 2);
srand((unsigned)time(NULL));
for (int i = 0; i < number_random_point; i++)
{
temp_p.x = (int)1000 * rand() / (RAND_MAX + 1);
temp_p.y = (int)1000 * rand() / (RAND_MAX + 1);//get the random point within (0,1000)
random_point.push_back(temp_p);
cv::Mat this_row(1,2,CV_16U);
this_row.at<ushort>(0, 0) = temp_p.x;
this_row.at<ushort>(0, 1) = temp_p.y;
source_p.at<float>(i, 0) = (float)temp_p.x;
source_p.at<float>(i, 1) = (float)temp_p.y;
}
cout << source_p << endl;
//*****②找出小于距离阈值d的异常点*****//
//index construction
vector<int> outlier_id;
cv::flann::Index flann_index(source_p, cv::flann::KDTreeIndexParams(1));
unsigned int max_searched_neighbours = 6;
float threshold_d = 30.0;
for (int i = 0; i < number_random_point; i++)
{
Mat_<float> my_query(1, 2);
my_query.at<float>(0, 0) = source_p.at<float>(i, 0);
my_query.at<float>(0, 1) = source_p.at<float>(i, 1);
cout << my_query << endl;
Mat index_p, index_distance;
flann_index.radiusSearch(my_query, index_p, index_distance, threshold_d,max_searched_neighbours);
cout << index_p << endl;
cout << index_distance << endl;
if (!index_p.empty())
{
for (int j = 0; j < index_p.cols; j++)
{
if (index_p.at<int>(0, j)<0 || index_p.at<int>(0, j)>number_random_point)
{
continue;//如果找到的存贮的点号不在(0,num_randon_point)中
}
if (index_distance.at<float>(0, j)<= 0 || index_distance.at<float>(0, j)>threshold_d)
{
continue;//如果距离不在阈值范围之内
}
if (find(outlier_id.begin(),outlier_id.end(),index_p.at<int>(0,j))==outlier_id.end())
{
outlier_id.push_back(index_p.at<int>(0,j));
}
}
}
}
//******③输出异常点********//
Mat dst_img(6000, 6000, CV_AA, Scalar(255, 255, 255));
float circle_radius = 3.0;
vector<Point2i>::iterator it1 = random_point.begin();
Point2i temp_center;
while (it1!=random_point.end())
{
temp_center.x = it1->x * 4;
temp_center.y = it1->y * 4;
circle(dst_img, temp_center, circle_radius, Scalar(0, 255, 0),-1);
it1++;
}
imshow("original_points", dst_img);
waitKey(0);
for (auto it = outlier_id.begin(); it != outlier_id.end(); it++)
{
Point2i circle_center = random_point[*it];
circle_center.x = circle_center.x * 4;
circle_center.y = circle_center.y * 4;
circle(dst_img, circle_center, circle_radius, Scalar(0, 0, 255), -1);
}
imshow("found_outliers", dst_img);
waitKey(0);
string output_filename = "outlier.bmp";
imwrite(output_filename, dst_img);
return 0;
}