欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

34、通过已存帧的角度和位置信息进行预测帧输出

程序员文章站 2022-07-14 23:37:37
...

基本思想:

    使用前两帧信息位置信息和角度信息进行第三帧的信息位置预测;

 

#include <iostream>
#include <cmath>
#include <opencv2/opencv.hpp>
#define PI 3.1415926
using namespace std;
using namespace cv;

float compute_angle(float x1, float y1, float x2, float y2) {
    float angle_temp;
    float xx, yy;
    xx = x2 - x1;
    yy = y2 - y1;
    if (xx == 0.0)
        angle_temp = PI / 2.0;
    else
        angle_temp = atan(fabs(yy / xx));
    if ((xx < 0.0) && (yy >= 0.0))
        angle_temp = PI - angle_temp;
    else if ((xx < 0.0) && (yy < 0.0))
        angle_temp = PI + angle_temp;
    else if ((xx >= 0.0) && (yy < 0.0))
        angle_temp = PI * 2.0 - angle_temp;
    return (angle_temp);
}

int main() {

    Mat  image(900,900,CV_8UC3,Scalar(255,255,255));

    vector<Point> vec_point;
    vec_point.push_back(Point(10,10));
    vec_point.push_back(Point(63,56));
    vec_point.push_back(Point(100,90));
    vec_point.push_back(Point(162,170));


    for(int i=0;i<vec_point.size();i=i+2) {
        putText(image, to_string(i/2), Point(vec_point[i].x, vec_point[i].y), 1, 2, Scalar(200, 45, 160), 2, 8, false);
        rectangle(image, vec_point[i], vec_point[i + 1], Scalar(0, 0, 255), 2, 8, 0);//黄色矩形框

    }
    int x_pre_center=(vec_point[0].x+vec_point[1].x)/2;
    int y_pre_center=(vec_point[0].y+vec_point[1].y)/2;
    int x_now_center=(vec_point[2].x+vec_point[3].x)/2;
    int y_now_center=(vec_point[2].y+vec_point[3].y)/2;
    int distance=sqrt(pow(x_now_center-x_pre_center,2)+pow(y_now_center-y_pre_center,2));
    float angle= compute_angle(x_pre_center, y_pre_center, x_now_center, y_now_center)  ;
    cout<<angle<<endl<<distance<<endl;
    int x_min_pre_box=vec_point[2].x+distance*cos(angle);
    int y_min_pre_box=vec_point[2].y+distance*sin(angle);
    int x_max_pre_box=vec_point[3].x+distance*cos(angle);
    int y_max_pre_box=vec_point[3].y+distance*sin(angle);
    rectangle (image, Point(x_min_pre_box, y_min_pre_box), Point(x_max_pre_box, y_max_pre_box), Scalar(0, 0, 0), 2, 8, 0);//黄色矩形框

    /
    vec_point.clear();
    vector<Point>().swap(vec_point);
    vec_point.push_back(Point(400,430));
    vec_point.push_back(Point(563,556));
    vec_point.push_back(Point(340,390));
    vec_point.push_back(Point(362,470));


    for(int i=0;i<vec_point.size();i=i+2) {
        putText(image, to_string(i/2), Point(vec_point[i].x, vec_point[i].y), 1, 2, Scalar(200, 45, 160), 2, 8, false);
        rectangle(image, vec_point[i], vec_point[i + 1], Scalar(0, 0, 255), 2, 8, 0);//黄色矩形框

    }
    x_pre_center=(vec_point[0].x+vec_point[1].x)/2;
    y_pre_center=(vec_point[0].y+vec_point[1].y)/2;
    x_now_center=(vec_point[2].x+vec_point[3].x)/2;
    y_now_center=(vec_point[2].y+vec_point[3].y)/2;
    distance=sqrt(pow(x_now_center-x_pre_center,2)+pow(y_now_center-y_pre_center,2));
    angle= compute_angle(x_pre_center, y_pre_center, x_now_center, y_now_center)  ;
    cout<<angle<<endl<<distance<<endl;
    x_min_pre_box=vec_point[2].x+distance*cos(angle);
    y_min_pre_box=vec_point[2].y+distance*sin(angle);
    x_max_pre_box=vec_point[3].x+distance*cos(angle);
    y_max_pre_box=vec_point[3].y+distance*sin(angle);
    rectangle (image, Point(x_min_pre_box, y_min_pre_box), Point(x_max_pre_box, y_max_pre_box), Scalar(0, 0, 0), 2, 8, 0);//黄色矩形框

    ///


    /
    vec_point.clear();
    vector<Point>().swap(vec_point);
    vec_point.push_back(Point(740,40));
    vec_point.push_back(Point(763,56));
    vec_point.push_back(Point(640,150));
    vec_point.push_back(Point(662,170));


    for(int i=0;i<vec_point.size();i=i+2) {
        putText(image, to_string(i/2), Point(vec_point[i].x, vec_point[i].y), 1, 2, Scalar(200, 45, 160), 2, 8, false);
        rectangle(image, vec_point[i], vec_point[i + 1], Scalar(0, 0, 255), 2, 8, 0);//黄色矩形框

    }

    x_pre_center=(vec_point[0].x+vec_point[1].x)/2;
    y_pre_center=(vec_point[0].y+vec_point[1].y)/2;
    x_now_center=(vec_point[2].x+vec_point[3].x)/2;
    y_now_center=(vec_point[2].y+vec_point[3].y)/2;
    distance=sqrt(pow(x_now_center-x_pre_center,2)+pow(y_now_center-y_pre_center,2));
    angle= compute_angle(x_pre_center, y_pre_center, x_now_center, y_now_center)  ;
    cout<<angle<<endl<<distance<<endl;

    x_min_pre_box=vec_point[2].x+distance*cos(angle);
    y_min_pre_box=vec_point[2].y+distance*sin(angle);
    x_max_pre_box=vec_point[3].x+distance*cos(angle);
    y_max_pre_box=vec_point[3].y+distance*sin(angle);
    rectangle (image, Point(x_min_pre_box, y_min_pre_box), Point(x_max_pre_box, y_max_pre_box), Scalar(0, 0, 0), 2, 8, 0);//黄色矩形框

/
    vec_point.clear();
    vector<Point>().swap(vec_point);


    vec_point.push_back(Point(340,690));
    vec_point.push_back(Point(362,770));
    vec_point.push_back(Point(400,730));
    vec_point.push_back(Point(563,856));


    for(int i=0;i<vec_point.size();i=i+2) {
        putText(image, to_string(i/2), Point(vec_point[i].x, vec_point[i].y), 1, 2, Scalar(200, 45, 160), 2, 8, false);
        rectangle(image, vec_point[i], vec_point[i + 1], Scalar(0, 0, 255), 2, 8, 0);//黄色矩形框

    }
    x_pre_center=(vec_point[0].x+vec_point[1].x)/2;
    y_pre_center=(vec_point[0].y+vec_point[1].y)/2;
    x_now_center=(vec_point[2].x+vec_point[3].x)/2;
    y_now_center=(vec_point[2].y+vec_point[3].y)/2;
    distance=sqrt(pow(x_now_center-x_pre_center,2)+pow(y_now_center-y_pre_center,2));
    angle= compute_angle(x_pre_center, y_pre_center, x_now_center, y_now_center)  ;
    cout<<angle<<endl<<distance<<endl;
    x_min_pre_box=vec_point[2].x+distance*cos(angle);
    y_min_pre_box=vec_point[2].y+distance*sin(angle);
    x_max_pre_box=vec_point[3].x+distance*cos(angle);
    y_max_pre_box=vec_point[3].y+distance*sin(angle);
    rectangle (image, Point(x_min_pre_box, y_min_pre_box), Point(x_max_pre_box, y_max_pre_box), Scalar(0, 0, 0), 2, 8, 0);//黄色矩形框

    ///


    imshow("跟踪轨迹",image);
    imwrite("a.jpg",image);
    waitKey();
    return 0;
}

CMakefile.txt

cmake_minimum_required(VERSION 3.17)
project(test)

set(CMAKE_CXX_STANDARD 11)
find_package(OpenCV REQUIRED)

add_executable(test main.cpp)
target_link_libraries(test ${OpenC

结果显示 

34、通过已存帧的角度和位置信息进行预测帧输出

相关标签: C/C++基础知识