OPENCV C++(六)canny边缘检测+仿射变换+透射变换

图像的缩放

cpp 复制代码
	resize(image, image, Size(round(image.cols * 0.5), round(image.rows * 0.5)));

输入图像 输出图像 大小变换

canny边缘算子的使用

cpp 复制代码
	cvtColor(image, gray, COLOR_BGR2GRAY);
	Canny(gray, canny_mat, 40, 100);

必须先转化为灰度图,作为输入 超过100是真的边缘 低于40是确定不是边缘 在中间若连接边缘 则为边缘
普通旋转缩放变换(仿射变换)

获取仿射变换矩阵

cpp 复制代码
	float angel = -10.0, scale = 1;
	Mat dstmat;
	Point2f center(image.cols * 0.5, image.rows * 0.5);
	Mat affine_matrix = getRotationMatrix2D(center, angel, scale);

获取仿射变换的矩阵 中心点 旋转角度 大小是否变换

-10是顺时针转

仿射变换函数

cpp 复制代码
warpAffine(image, dstmat, affine_matrix,image.size());

输入图 输出图 仿射变换矩阵 画布的大小

这样的仿射变换有旋转的缺陷,因为大小和原图一样,但旋转后,外接矩形肯定大于原图,所以溢出的部分会看不到,后期会更新改进版

点到点的仿射变换(6变量 所以要3个点对3个点)

cpp 复制代码
	Mat affine_Mat;
	const cv::Point2f src_pt[] = {
		  cv::Point2f(100,100),
		  cv::Point2f(20,30),
		  cv::Point2f(70,90),
	};
	const cv::Point2f warp_pt[] = {
		  cv::Point2f(50,100),
		  cv::Point2f(50,20),
		  cv::Point2f(70,96),
	};

	Mat affine_matrix2 = cv::getAffineTransform(src_pt, warp_pt);

	warpAffine(image, affine_Mat, affine_matrix2,image.size());

一个点对应一个点

计算机会帮我们求出仿射变换的矩阵

点到点的透射变换(8变量 所以要4个点对4个点)

cpp 复制代码
	Mat perspective_Mat;
	cv::Point2f pts1[] = {
		cv::Point2f(150,150),
		cv::Point2f(150,300),
		cv::Point2f(350,300),
		cv::Point2f(350,150),

	};
	cv::Point2f pts2[] = {
	cv::Point2f(200,150),
	cv::Point2f(200,300),
	cv::Point2f(340,270),
	cv::Point2f(340,180),
	};

	Mat perspective_matrix = cv::getPerspectiveTransform(pts1, pts2);
	warpPerspective(image, perspective_Mat, perspective_matrix, image.size());

总体代码:

cpp 复制代码
#include <opencv2/opencv.hpp>
#include<iostream>
using namespace std;
using namespace cv;


int main() {
	Mat image = imread("lena.jpeg");
	imshow("lena", image);
	waitKey(0);
	cvDestroyAllWindows();

	resize(image, image, Size(round(image.cols * 0.5), round(image.rows * 0.5)));
	imshow("lena", image);
	waitKey(0);
	cvDestroyAllWindows();

	Mat gray;
	Mat canny_mat;
	cvtColor(image, gray, COLOR_BGR2GRAY);
	Canny(gray, canny_mat, 40, 100);
	imshow("canny_mat", canny_mat);
	waitKey(0);
	cvDestroyAllWindows();	

	float angel = -10.0, scale = 1;
	Mat dstmat;
	Point2f center(image.cols * 0.5, image.rows * 0.5);
	Mat affine_matrix = getRotationMatrix2D(center, angel, scale);
	warpAffine(image, dstmat, affine_matrix,image.size());
	imshow("dstmat", dstmat);
	waitKey(0);
	cvDestroyAllWindows();


	Mat affine_Mat;
	const cv::Point2f src_pt[] = {
		  cv::Point2f(100,100),
		  cv::Point2f(20,30),
		  cv::Point2f(70,90),
	};
	const cv::Point2f warp_pt[] = {
		  cv::Point2f(50,100),
		  cv::Point2f(50,20),
		  cv::Point2f(70,96),
	};

	Mat affine_matrix2 = cv::getAffineTransform(src_pt, warp_pt);

	warpAffine(image, affine_Mat, affine_matrix2,image.size());
	imshow("affine_Mat", affine_Mat);
	waitKey(0);
	cvDestroyAllWindows();



	Mat perspective_Mat;
	cv::Point2f pts1[] = {
		cv::Point2f(150,150),
		cv::Point2f(150,300),
		cv::Point2f(350,300),
		cv::Point2f(350,150),

	};
	cv::Point2f pts2[] = {
	cv::Point2f(200,150),
	cv::Point2f(200,300),
	cv::Point2f(340,270),
	cv::Point2f(340,180),
	};

	Mat perspective_matrix = cv::getPerspectiveTransform(pts1, pts2);
	warpPerspective(image, perspective_Mat, perspective_matrix, image.size());
	imshow("perspective_Mat", perspective_Mat);
	waitKey(0);
	cvDestroyAllWindows();

	//疑问 图像的平移如何实现  image.size()是什么个东西 如何改变图像大小?


	
	return 0;
}
相关推荐
saltymilk8 小时前
C++ 模板参数推导问题小记(模板类的模板构造函数)
c++·模板元编程
感哥8 小时前
C++ lambda 匿名函数
c++
飞哥数智坊10 小时前
GPT-5-Codex 发布,Codex 正在取代 Claude
人工智能·ai编程
倔强青铜三10 小时前
苦练Python第46天:文件写入与上下文管理器
人工智能·python·面试
虫无涯11 小时前
Dify Agent + AntV 实战:从 0 到 1 打造数据可视化解决方案
人工智能
Dm_dotnet13 小时前
公益站Agent Router注册送200刀额度竟然是真的
人工智能
算家计算13 小时前
7B参数拿下30个世界第一!Hunyuan-MT-7B本地部署教程:腾讯混元开源业界首个翻译集成模型
人工智能·开源
机器之心13 小时前
LLM开源2.0大洗牌:60个出局,39个上桌,AI Coding疯魔,TensorFlow已死
人工智能·openai
Juchecar14 小时前
交叉熵:深度学习中最常用的损失函数
人工智能
林木森ai14 小时前
爆款AI动物运动会视频,用Coze(扣子)一键搞定全流程(附保姆级拆解)
人工智能·aigc