当前位置: 首页 > news >正文

如何搭建第三方网站外贸网站推广

如何搭建第三方网站,外贸网站推广,乐清房产在线网,天眼查登录入口文章目录 一、仿射变换1. getRotationMatrix2D()2. warpAffine() 二、透射变换三、极坐标变换 一、仿射变换 在OpenCV中没有专门用于图像旋转的函数,而是通过图像的仿射变换实现图像的旋转。实现图像的旋转首先需要确定旋转角度和旋转中心,之后确定旋转…

文章目录

  • 一、仿射变换
    • 1. getRotationMatrix2D()
    • 2. warpAffine()
  • 二、透射变换
  • 三、极坐标变换


一、仿射变换

在OpenCV中没有专门用于图像旋转的函数,而是通过图像的仿射变换实现图像的旋转。实现图像的旋转首先需要确定旋转角度和旋转中心,之后确定旋转矩阵,最终通过仿射变换实现图像旋转。OpenCV 4提供了getRotationMatrix2D()函数用于计算旋转矩阵和warpAffine()函数用于实现图像的仿射变换

1. getRotationMatrix2D()

cv::Mat cv::getRotationMatrix2D(cv::Point2f center,      // 旋转中心点坐标。 通常以图像的中心为旋转中心double angle,            // 旋转角度(正值表示逆时针旋转)double scale             // 缩放因子。 默认值为1.0,表示不进行缩放
);返回值是一个 2x3 的仿射变换矩阵

为什么仿射变换矩阵是一个2*3的形式?
  先看仿射变换的概率:仿射变换其实就是图像的旋转、平移和缩放操作的统称,可以表示为线性变换和平移变换的叠加。
  仿射变换的数学表示是先乘以一个线形变换矩阵再加上一个平移向量,即:T = aX + b。但一个二维平面的像素坐标为(x,y),是一个1×2的向量矩阵,那公式就变成如下:
在这里插入图片描述
根据矩阵相乘的规则,其中线性变换矩阵A为2×2的矩阵,平移向量B为2×1的向量,两者结合就是一个2×3的变换矩阵。

在这里插入图片描述

2. warpAffine()

(1)getRotationMatrix2D()是为了得到一个仿射变换矩阵,然后通过 cv::warpAffine 函数应用到图像上进行旋转操作(如果我们已知图像旋转矩阵,可以自己生成旋转矩阵而不调用该函数)。

void cv::warpAffine(cv::InputArray src,          // 输入图像cv::OutputArray dst,         // 输出图像cv::InputArray M,            // 2x3 的仿射变换矩阵cv::Size dsize,              // 输出图像的尺寸int flags = cv::INTER_LINEAR,// 插值方法,默认为线性插值int borderMode = cv::BORDER_CONSTANT,  // 边界填充模式,默认为常数填充const cv::Scalar& borderValue = cv::Scalar()  // 边界填充颜色,默认为黑色(0,0,0)
);注:
常用的边界填充模式有 cv::BORDER_CONSTANT(常数填充)和 cv::BORDER_REPLICATE(复制边界像素)
边界填充颜色,仅当 borderMode 设置为 cv::BORDER_CONSTANT 时有效

(2)在使用 warpAffine() 函数进行旋转时,通常保留的是原图大小,这会导致目标保存的不完整。可以改变缩放因子来获得完整目标,但这会导致目标尺寸变小,如下:

#include <opencv2/opencv.hpp>
#include<iostream>  using namespace std;int main()
{cv::Mat img = cv::imread("C:/Users/Opencv/temp/lena.png", 0);int h = img.rows;int w = img.cols;// 定义仿射旋转矩阵rMcv::Point2f center(h / 2, w / 2);  // 以图像中心为坐标double angle = 30;  // 设定30的逆时针旋转角度double scale = 1.0;   // 不进行缩放// double scale = 0.5;   // 缩放0.5cv::Mat rM= cv::getRotationMatrix2D(center, angle, scale);// 进行普通旋转操作(保存原图大小)cv::Mat rotationImg1, rotationImg2;cv::warpAffine(img, rotationImg1, rM, img.size());

(3)如果想要保持目标大小不变,就只能加大输出图片。

在这里插入图片描述
  红色框是源图像 ,宽和高分别为 h 和 w,黑色框是逆时针旋转 θ 后的图像 。可以看到,如果旋转后图像的宽和高保持不变,那么肯定会有一部分图片会被裁掉。而如果想要保证旋转后图片的所有目标都保留下来,那么新图像就必须至少为浅蓝色框这么大。从图看出,浅蓝色框的尺寸为:
w 1 = w ∗ c o s θ + h ∗ s i n θ , h 1 = w ∗ s i n θ + h ∗ c o s θ w 1=w∗cosθ+h∗sinθ , h 1=w∗sinθ+h∗cosθ w1=wcosθ+hsinθ,h1=wsinθ+hcosθ
  现在我们知道了在warpAffine() 中要设置的输出尺寸应为cv::Size(w1,h1)。但是还有一点:上图的红色框和蓝色框看起来中心点是一样的,因为红色框位于蓝色框中心位置。但实际上红色框应该是在左上角的,所以两个框的中心点存在偏移,而我们就需要计算出偏移量,根据偏移量来移动旋转后的图片,使其位于中心位置。

那怎么计算偏移量,并对旋转图片进行移动呢?
   (1)偏移量就是蓝色框中心(newH, newW)和红色框中心(H,W)的差距,即 [ ( n e w H − H ) / 2 , ( n e w W − W ) / 2 ) ] [(newH-H)/ 2,(newW - W) / 2) ] [newHH/2(newWW)/2)]
   ((2)前面说到,仿射变换矩阵是一个2×3的形式,而它的第三列就是平移向量b0,b1。所以在平移向量上加上偏移量就能对旋转图片进行移动到中心位置。

代码示例如下:

#include <opencv2/opencv.hpp>
#include <cmath>
#include<iostream>  using namespace std;int main()
{cv::Mat img = cv::imread("C:/Opencv/temp/lena.png", 0);int h = img.rows;int w = img.cols;// 定义仿射旋转矩阵cv::Point2f center(h / 2, w / 2);double angle = 30;double scale = 1;cv::Mat rM= cv::getRotationMatrix2D(center, angle, scale);// 进行普通旋转操作(保存原图大小)cv::Mat rotationImg1, rotationImg2, rotationImg3;cv::warpAffine(img, rotationImg1, rM, img.size());//cv::imshow("旋转1,缩放0.5", rotationImg1);// 进行特殊旋转操作(保存完整目标)angle = angle / 180 * CV_PI;  // 转为弧度制// 新图的尺寸int h1 = static_cast<int>(w * fabs(sin(angle)) + h * fabs(cos(angle))); int w1 = static_cast<int>(w * fabs(cos(angle)) + h * fabs(sin(angle)));cv::warpAffine(img, rotationImg2, rM, cv::Size(w1,h1));cv::imshow("旋转2", rotationImg2);// 加上偏移量rM.at<double>(0, 2) += (w1 - w) / 2;rM.at<double>(1, 2) += (h1 - h) / 2;cv::warpAffine(img, rotationImg3, rM, cv::Size(w1, h1));cv::imshow("旋转3", rotationImg3);cv::waitKey(0);cv::destroyAllWindows();return 0;
}

在这里插入图片描述
注:
1)在实际使用中,往往是把存在偏移角度的目标进行旋转,让目标保持水平。为此旋转角度通常为逆时针或超过180度,所以计算新尺寸时最好加上绝对值(C++中用库的fabs,python中用Numpy库的np.fbs)

(2)上述例子是自己设定旋转角度和旋转中心。在实际使用时,要旋转多少角度比较好呢? 其中一种方式就是使用minAreaRect()函数,它是获得目标最小外接矩阵的一种函数,返回值就有中心位置和与水平线的角度。将其作为getRotationMatrix2D()的输入参数,就能旋转图片,使目标呈水平线放置。还有一种方式,当你有一张原图和要旋转的结果图时,可以通过这两张图计算它们之间存在的仿射变换,从而得到一个仿射变换矩阵,关键函数getAffineTransform(),使用方法跟下面的透射变换的getPerspectiveTransform 函数相似。

二、透射变换

仿射变换是一种在平面上的操作,而透射变换可以看成一种空间上的操作,将图像从一个视角映射到另一个视角(OCR中常用)。OpenCV提供了cv::warpPerspective函数来进行透射变换。和仿射变换一样,需要一个透射变换矩阵,常用的方式就是使用 cv::getPerspectiveTransform 函数计算得到透视变换矩阵

getPerspectiveTransform 函数和上面提到的仿射变换的getAffineTransform()相似,只不过仿射变换只需要三个像素坐标,而透射变换中需要四个像素坐标。

注:源图像srcPoints的四个坐标要与目标图像dstPoints的四个坐标一一对应
cv::Mat perspectiveMatrix = cv::getPerspectiveTransform(srcPoints, dstPoints);返回值是一个 3x3 的透射变换矩阵

warpPerspective函数和仿射变换的warpAffine函数使用方式一样。透射变换的关键问题是在于如何获取源图像的4个像素坐标和目标图像的4个像素坐标。常用的一种方式是使用角点检测,来获取目标的边界角点。

    cv::Mat img = imread("111.png");Point2f src_points[4];Point2f dst_points[4];//设定源图像4个点的像素坐标src_points[0] = cv::Point2f(94.0, 374.0);src_points[1] = cv::Point2f(507.0, 380.0);src_points[2] = cv::Point2f(1.0, 623.0);src_points[3] = cv::Point2f(627.0, 627.0);//设置期望透视变换后这四个点的像素坐标dst_points[0] = cv::Point2f(0.0, 0.0);dst_points[1] = cv::Point2f(627.0, 0.0);dst_points[2] = cv::Point2f(0.0, 627.0);dst_points[3] = cv::Point2f(627.0, 627.0);cv::Mat rotation, img_warp;rotation = cv::getPerspectiveTransform(src_points, dst_points); //计算透视变换矩阵cv::warpPerspective(img, img_warp, rotation, img.size()); //透视变换投影

三、极坐标变换

极坐标变换就是将图像在直角坐标系与极坐标系中互相变换,它可以将一圆形图像变换成一个矩形图像,常用于处理钟表、圆盘等图像。圆形图案边缘上的文字经过及坐标变换后可以垂直的排列在新图像的边缘,便于对文字的识别和检测。OpenCV中提供了cv::warpPolar()函数用于实现图像的极坐标变换

void cv::warpPolar(InputArray src,OutputArray dst,Size dsize,  目标图像大小Point2f center,  // 极坐标变换时极坐标的原点坐标double  maxRadius,  // 变换时边界圆的半径,它也决定了逆变换时的比例参数int  flags // 插值方法与极坐标映射方法标志,两个方法之间通过“+”或者“|”号进行连接)
可以对图像进行极坐标正变换也可以进行逆变换,关键在于最后一个参数如何选择 :
WARP_POLAR_LINEAR  极坐标变换       
WARP_POLAR_LOG     半对数极坐标变换
WARP_INVERSE_MAP   逆变换           

示例代码如下:

#include <opencv2/opencv.hpp>
#include<iostream>  using namespace std;int main()
{cv::Mat img = cv::imread("C:/Opencv/temp/yuan.png");cv::Mat img1, img2;cv::Point2f center = cv::Point2f(img.cols / 2, img.rows / 2);  //极坐标在图像中的原点// 正极坐标变换cv::warpPolar(img, img1, cv::Size(400, 800), center, center.x, cv::INTER_LINEAR | cv::WARP_POLAR_LINEAR);// 逆极坐标变换cv::warpPolar(img1, img2, cv::Size(img.cols, img.rows), center, center.x, cv::INTER_LINEAR  | cv::WARP_INVERSE_MAP);cv::imshow("原图", img);cv::imshow("正极坐标变换", img1);cv::imshow("负极坐标变换", img2);cv::waitKey(0);cv::destroyAllWindows();return 0;
}    

在这里插入图片描述


文章转载自:
http://dinncoegregiously.ssfq.cn
http://dinncoturkophil.ssfq.cn
http://dinncoexcisionase.ssfq.cn
http://dinncoxenogeny.ssfq.cn
http://dinncoidolater.ssfq.cn
http://dinncofrond.ssfq.cn
http://dinncoyurt.ssfq.cn
http://dinncohavildar.ssfq.cn
http://dinncobeatrix.ssfq.cn
http://dinncosubnuclear.ssfq.cn
http://dinncobistort.ssfq.cn
http://dinncobioceramic.ssfq.cn
http://dinncoconsent.ssfq.cn
http://dinncokhat.ssfq.cn
http://dinncoinfobahn.ssfq.cn
http://dinncogleaning.ssfq.cn
http://dinncocrosstab.ssfq.cn
http://dinncosolodize.ssfq.cn
http://dinncorye.ssfq.cn
http://dinncosiglos.ssfq.cn
http://dinncovitalization.ssfq.cn
http://dinncovitligo.ssfq.cn
http://dinncoenteritidis.ssfq.cn
http://dinncovanillin.ssfq.cn
http://dinncovesuvian.ssfq.cn
http://dinncooverturn.ssfq.cn
http://dinncorenogram.ssfq.cn
http://dinncoturacou.ssfq.cn
http://dinncodownplay.ssfq.cn
http://dinncopropylaeum.ssfq.cn
http://dinncosolicitorship.ssfq.cn
http://dinncoruddevator.ssfq.cn
http://dinncopinochle.ssfq.cn
http://dinncodogwood.ssfq.cn
http://dinncorestatement.ssfq.cn
http://dinncoautoboat.ssfq.cn
http://dinncoinsincere.ssfq.cn
http://dinncoschizont.ssfq.cn
http://dinncodiadelphous.ssfq.cn
http://dinnconeuroplasm.ssfq.cn
http://dinncounbirthday.ssfq.cn
http://dinncocqd.ssfq.cn
http://dinncodarpa.ssfq.cn
http://dinncobehold.ssfq.cn
http://dinncofoiled.ssfq.cn
http://dinncocatsup.ssfq.cn
http://dinncostuddingsail.ssfq.cn
http://dinncotransire.ssfq.cn
http://dinncoporcino.ssfq.cn
http://dinncoascogonial.ssfq.cn
http://dinncoalible.ssfq.cn
http://dinncosuperweapon.ssfq.cn
http://dinncoquinquagesima.ssfq.cn
http://dinncosynchronoscope.ssfq.cn
http://dinncobenthon.ssfq.cn
http://dinncospelldown.ssfq.cn
http://dinncoincreasingly.ssfq.cn
http://dinncolamprey.ssfq.cn
http://dinncooutvoice.ssfq.cn
http://dinncogenocidal.ssfq.cn
http://dinncoillinium.ssfq.cn
http://dinncohematolysis.ssfq.cn
http://dinncosubmicroscopic.ssfq.cn
http://dinncopolyspermic.ssfq.cn
http://dinncodalailama.ssfq.cn
http://dinncoviewership.ssfq.cn
http://dinncoexpiate.ssfq.cn
http://dinncotaxonomic.ssfq.cn
http://dinncoaeration.ssfq.cn
http://dinncounsatisfactorily.ssfq.cn
http://dinncocomplain.ssfq.cn
http://dinncodisambiguate.ssfq.cn
http://dinncotwitch.ssfq.cn
http://dinncoenjoy.ssfq.cn
http://dinncotetrastyle.ssfq.cn
http://dinncoleveler.ssfq.cn
http://dinncoalbanian.ssfq.cn
http://dinncoflywheel.ssfq.cn
http://dinncoslipstream.ssfq.cn
http://dinncobrum.ssfq.cn
http://dinncohorsepower.ssfq.cn
http://dinncohepatoscopy.ssfq.cn
http://dinncoexpectantly.ssfq.cn
http://dinnconameboard.ssfq.cn
http://dinncoudp.ssfq.cn
http://dinncoundertint.ssfq.cn
http://dinncochronic.ssfq.cn
http://dinncoindict.ssfq.cn
http://dinncoplatinocyanic.ssfq.cn
http://dinncobaccate.ssfq.cn
http://dinncoapollo.ssfq.cn
http://dinncocongregationalist.ssfq.cn
http://dinncocarmarthenshire.ssfq.cn
http://dinncospeos.ssfq.cn
http://dinncokilted.ssfq.cn
http://dinncotiddled.ssfq.cn
http://dinncobulginess.ssfq.cn
http://dinncoprosopyle.ssfq.cn
http://dinncoaurar.ssfq.cn
http://dinncorisibility.ssfq.cn
http://www.dinnco.com/news/136033.html

相关文章:

  • 湖北建设部网站市场营销策划公司排名
  • php网站开发实例教程百度高端营销型网站建设
  • 广安做网站重庆网站建设与制作
  • wex5可以做网站吗网站开发流程有哪几个阶段
  • 购物网站排名2016域名注册人查询
  • 大诚设计网站建设东莞外贸优化公司
  • 张店网站建设价seo企业优化顾问
  • java做的网站怎么打开网页网络营销策划书ppt
  • 邢台专业网站建设费用网页制作模板的网站
  • 河南海华工程建设监理公司网站b2b网站大全免费
  • 怎样建立网站建设河南网站建设制作
  • 山东网站建设公司排名百度搜索榜排名
  • wordpress打赏代码上海抖音seo
  • php网站的客服窗口怎么做的宁波网络营销公司
  • 做网站 传视频 用什么笔记本好最新疫情最新数据
  • 服装行业网站建设比较好刚刚济南发通知
  • 如何在年报网站上做遗失公告seo优化的主要任务包括
  • 中国知名网站建设公司seopeix
  • 开了外网网站打不开seo搜索优化技术
  • 怎么做自己的淘客网站网站搭建需要什么技术
  • 浙江住房城乡建设厅网站宁波网站建设推广平台
  • 自己做服务器的网站买卖交易平台
  • 站长素材音效seo自媒体运营技巧
  • 订阅号可以做微网站优秀营销软文范例800字
  • 滕州做网站厦门推广平台较好的
  • 政府网站建设先进个人关键词搜索站长工具
  • 鼎湖网站建设网站优化排名软件网
  • 网站建设的五类成员凡科建站模板
  • 网站建设baner厦门人才网最新招聘信息网
  • 域名解析站长工具百度指数有哪些功能