Opencv 图像边缘拉伸

需求如下图,需要将绿色点围成的区域(记做inside)到红色点围成的区域(记做outside)拉伸到蓝色点(图片中有两圈蓝点,本文以内圈蓝点为准)围成的区域(记做affine),并且绿色区域内部的图像保持不变,仅拉伸边缘区域,原图中红色区域以外的图像信息忽略。

原理:将红点与绿点间的区域切分为多个三角形,记住srcTriangles, 将蓝点与绿点间的区域也切分为多个三角形,记住dstTriangles。使用仿射变换,将srcTriangles变换为dstTriangles即可实现边缘拉伸的效果。

注意三角形切分时,srcTriangles与dstTriangles一一对应


涉及Opencv API:getAffineTransform(仿射变换)

仿射变换的用法:(三点确定仿射矩阵)

		src_tri[0] = Point2f(srcTriangles[i].p1.x, srcTriangles[i].p1.y);
		src_tri[1] = Point2f(srcTriangles[i].p2.x, srcTriangles[i].p2.y);
		src_tri[2] = Point2f(srcTriangles[i].p3.x, srcTriangles[i].p3.y);

		dst_tri[0] = Point2f(dstTriangles[i].p1.x, dstTriangles[i].p1.y);
		dst_tri[1] = Point2f(dstTriangles[i].p2.x, dstTriangles[i].p2.y);
		dst_tri[2] = Point2f(dstTriangles[i].p3.x, dstTriangles[i].p3.y);

		Mat warp_mat_org_inv = cv::getAffineTransform(dst_tri, src_tri);

实现需求的核心代码:

Mat deform(std::vector<Triangle> srcTriangles, std::vector<Triangle> dstTriangles, Mat srcImage) {
	int nIsoSize = 512;
	Mat result = Mat::zeros(nIsoSize, nIsoSize, CV_8UC3);

	for (int i = 0; i < srcTriangles.size(); i++)
	{
		cv::Point2f src_tri[3];
		cv::Point2f dst_tri[3];

		src_tri[0] = Point2f(srcTriangles[i].p1.x, srcTriangles[i].p1.y);
		src_tri[1] = Point2f(srcTriangles[i].p2.x, srcTriangles[i].p2.y);
		src_tri[2] = Point2f(srcTriangles[i].p3.x, srcTriangles[i].p3.y);

		dst_tri[0] = Point2f(dstTriangles[i].p1.x, dstTriangles[i].p1.y);
		dst_tri[1] = Point2f(dstTriangles[i].p2.x, dstTriangles[i].p2.y);
		dst_tri[2] = Point2f(dstTriangles[i].p3.x, dstTriangles[i].p3.y);

		Mat warp_mat_org_inv = cv::getAffineTransform(dst_tri, src_tri);
		warp_mat_org_inv.convertTo(warp_mat_org_inv, CV_32FC1);

		for (int x = min(dst_tri[0].x, min(dst_tri[1].x, dst_tri[2].x)); x < max(dst_tri[0].x, max(dst_tri[1].x, dst_tri[2].x)); ++x) {
			for (int y = min(dst_tri[0].y, min(dst_tri[1].y, dst_tri[2].y)); y < max(dst_tri[0].y, max(dst_tri[1].y, dst_tri[2].y)); ++y) {
				if (is_point_in_triangle(cv::Point2f(x, y), dst_tri[0], dst_tri[1], dst_tri[2])) {

					// calculate corresponding position of dst_coord pixel center in image (src)
					const Mat homogenous_dst_coord(Vec3f(x, y, 1.0f));
					const Vec2f src_texel = Mat(warp_mat_org_inv * homogenous_dst_coord);

					if ((round(src_texel[1]) < srcImage.rows) && (round(src_texel[0]) < srcImage.cols) && round(src_texel[0]) > 0 && round(src_texel[1]) > 0)
					{
						int a1 = round(src_texel[1]);
						int a2 = round(src_texel[0]);

						result.at<cv::Vec3b>(y, x)[0] = srcImage.at<cv::Vec3b>(round(src_texel[1]), round(src_texel[0]))[0];
						result.at<cv::Vec3b>(y, x)[1] = srcImage.at<cv::Vec3b>(round(src_texel[1]), round(src_texel[0]))[1];
						result.at<cv::Vec3b>(y, x)[2] = srcImage.at<cv::Vec3b>(round(src_texel[1]), round(src_texel[0]))[2];
					}
				}
			}
		}
	}

	return result;
}

最终效果:(第一张为原图,第二张为边缘拉伸,中间不拉伸的效果)



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值