获取图像中的点坐标并保存在txt中

获取摄像头中的图像,通过鼠标选中点坐标并保存在txt中

#include <QCoreApplication>
#include <iostream>
#include <fstream>
#include "opencv2/opencv.hpp"

using namespace std;
using namespace cv;

//设置一个全局变量保存onMouse函数获取的坐标点
cv::Point2f point;

//获取坐标点并保存在txt中
void onMouse(int event, int x, int y, int flags, void*)
{
    if (event == cv::EVENT_LBUTTONDOWN)
    {
        //获取坐标
        point  = cv::Point2f(static_cast<float>(x), static_cast<float>(y));
        std::cout << "click point is: " << point << std::endl;

        //保存坐标
        ofstream point_out;
        point_out.open("point.txt", std::ios::out | std::ios::app);  //以写入和在文件末尾添加的方式打开.txt文件,没有的话就创建该文件。
        //如果没有文件那么就结束程序
        if(!point_out.is_open()){
            cout << "file open fail" << endl;
            exit(0);
        }
        point_out << point << endl;
        point_out.close();
    }
}

int main(int argc, char *argv[])
{
    std::string url = "url";
    cv::VideoCapture cap(url);
    cv::Mat mat;

    //判断相机是否打开
    if (!cap.isOpened()){
        cout << "could not open camera" << endl;
        return -1;
    }

    //从摄像头中获取一张图片并显示
    cap.read(mat);
    cv::namedWindow("source_image", cv::WINDOW_AUTOSIZE);
    std::cout << "Please click points" << std::endl;
    //点击图片获得坐标
    cv::setMouseCallback("source_image", onMouse);
    cv::imshow("source_image", mat);
    cv::waitKey(0);

    cap.release();
    cv::destroyAllWindows();
}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值