c++鼠标点点,获取坐标值,放入到txt文件中
2017-01-12 09:47
567 查看
// oj3.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include<iostream>
#include<opencv2\opencv.hpp>
#include<opencv2/imgproc/types_c.h>
#include <opencv2\objdetect\objdetect_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 << "\t" << mypoint1[i].y << "\t"<<mypoint1[i].label<<endl;
}
outfile.close();
return 0;
}
//
#include "stdafx.h"
#include<iostream>
#include<opencv2\opencv.hpp>
#include<opencv2/imgproc/types_c.h>
#include <opencv2\objdetect\objdetect_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 << "\t" << mypoint1[i].y << "\t"<<mypoint1[i].label<<endl;
}
outfile.close();
return 0;
}
相关文章推荐
- C++获取鼠标坐标并移动鼠标
- C++获取鼠标坐标并移动鼠标
- Kinect获取深度图,鼠标点击获取该点三维坐标并保存txt
- C++学习--向txt文件写入数据、读取数据、获取行数以及删除文件
- 002--VS C++ 获取鼠标坐标并显示在窗口上
- C++ 读取txt文件中,n行两点坐标值
- c++--------获取某个路径下所有文件的文件名,读写TXT文件到新的文件
- C++获取程序窗口外的鼠标坐标
- C++读取txt文件获取txt文件行数
- C++获取目录下所有文件列表函数
- 获取鼠标坐标,鼠标所在窗体句柄
- JS在AJAX中获取鼠标坐标
- How to get the file's icon using C++ - 用C++如何获取文件的图标
- C/C++多种方法获取文件大小
- 获取鼠标当前位置坐标的方法 - 回复 "ps8.0" 同学
- C/C++获取文件大小的多种方法
- 获取鼠标坐标
- 显示桌面 图标 创建可以新建一个txt文件,把以下内容放入,名字改为显示桌面 .scf
- How to get the size of file in C++ - 在C++中如何获取文件的大小
- js 获取鼠标点击坐标