0
点赞
收藏
分享

微信扫一扫

Tomcat的介绍和安装配置、eclipse中动态web项目的创建和运行、使用IDEA创建web项目并运行

三次方 2023-08-08 阅读 63
vector<Vec2f> lines1;
HoughLines(canny_mat, lines1, 1, CV_PI / 180.0,90 );//45可以检测里面两条线 80检测出外边两条线

 定义存放输出线的向量 此向量输出有<距离,角度>

因为检测的原理就是在变换霍夫空间里面去检测的,这里可以理解为极坐标

第3个参数是距离精度 第四个参数是角度精度,第五个是阈值,只有点超过90个才算一条线

在图中画线操作:

Point ptz1, ptz2;
for (size_t i = 0; i < lines1.size(); ++i) {
float rth = lines1[i][0];//距离
float theta = lines1[i][1];//角度

double a = cos(theta);
double b = sin(theta);
double x0 = a * rth, y0 = b * rth;

ptz1.x = cvRound(x0 + 1000 * (-b));
ptz1.y = cvRound(y0 + 1000 * (a));
ptz2.x = cvRound(x0 - 1000 * (-b));
ptz2.y = cvRound(y0 - 1000 * (a));

line(image1, ptz1, ptz2, Scalar(0, 255, 0), 2, 8);

}

这里是画线操作 

vector<Vec4i>lines2;
HoughLinesP(canny_mat, lines2, 1, CV_PI / 180.0, 25,25,32);//参数如何设置啊? 点数多少 最小长度 最大容忍间隔

后面三个参数依次是

// 大于阈值threshold的线段才可以被检测通过并返回到结果中。

// 表示最低线段的长度,比这个设定参数短的线段就不能被显现出来

// 允许将同一行点与点之间连接起来的最大的距离 

	Point ptz3, ptz4;
for (int j = 0; j < lines2.size(); j++) {



ptz3.x = lines2[j][0];
ptz3.y = lines2[j][1];
ptz4.x = lines2[j][2];
ptz4.y = lines2[j][3];

line(image2, ptz3, ptz4, Scalar(0, 255, 255), 2, 8);

}

 



	Mat binary;
threshold(gray, binary, 84, 255, THRESH_OTSU);
imshow("binary", binary);
waitKey(0);
cvDestroyAllWindows();

vector<vector<Point>> contours;
findContours(binary, contours, RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE);
Mat image3clone = image3.clone();

for (int i = 0; i < contours.size(); i++) {
drawContours(image3clone, contours, i, Scalar(0, 255, 255), 2, 8);
}

先是二值化图像,再用findcontours函数寻找轮廓,后面两个参数是寻找最外轮廓,内接轮廓就不管,用的是简单保存的方式。

画出轮廓用的是drawcontours来画轮廓,直接在图像上画出轮廓即可

定义4个点存储外接矩形定点,minAreaRect是外接矩形的rect,rbox.points是赋值给vtx,然后用一个简单的画线操作,依次画点和点的连线

前面的获得矩阵操作一样

	float angel = -10.0, scale = 1;
Point2f center(lena.cols * 0.5, lena.rows * 0.5);
Mat rot = getRotationMatrix2D(center, angel, scale);

获得外接矩形的作为最后的大小

Rect bbox = RotatedRect(center, lena.size(), angel).boundingRect();//获取外接矩形

然后得调整rot矩阵得参数,因为中心点有所偏移,需要平移操作

rot.at<double>(0, 2) += bbox.width / 2.0 - center.x;//调整仿射变换矩阵参数【a,b,l/n b,c,m】此步在调节l,m值
rot.at<double>(1, 2) += bbox.height / 2.0 - center.y;//
Mat dst;
warpAffine(lena, dst, rot, bbox.size());

即可完成没有缺损得旋转图像!

举报

相关推荐

0 条评论