cv透视变换(鼠标拾取图像角点)

目录

思路

鼠标拾取图像四个角点,然后定义透视变换后图像尺寸,得到透视变换后图像。

源码

//********************************************************************************
//
//             透视变换
//
//********************************************************************************

#include<opencv2/opencv.hpp>
#include<iostream>

//变换后尺寸
#define HANG 600 
#define LIE 600

using namespace cv;
using namespace std;

Mat src, resultImg;
vector<Point2f> src_corner;
void tou_shi();
void on_Mouse(int event, int x, int y, int flags, void* param);

int main()
{
	src = imread("1.png");
	if (src.empty())
	{
		cout << "图像未找到" << endl;
		return -1;
	}
	imshow("yuan_image", src);

	setMouseCallback("yuan_image", on_Mouse);//窗口名必须和之前创建窗口同名

	waitKey(0);
	return 0;

}

void on_Mouse(int event, int x, int y, int flags, void* param)
{
	if (event == CV_EVENT_LBUTTONDOWN)//鼠标点击将会触发此事件
	{
		cout << "x:" << x << "y:" << y << endl;
		Point2f  thispoint;
		thispoint.x = x;
		thispoint.y = y;
		src_corner.push_back(thispoint);
	}
	if (src_corner.size() == 4)
		tou_shi();
}

//透视变换 
void tou_shi()
{
	//透视变换后的四个点所在坐标
	vector<Point2f> dst_corner(4);
	dst_corner[0] = Point(0, 0);
	dst_corner[1] = Point(HANG, 0);
	dst_corner[2] = Point(HANG, LIE);
	dst_corner[3] = Point(0, LIE);

	Mat M = getPerspectiveTransform(src_corner, dst_corner);  //求变换矩阵
	warpPerspective(src, resultImg, M, cv::Size(HANG,LIE), INTER_LINEAR);//透视
	imshow("result_image", resultImg);
	imwrite("tou_shi.png ", resultImg);
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值
>