提取有角度的形状的最小矩形,求其旋转角度、长宽



这个程序搞了许久

#include <iostream>
#include <vector>
#include <opencv2/opencv.hpp> 
#include <opencv2/core/core.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>


#pragma comment(lib,"opencv_core249d.lib")      
#pragma comment(lib,"opencv_highgui249d.lib")      
#pragma comment(lib,"opencv_imgproc249d.lib") 

#define PI 3.1415926

using namespace std;
using namespace cv;


int main()
{
	// Read input binary image
	RNG rng(12345);
	char *image_name = "P2.png";
	Mat image = imread(image_name, 0);
	if (!image.data)
		return 0;

	namedWindow("Binary Image");
	imshow("Binary Image", image);

	// 从文件中加载原图  
	Mat pSrcImage = imread(image_name, CV_LOAD_IMAGE_UNCHANGED);
	//IplImage *pSrcImage = cvLoadImage("D2.bmp", CV_LOAD_IMAGE_UNCHANGED);

	// 转为2值图
	threshold(pSrcImage, pSrcImage, 200, 255, cv::THRESH_BINARY_INV);
	//cvThreshold(pSrcImage, pSrcImage, 200, 255, cv::THRESH_BINARY_INV);

	imwrite("binary.jpg", image);

	vector<std::vector<cv::Point>> contours;
	Mat canny_output;
	Canny(pSrcImage, canny_output, 80, 255, 3);
	vector<Vec4i> hierarchy;
	findContours(canny_output, contours, hierarchy, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE, Point(0, 0));

	/// 对每个找到的轮廓创建可倾斜的边界框和椭圆
	vector<RotatedRect> minRect(contours.size());
	vector<RotatedRect> minEllipse(contours.size());
	for (int i = 0; i < contours.size(); i++)
	{
		minRect[i] = minAreaRect(Mat(contours[i]));
		if (contours[i].size() > 5)
		{
			minEllipse[i] = fitEllipse(Mat(contours[i]));
		}
	}
	/// 绘出轮廓及其可倾斜的边界框和边界椭圆

	CvBox2D     End_Rage2D;
	CvPoint2D32f rectpoint[4];
	Point2f rect_points[4];
	
	Mat drawing = Mat::zeros(canny_output.size(), CV_8UC3);
	for (int i = 0; i< contours.size(); i++)
	{
		//End_Rage2D = cvMinAreaRect2(contour);
		End_Rage2D = minAreaRect(Mat(contours[i]));
		Scalar color = Scalar(rng.uniform(0, 255), rng.uniform(0, 255), rng.uniform(0, 255));
		// contour
		drawContours(drawing, contours, i, color, 1, 8, vector<Vec4i>(), 0, Point());
		// ellipse  椭圆
		//ellipse(drawing, minEllipse[i], color, 2, 8);
		// rotated rectangle
		Point2f rect_points[4]; minRect[i].points(rect_points);
		for (int j = 0; j < 4; j++)
		{
         line(drawing, rect_points[j], rect_points[(j + 1) % 4], color, 1, 8);
		 cout << "坐标点:"<<rect_points[j] << "........" << rect_points[(j + 1) % 4] << endl;
		}
		
		
			int aaa = 0, bbb = 0, k = 0;
			aaa = (int)sqrt((pow((rect_points[0].x - rect_points[1].x), 2) + pow((rect_points[0].y - rect_points[1].y), 2)));
			bbb = (int)sqrt((pow((rect_points[0].x - rect_points[3].x), 2) + pow((rect_points[0].y - rect_points[3].y), 2)));

			if (aaa<bbb)
			{
				k = aaa;
				aaa = bbb;
				bbb = k;
			}
			printf("最小外接矩形的长为:%d,宽为:%d。\n\n", aaa, bbb);
		    cout << " angle:\n" << (float)End_Rage2D.angle << std::endl;      //被测物体旋转角度 
	}    

	namedWindow("Contours", CV_WINDOW_AUTOSIZE);
	imshow("Contours1", drawing);



	以上成功提取斜框/

	waitKey(0);
	return 0;

}





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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值