c++鼠標點點,獲取坐標值,放入到txt文件中


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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM