zoukankan      html  css  js  c++  java
  • c++鼠标点点,获取坐标值,放入到txt文件中

    // oj3.cpp : Defines the entry point for the console application.
    //

    #include "stdafx.h"
    #include<iostream>
    #include<opencv2opencv.hpp>
    #include<opencv2/imgproc/types_c.h>

    #include <opencv2objdetectobjdetect_c.h>
    #include <fstream>

    using namespace cv;
    using namespace std;
    struct my3dim
    {
    int x;
    int y;
    int label;
    };
    vector<my3dim> mypoint1;
    vector<my3dim> mypoint2;
    vector<CvPoint> points1;
    vector<CvPoint> points2;
    vector<CvPoint> points3;
    Mat expanded, img1, img2;
    Point cent;
    my3dim mytmp;
    void onmouse(int event, int x, int y, int flag, void *img)//鼠标事件回调函数,鼠标点击后执行的内容应在此
    {
    switch (event)
    {
    case EVENT_LBUTTONDOWN://鼠标左键按下事件
    cent.x = x;
    cent.y = y;
    mytmp.x = x;
    mytmp.y = y;
    mytmp.label = 1;
    circle(img1, cent, 2, Scalar(255, 255, 0), 3);
    cout << "第一个 " << x << " " << y << endl;
    points1.push_back(CvPoint(x, y));
    mypoint1.push_back(mytmp);
    break;
    case EVENT_RBUTTONDOWN://鼠标左键按下事件
    cent.x = x;
    cent.y = y;
    mytmp.x = x;
    mytmp.y = y;
    mytmp.label = -1;
    circle(img1, cent, 2, Scalar(255, 0, 0), 3);
    cout << "第一个 " << x << " " << y << endl;
    points1.push_back(CvPoint(x, y));
    mypoint1.push_back(mytmp);
    break;
    default:
    break;
    }
    }
    void onmouse2(int event, int x, int y, int flag, void *img)//鼠标事件回调函数,鼠标点击后执行的内容应在此
    {
    switch (event)
    {
    case EVENT_LBUTTONDOWN://鼠标左键按下事件
    cout << "第二个 " << x << " " << y << endl;
    points2.push_back(CvPoint(x, y));
    break;
    case EVENT_LBUTTONUP:

    break;
    default:
    break;
    }
    }
    void onmouse3(int event, int x, int y, int flag, void *img)//鼠标事件回调函数,鼠标点击后执行的内容应在此
    {
    switch (event)
    {
    case EVENT_LBUTTONDOWN://鼠标左键按下事件
    cout << "第二个 " << x << " " << y << endl;
    points3.push_back(CvPoint(x, y));

    cent.x = x;
    cent.y = y;
    if (x <= img1.cols)
    circle(expanded, cent, 2, Scalar(255, 0, 0), 3);
    else
    circle(expanded, cent, 2, Scalar(0, 0, 255), 3);
    break;
    default:
    break;
    }
    //imshow("combined", expanded);
    }

    int main()
    {
    img1 = imread("E:\zhouliyang\fordebug\MVS\data\1452252565655\back\key\00009.jpg");//读取图像
    //从文件中读入图像

    img2 = imread("E:\zhouliyang\fordebug\MVS\data\1452252565655\side2\key\00000.jpg");//读取图像

    namedWindow("第一个图像");//窗口
    //namedWindow("第二个图像", WINDOW_NORMAL);//窗口
    setMouseCallback("第一个图像", onmouse, &img1);//注册鼠标事件到“鼠标画个框”窗口,即使在该窗口下出现鼠标事件就执行onmouse函数的内容,最后一个参数为传入的数据。这里其实没有用到
    //setMouseCallback("第二个图像", onmouse2, &img2);
    imshow("第一个图像", img1);

    while (true)
    {

    imshow("第一个图像", img1);
    int key = waitKey(10);
    if (key == 27)
    break;
    //expanded = srcImg.clone();
    }

    destroyAllWindows();

    fstream outfile("G:\download\points.txt");
    if (!outfile)
    {
    cout << "unable open outfile" << endl;
    exit(1);
    }
    for (int i = 0; i < mypoint1.size(); i++)
    {

    outfile << mypoint1[i].x << " " << mypoint1[i].y << " "<<mypoint1[i].label<<endl;
    }
    outfile.close();

    return 0;
    }

  • 相关阅读:
    jQuery火箭图标返回顶部代码
    jQuery火箭图标返回顶部代码
    jQuery火箭图标返回顶部代码
    jQuery火箭图标返回顶部代码
    C# List分组
    Win7安装VS2019
    C# Lambda Left Join AND Group by Then Sum
    RSA加密解密,Base64String
    Ion-select and ion-option list styling 自定义样式
    Docker镜像
  • 原文地址:https://www.cnblogs.com/wuxiangli/p/6276887.html
Copyright © 2011-2022 走看看