目录
1、稀疏光流法跟中移动物体
2、监督学习聚类
3、K均值聚类
4、加载深度神经网络模型
5、深度神经网络模型的使用
1、稀疏光流法跟中移动物体
//稀疏光流法跟中移动物体
vector<Scalar> color_lut;//颜色查找表
void draw_lines(Mat &image, vector<Point2f> pt1, vector<Point2f> pt2)
{RNG rng(5000);if (color_lut.size() < pt1.size()){for (size_t t = 0; t < pt1.size(); t++){color_lut.push_back(Scalar(rng.uniform(0, 255), rng.uniform(0, 255),rng.uniform(0, 255)));}}for (size_t t = 0; t < pt1.size(); t++){line(image, pt1[t], pt2[t], color_lut[t], 2, 8, 0);}
}
int test6()
{VideoCapture capture("F:/testMap/lolTFT.mp4");Mat prevframe, prevImg;if (!capture.read(prevframe)){cout << "请确认输入视频文件是否正确" << endl;return -1;}cvtColor(prevframe, prevImg, COLOR_BGR2GRAY);//角点检测相关参数设置vector<Point2f> Points;double qualityLevel = 0.01;int minDistance = 10;int blockSize = 3;bool useHarrisDetector = false; double k = 0.04;int Corners = 5000;//角点检测goodFeaturesToTrack(prevImg, Points, Corners, qualityLevel, minDistance,Mat(), blockSize, useHarrisDetector, k);//稀疏光流检测相关参数设置vector<Point2f> prevPts;//前一帧图像角点坐标vector<Point2f> nextPts;//当前帧图像角点坐标vector<uchar> status;//检点检测到的状态vector<float> err;TermCriteria criteria = TermCriteria(TermCriteria::COUNT + TermCriteria::EPS, 30, 0.01);double derivlambda = 0.5;int flags = 0;//初始状态的角点vector<Point2f> initPoints;initPoints.insert(initPoints.end(), Points.begin(), Points.end());//前一帧图像中的角点坐标prevPts.insert(prevPts.end(), Points.begin(), Points.end());while (true){Mat nextframe, nextImg;if (!capture.read(nextframe)){break;}imshow("nextframe", nextframe);//光流跟踪cvtColor(nextframe, nextImg, COLOR_BGR2GRAY);calcOpticalFlowPyrLK(prevImg, nextImg, prevPts, nextPts, status, err,Size(31, 31), 3, criteria, derivlambda, flags);//判断角点是否移动,如果不移动就删除size_t i, k;for (i = k = 0; i < nextPts.size(); i++){//距离与状态测量double dist = abs(prevPts[i].x - nextPts[i].x) + abs(prevPts[i].y - nextPts[i].y);if (status[i] && dist > 2){prevPts[k] = prevPts[i];initPoints[k] = initPoints[i]; nextPts[k++] = nextPts[i];circle(nextframe, nextPts[i], 3, Scalar(0, 255, 0), -1, 8);}}//更新移动角点数目nextPts.resize(k);prevPts.resize(k);initPoints.resize(k);//绘制跟踪轨迹draw_lines(nextframe, initPoints, nextPts);imshow("result", nextframe);char c = waitKey(50);if (c == 27){break;}//更新角点坐标和前一帧图像std::swap(nextPts, prevPts);nextImg.copyTo(prevImg);//如果角点数目少于30,就重新检测角点if (initPoints.size() < 30){goodFeaturesToTrack(prevImg, Points, Corners, qualityLevel,minDistance, Mat(), blockSize, useHarrisDetector, k);initPoints.insert(initPoints.end(), Points.begin(), Points.end());prevPts.insert(prevPts.end(), Points.begin(), Points.end());printf("total feature points : %d\n", prevPts.size());}}waitKey(0);return 0;
}
2、监督学习聚类
//监督学习聚类
int test7()
{Mat img = imread("F:/testMap/digits.png"); Mat gray;cvtColor(img, gray, COLOR_BGR2GRAY);//分割为160个cellsMat images = Mat::zeros(5000, 400, CV_8UC1);Mat labels = Mat::zeros(5000, 1, CV_8UC1);int index = 0;Rect numberImg;numberImg.x = 0;numberImg.height = 1;numberImg.width = 400;for (int row = 0; row < 50; row++){//从图像中分割出20×20的图像作为独立数字图像int label = row / 5;int datay = row * 20;for (int col = 0; col < 100; col++){int datax = col * 20;Mat number = Mat::zeros(Size(20, 20), CV_8UC1);for (int x = 0; x < 20; x++){for (int y = 0; y < 20; y++){number.at<uchar>(x, y) = gray.at<uchar>(x + datay, y + datax);}}//将二维图像数据转成行数据Mat row = number.reshape(1, 1);cout << "提取第"<< index + 1 <<"个数据" << endl; numberImg.y = index;//添加到总数据中row.copyTo(images(numberImg));//记录每个图像对应的数字标签labels.at<uchar>(index, 0) = label;index++;}}imwrite("所有数据按行排列结果.png", images); imwrite("标签. png", labels);//加载训练数据集images.convertTo(images,CV_32FC1); labels.convertTo(labels,CV_32SC1);Ptr<ml::TrainData> tdata = ml::TrainData::create(images,ml::ROW_SAMPLE, labels);//创建K近邻类Ptr<KNearest> knn = KNearest::create();knn->setDefaultK(5);//每个类别拿出5个数据knn->setIsClassifier(true);//进行分类//训练数据knn->train(tdata);//保存训练结果knn->save("knn_model.ym1");//输出运行结果提示cout << "己使用K近邻完成数据训练和保存" << endl;waitKey(0);return 0;
}