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;
    }

  • 相关阅读:
    201720181 JaWorld 第三周作业
    201720181 Java小组1623 第一周作业
    201720181 JaWorld 第四、五周作业
    201720181 Java小组1623 第二周作业
    无法打开用户默认数据库的解决方法
    技术网站/博客网址收藏
    .net中防止用户多次登录的方法
    asp.net导出excel方法总结
    面向对象设计模式之Bridge桥接模式(结构型)
    面向对象设计模式之Composite组合模式(结构型)
  • 原文地址:https://www.cnblogs.com/wuxiangli/p/6276887.html
Copyright © 2011-2022 走看看