ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

实验12

2020-12-27 09:33:46  阅读:198  来源: 互联网

标签:12 int image rd matches lu 实验 include


特征点检测与匹配

#include <bits/stdc++.h>
#include <opencv2/opencv.hpp>
#include "opencv2/core.hpp"
#include "opencv2/imgproc.hpp"
#include "opencv2/video.hpp"
#include "opencv2/objdetect.hpp"
#include "opencv2/imgcodecs.hpp"
#include "opencv2/highgui.hpp"
#include "opencv2/ml.hpp"
#define RATIO    0.2
#define p(a) putchar(a)
#define For(i,a,b) for(int i=a;i<=b;++i)
//by war
//2020.12.27
using namespace std;
using namespace cv;

int w,h,w0,h0,bc0,bc1,cnt;
double fps,part,diff,val;
bool lu_flag,flag;
Point lu,rd,mid,st,ed;
Mat temp,image,IMAGE;

const char * path = "/Users/war/Downloads/VID_20201226_210228.mp4";

void in(int &x){
    int y=1;char c=getchar();x=0;
    while(c<'0'||c>'9'){if(c=='-')y=-1;c=getchar();}
    while(c<='9'&&c>='0'){ x=(x<<1)+(x<<3)+c-'0';c=getchar();}
    x*=y;
}
void o(int x){
    if(x<0){p('-');x=-x;}
    if(x>9)o(x/10);
    p(x%10+'0');
}

void onm ouse(int event, int x, int y, int flags, void *ustc){
    if(event == EVENT_LBUTTONDOWN){
        lu_flag = 1;
        lu = Point(x,y);
    }
    if(event == EVENT_MOUSEMOVE && lu_flag){
        temp = image.clone();
        rd = Point(x,y);
        if(lu != rd) rectangle(temp, lu, rd, Scalar(255, 0, 0), 3);
        imshow("temp",temp);
    }
    if(event == EVENT_LBUTTONUP){
        lu_flag = 0;flag = 1;
        rd = Point(x,y);
        IMAGE = temp(Rect(lu, rd));
    }
}

void init(){
    VideoCapture video(path);
    if(!video.isOpened()){
        cout<<"视频打开失败!"<<endl;
        return;
    }
    fps = video.get(CAP_PROP_FPS);
    part = 1000.0 / fps;
    namedWindow("temp");
    setMouseCallback("temp", onm ouse);
    while(1){
        if(!lu_flag) video >> image;
        if(!image.data || waitKey(part) == 27) break;
        imshow("temp",image);
        if(flag){
            destroyWindow("temp");
            break;
        }
    }
    video.release();
}

void deal(Mat box,Mat scene){
    vector<KeyPoint> keypoints_obj, keypoints_sence;
        Mat descriptors_box, descriptors_sence;
        Ptr<ORB> detector = ORB::create();
        detector->detectAndCompute(scene, Mat(), keypoints_sence, descriptors_sence);
        detector->detectAndCompute(box, Mat(), keypoints_obj, descriptors_box);
        vector<DMatch> matches;

        Ptr<DescriptorMatcher> matcher = makePtr<FlannBasedMatcher>(makePtr<flann::LshIndexParams>(12, 20, 2));
        matcher->match(descriptors_box, descriptors_sence, matches);
        vector<DMatch> goodMatches;
        //printf("total match points : %d\n", (int)matches.size());
        float maxdist = 0;
        for (unsigned int i = 0; i < matches.size(); ++i) {
            maxdist = max(maxdist, matches[i].distance);
        }
        for (unsigned int i = 0; i < matches.size(); ++i) {
            if (matches[i].distance < maxdist*RATIO)
                goodMatches.push_back(matches[i]);
        }
        printf("good match points : %d\n", (int)goodMatches.size());
        Mat dst;
        drawMatches(IMAGE, keypoints_obj, scene, keypoints_sence, goodMatches, dst);
        imshow("output", dst);
}

void tracking(){
    w = abs(lu.x - rd.x);
    h = abs(lu.y - rd.y);
    auto x0 = lu.x - w;
    auto x1 = lu.x + w;
    auto y0 = rd.y - h;
    auto y1 = rd.y + h;
    x0 = max(0, x0);
    x1 = min(x1, image.cols);
    y0 = max(0, y0);
    y1 = min(y1, image.rows);
    VideoCapture video(path);
    if(!video.isOpened()){
        cout<<"视频打开失败!"<<endl;
        return;
    }
    fps = video.get(CAP_PROP_FPS);
    part = 1000.0 / fps;
    namedWindow("tracking");
    while(1){
        video >> image;
        if(!image.data || waitKey(part) == 27) break;
        deal(IMAGE,image);
    }
    video.release();
}

signed main(){
    init();
    tracking();
    waitKey(0);
    return 0;
}

 

 

 

 

标签:12,int,image,rd,matches,lu,实验,include
来源: https://www.cnblogs.com/war1111/p/14195434.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有