Opencv学习(15)—warpPerspective()

理论

在这里插入图片描述
在这里插入图片描述

warpPerspective()函数

主要作用:对图像进行透视变换,就是变形

void cv::warpPerspective	(	InputArray 	src,
OutputArray 	dst,
InputArray 	M,
Size 	dsize,
int 	flags = INTER_LINEAR,
int 	borderMode = BORDER_CONSTANT,
const Scalar & 	borderValue = Scalar() 
)	
	
Python:
dst	=	cv.warpPerspective(	src, M, dsize[, dst[, flags[, borderMode[, borderValue]]]]	)

参数:
int flags=INTER_LINEAR:输出图像的插值方法
int borderMode=BORDER_CONSTANT:图像边界的处理方式
const Scalar& borderValue=Scalar():边界的颜色设置,一般默认是0

实践

/****************************
 * 实现虚拟广告牌的效果。
 * 提供两张图,一张是笑脸图,另外一张是带广告牌的原图,请用单应矩阵实现将原图中广告牌替换为提供的笑脸图的效果。
 * 利用OpenCV函数,通过鼠标点击来选择要替换的广告牌的四个顶点。
****************************/

#include <opencv2/opencv.hpp>

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("/home/xiaohu/learn_SLAM/zuoye12/homography_matrix/xiaolian.png");
    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("/home/xiaohu/learn_SLAM/zuoye12/homography_matrix/ad.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);

    // ----------  开始你的代码  --------------
    // 计算原图四个角点和目标图区域对应角点的 Homography
    Mat Homography=findHomography(pts_src,data.points);
    // 用H对原图做变换
    warpPerspective(im_src,im_temp,Homography,im_temp.size());

    // 提取鼠标点击的四个角点
    Point pts_dst[4];
    for(int i=0;i<4;i++)
    {
        pts_dst[i]=data.points[i];
    }
    // 把目标图中对应区域像素值设置为0
    fillConvexPoly(im_dst,pts_dst,4,Scalar(0),CV_AA);
    // 把原图叠加到目标图上
    im_dst = im_dst + im_temp;
    // ----------  结束你的代码  --------------
    
    // Display image.
    imshow("Image", im_dst);
    imwrite("../Image.jpg",im_dst);
    waitKey(0);

    return 0;
}

在这里插入图片描述

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

令狐少侠、

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值