OpenCV单应矩阵应用——更换广告牌内容

最近看了公众号“计算机视觉life”的一篇推送,讲的是单应矩阵的原理,最后给出了更换广告牌的作业,代码框架给出,要自己实现单应矩阵的应用。先看一下效果:

更换后的效果:

以下是实现代码:

#include <opencv2/opencv.hpp>
#include <opencv2/highgui/highgui_c.h>

using namespace cv;
using namespace std;

struct userdata {
	Mat im;
	vector<Point2f> points;
};


void mouseHandler(int event, int x, int y, int flags, void* data_ptr)
{
	if (event == EVENT_LBUTTONDOWN)
	{
		userdata *data = ((userdata *)data_ptr);
		circle(data->im, Point(x, y), 3, Scalar(0, 255, 255), 5, CV_AA);
		imshow("Image", data->im);
		if (data->points.size() < 4)
		{
			data->points.push_back(Point2f(x, y));
		}
	}

}



int main(int argc, char** argv)
{

	// Read in the image.
	//Mat im_src = imread("first-image.jpg");
	Mat im_src = imread("cvlife.jpg");
	Size size = im_src.size();

	// Create a vector of points.
	vector<Point2f> pts_src;
	pts_src.push_back(Point2f(0, 0));
	pts_src.push_back(Point2f(size.width - 1, 0));
	pts_src.push_back(Point2f(size.width - 1, size.height - 1));
	pts_src.push_back(Point2f(0, size.height - 1));



	// Destination image
	//Mat im_dst = imread("times-square.jpg");
	Mat im_dst = imread("ad1.jpg");


	// Set data for mouse handler
	Mat im_temp = im_dst.clone();
	userdata data;
	data.im = im_temp;


	//show the image
	imshow("Image", im_temp);

	cout << "Click on four corners of a billboard and then press ENTER" << endl;
	//set the callback function for any mouse event
	setMouseCallback("Image", mouseHandler, &data);
	waitKey(0);

	Mat H = findHomography(pts_src, data.points,0);
	Mat logo;
	warpPerspective(im_src, logo,H,im_dst.size());
	
	//阈值化
	Mat logo1;
	threshold(logo,logo1,0,255,1);

	im_dst.copyTo(logo,logo1);//把im_dst和logo1做 与 运算后再和logo相加

	imwrite("final.jpg",logo);

	// Display image.


	imshow("Image", logo);

	waitKey(0);

	return 0;
}

大家可以通过这段代码做出一些更有意思的图片,快发挥你的想象力,动手试试吧!

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
单应矩阵(Homography Matrix)是一种用于图像处理和计算机视觉中的变换矩阵,它可以将一个图像中的特定区域映射到另一个图像中的对应区域。在 OpenCV 中,可以使用函数 `findHomography()` 来计算两个图像之间的单应矩阵。 下面是一个简单的示例,演示如何使用 OpenCV 中的 `findHomography()` 函数来计算两个图像之间的单应矩阵: ```python import cv2 import numpy as np # 读入两个图像 img1 = cv2.imread('image1.png') img2 = cv2.imread('image2.png') # 提取两个图像中的特征点 orb = cv2.ORB_create() kp1, des1 = orb.detectAndCompute(img1, None) kp2, des2 = orb.detectAndCompute(img2, None) # 使用特征点匹配算法找到两个图像中的匹配点对 bf = cv2.BFMatcher(cv2.NORM_HAMMING, crossCheck=True) matches = bf.match(des1, des2) # 选取前50个最佳匹配点对 matches = sorted(matches, key=lambda x: x.distance) good_matches = matches[:50] # 提取匹配点对的坐标 src_pts = np.float32([kp1[m.queryIdx].pt for m in good_matches]).reshape(-1, 1, 2) dst_pts = np.float32([kp2[m.trainIdx].pt for m in good_matches]).reshape(-1, 1, 2) # 计算单应矩阵 H, mask = cv2.findHomography(src_pts, dst_pts, cv2.RANSAC, 5.0) # 使用单应矩阵对图像进行透视变换 img3 = cv2.warpPerspective(img1, H, (img2.shape[1], img2.shape[0])) # 显示结果 cv2.imshow('img1', img1) cv2.imshow('img2', img2) cv2.imshow('img3', img3) cv2.waitKey(0) cv2.destroyAllWindows() ``` 在上面的示例中,我们首先使用 `ORB` 特征检测器提取两个图像中的特征点,然后使用 `BFMatcher` 算法找到两个图像中的匹配点对。接下来,我们使用 `cv2.findHomography()` 函数计算两个图像之间的单应矩阵,并使用 `cv2.warpPerspective()` 函数对图像进行透视变换,最后将结果显示出来。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值