资源简介
OpenCV根据轮廓进行倾斜校正。算法参考可以参考我的文章。
代码片段和文件信息
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
using namespace cv;
double angleOfLines(const cv::Vec4i& line1 const cv::Vec4i& line2);
void rotateIamge(cv::Mat& src cv::Mat& dst double angle);
double angleForCorrect(const cv::Vec4i& line);
bool getMinMidX(const cv::Vec4i& line1 const cv::Vec4i& line2);
bool getMaxMidX(const cv::Vec4i& line1 const cv::Vec4i& line2);
bool getMinMidY(const cv::Vec4i& line1 const cv::Vec4i& line2);
bool getMaxMidY(const cv::Vec4i& line1 const cv::Vec4i& line2);
double angle;
double angleOfLines(const cv::Vec4i& line1 const cv::Vec4i& line2)
{
double moduleLine1 = sqrt(pow(line1[0] - line1[2] 2) + pow(line1[1] - line1[3] 2));
double moduleLine2 = sqrt(pow(line2[0] - line2[2] 2) + pow(line2[1] - line2[3] 2));
double dotProduct = (line1[2] - line1[0])*(line2[2] - line2[0]) + (line1[3] - line1[1])*(line2[3] - line2[1]);
return acos(dotProduct / moduleLine1 / moduleLine2) * 180 / CV_PI;
}
double angleForCorrect(const cv::Vec4i& line)
{
Vec4i unitXVector(0 0 1 0);
angle = angleOfLines(unitXVector line); // here angle belongs to [0 pi/2]
// @attention: the increment direction of X and Y axis of OpenCV is different from usual rectangular coordinate system. The origin point is in the upper left corner of the image
if (angle < 45)
{
// consider in the horizontal direction
if (line[1] > line[3])
{
angle = -angle;
}
}
else
{
// consider in the vertical direction
if (line[1] > line[3])
{
angle = 90 - angle;
}
else
{
angle = angle - 90;
}
}
cout << “angle = “ << angle << endl;
return angle;
}
bool getMinMidX(const cv::Vec4i& line1 const cv::Vec4i& line2)
{
return (line1[0] + line1[2]) < (line2[0] + line2[2]); // Although middle point compared there is no need to divide 2
}
bool getMaxMidX(const cv::Vec4i& line1 const cv::Vec4i& line2)
{
return (line1[0] + line1[2]) > (line2[0] + line2[2]);
}
/**
* @brief comparison function for sort sort vector from small to large accodoring to y of the midpoint of each element
* @param line1
* @param line2
* @return
*/
bool getMinMidY(const cv::Vec4i& line1 const cv::Vec4i& line2)
{
return (line1[1] + line1[3]) < (line2[1] + line2[3]);
}
/**
* @brief comparison function for sort sort vector from large to small accodoring to y of the midpoint of each element
* @param line1
* @param line2
* @return
*/
bool getMaxMidY(const cv::Vec4i& line1 const cv::Vec4i& line2)
{
return (line1[1] + line1[3]) > (line2[1] + line2[3]);
}
void rotateIamge(cv::Mat& src cv::Mat& dst double angle)
{
cv::Point2f center(src.cols / 2 src.rows / 2);
cv::Mat rot = getRotationMatrix2D(center angle 1);
cv::Rect box = RotatedRect(center src.size() angle).boundingRect(
相关资源
- 在opencv中将两个图像给融合,让图像
- 使用OpenCV编写一个程序,该程序完成
- 使用 OpenCV 编制一个简单的徒手绘图程
- OpenCV解决PnP问题
- 基于opencv3.2的Moravec、Forstner、Harris算
- opencv+udp+c++ 的摄像头实时传输显示源
- MFC中嵌入显示opencv图像
- opencv调用海康摄像头
- 激光雷达数据读取以及显示C++需配置
- OpenCV在TI 达芬奇以及OMAP平台下的移植
- 双目视觉匹配得到视差图
- CvImgCtrl 演示程序
- 用OpenCV实现Photoshop色阶调整算法
- 贝叶斯抠图C++源代码
- emgucv&opencv图像处理函数说明
- opencv实现分水岭算法
- 人工势场法C++版及利用opencv(or matl
- OpenCV+C++图像处理项目14个
- 用VS2015+opencv3.4.2+C++编写Yolov3目标检测
- 计算图像的平均灰度值
- OpenCV2.4.8
- 基于opencv的人脸识别程序-代码详解
- OpenCV通过直方图均衡化增强图像对比
- OpenCV种子填充实现彩色图像分割的代
- 基于 OPENCV 对三角形 的 角度检测
- 相机标定与图像畸变校正程序
- moravec算子c++代码
- openCV中stitching_detailed.cpp
- 11个常用OpenCV+C++图像处理
- opencv计算5*5邻域方差图
评论
共有 条评论