当前位置:网站首页>How opencv implements image skew correction
How opencv implements image skew correction
2022-08-01 18:00:00 【Yisuyun】
opencvHow to implement image skew correction
今天小编给大家分享一下opencvHow to realize the relevant knowledge points of image tilt correction,内容详细,逻辑清晰,相信大部分人都还太了解这方面的知识,所以分享这篇文章给大家参考一下,希望大家阅读完这篇文章后有所收获,下面我们一起来了解一下吧.
Today's task is to picture these two situations,My current situation is the same as pictured,The left image is slightly tilted to the left,Then I need to push the left side down for tilt correction.右图,Right tilt I need to push the right side down to a horizontal position for tilt correction.My scene doesn't have a large angle of tilt,It's probably not more than that45°的小角度.
Standard traditional image processing methods.The first is grayscale,二值化,取轮廓,Filter to get the target(面积最大的那个),得到倾斜角度,转正.
The specific process is not described in detail,You can see it all in the code.效果图如下:
#include <iostream>#include"opencv2/opencv.hpp"using namespace std;using namespace cv;bool cmp_x(cv::Point pt1, cv::Point pt2){ return (pt1.x < pt2.x);}cv::Point2f get_mid_pt(cv::Point2f pt1, cv::Point2f pt2){ return cv::Point2f((pt1.x + pt2.x)/2.0,(pt1.y + pt2.y) / 2.0);}double get_point_angle(cv::Point2f pointO,cv::Point2f pointA){ double angle = 0; cv::Point2f point; double temp; point = cv::Point2f((pointA.x - pointO.x), (pointA.y - pointO.y)); if ((0==point.x) && (0==point.y)) { return 0; } if (0==point.x) { angle = 90; return angle; } if (0==point.y) { angle = 0; return angle; } temp = fabsf(float(point.y)/float(point.x)); temp = atan(temp); temp = temp*180/CV_PI ; if ((0<point.x)&&(0<point.y)) { angle = 360 - temp; return angle; } if ((0>point.x)&&(0<point.y)) { angle = 360 - (180 - temp); return angle; } if ((0<point.x)&&(0>point.y)) { angle = temp; return angle; } if ((0>point.x)&&(0>point.y)) { angle = 180 - temp; return angle; } printf("sceneDrawing :: getAngle error!"); return -1;}int RotateImage(const cv::Mat &src, double angle, cv::Mat &dst, cv::Mat &rot_matrix, bool crop = true, int flags = cv::INTER_NEAREST, int borderMode = cv::BORDER_CONSTANT, const cv::Scalar &borderValue = cv::Scalar()){ if(0 == src.cols * src.rows) { return 0;} cv::Point2f center(src.cols / 2.0f, src.rows / 2.0f); rot_matrix = cv::getRotationMatrix2D(center, angle, 1.0); if (crop) { if (dst.data == NULL) { dst = cv::Mat(src.rows, src.cols, src.type()); } } else { cv::Rect bbox = cv::RotatedRect(center, cv::Size2f(src.cols, src.rows), angle).boundingRect(); double *p = (double *) rot_matrix.data; p[2] += bbox.width / 2.0 - center.x; p[5] += bbox.height / 2.0 - center.y; if (dst.rows != bbox.height || dst.cols != bbox.width) { dst = cv::Mat(bbox.height, bbox.width, src.type()); } } cv::warpAffine(src, dst, rot_matrix, dst.size(), flags, borderMode, borderValue); return 0;}int main(int argc, char *argv[]){ cv::Mat img = cv::imread("/data_1/everyday/0325/13.jpeg"); cv::Mat m_gray,m_bi; cv::cvtColor(img,m_gray,CV_BGR2GRAY); cv::threshold(m_gray,m_bi,100,255,THRESH_BINARY_INV); vector<vector<Point>> contours; vector<Vec4i> hierarchy; findContours(m_bi,contours,hierarchy,RETR_TREE,CHAIN_APPROX_SIMPLE,Point()); RotatedRect rt_rot_max,rt_tmp; int max_szie = -1; for(int i=0;i<contours.size();i++) { rt_tmp = minAreaRect(Mat(contours[i])); if(rt_tmp.size.area() > max_szie) { max_szie = rt_tmp.size.area(); rt_rot_max = rt_tmp; } } std::vector<cv::Point2f> v_pt(4); rt_rot_max.points(v_pt.data()); std::sort(v_pt.begin(),v_pt.end(),cmp_x); cv::Point2f pt_left = get_mid_pt(v_pt[0], v_pt[1]); cv::Point2f pt_right = get_mid_pt(v_pt[2], v_pt[3]); double ang = get_point_angle(pt_left,pt_right); std::cout<<"ang="<<ang<<std::endl; cv::circle(img,v_pt[0],6,Scalar(50,12,189),3); cv::circle(img,v_pt[1],6,Scalar(10,255,255),3); cv::circle(img,v_pt[2],6,Scalar(150,120,19),3); cv::circle(img,v_pt[3],6,Scalar(0,0,0),3); cv::Mat m_rot,rot_matrix; RotateImage(img, -ang, m_rot, rot_matrix, false); cv::imshow("m_rot",m_rot); cv::imshow("m_bi",m_bi); cv::imshow("m_gray",m_gray); cv::imshow("img",img); cv::waitKey(0); return 0;}
以上就是“opencvHow to implement image skew correction”这篇文章的所有内容,感谢各位的阅读!相信大家阅读完这篇文章都有很大的收获,小编每天都会为大家更新不同的知识,如果还想学习更多的知识,请关注亿速云行业资讯频道.
边栏推荐
猜你喜欢
【Error】Uncaught (in promise) TypeError: Cannot read properties of undefined (reading ‘concat’)
XAML WPF item groupBox control
【报错】Uncaught (in promise) TypeError: Cannot read properties of undefined (reading ‘concat‘)
MySQL Lock wait timeout exceeded; try restarting transaction 锁等待
TCP百万并发服务器优化调参
程序员架构修炼之道:如何设计“易理解”的系统架构?
OnePlus 10RT appears on Geekbench, product launch also seems to be approaching
Xingtu has been short of disruptive products?Will this M38T from the Qingdao factory be a breakthrough?
计算IoU(D2L)
OpenCV installation, QT, VS configuration project settings
随机推荐
Shell nl命令详解(显示行号、读取文件)
JVM运行时数据区与JMM内存模型是什么
后台管理系统的权限思路
golang json returns null
EpiSci|片上系统的深度强化学习:神话与现实
gtk显示4通道rgba图像
OpenCV安装、QT、VS配置项目设置
XAML WPF项目groupBox控件
Detailed explanation of DBPack SQL Tracing function and data encryption function
开发工具:第五章:使用idea生成实体类
Solve the problem that MySQL cannot insert Chinese data
打开微信客服
【报错】Uncaught (in promise) TypeError: Cannot read properties of undefined (reading ‘concat‘)
Tower Defense Shoreline User Agreement
C语言理论--笔试面试基础稳固
【Day_10 0428】井字棋
【Day_12 0507】查找组成一个偶数最接近的两个素数
【Day_08 0426】两种排序方法
BITS Pilani|SAC-AP:基于 Soft Actor Critic 的深度强化学习用于警报优先级
移动端吸顶方案