opencv c++图像轮廓计算

1、轮廓面积:

        基于格林公式计算:

连续型:                                                               离散型

 2、轮廓周长:

        基于L2范数计算距离。

3、最小外接轮廓:

        矩形,椭圆等外接形状。

4、API

查找轮廓:

void cv::findContours	(	InputArray 	image,
                            OutputArrayOfArrays 	contours,
                            OutputArray 	hierarchy,
                            int 	mode,
                            int 	method,
                            Point 	offset = Point() 
                            )	

 绘制轮廓:


void cv::drawContours	(	InputOutputArray 	image,
                            InputArrayOfArrays 	contours,
                            int 	contourIdx,
                            const Scalar & 	color,
                            int 	thickness = 1,
                            int 	lineType = LINE_8,
                            InputArray 	hierarchy = noArray(),
                            int 	maxLevel = INT_MAX,
                            Point 	offset = Point() 
                            )	

5、示例代码

        代码展示了对二值化图像的轮廓查找轮廓绘制轮廓面积及周长的计算和应用轮廓外接矩形椭圆带角度的矩形角度信息的提取

void QuickDemo::contour(Mat& image)
{
	//高斯模糊
	GaussianBlur(image, image, Size(3, 3), 0);
	Mat gray;
	cvtColor(image, gray, COLOR_BGR2GRAY);

	Mat binary;
	threshold(gray, binary, 0, 255, THRESH_BINARY_INV | THRESH_OTSU);
	namedWindow("THRESH_OTSU", WINDOW_FREERATIO);
	imshow("THRESH_OTSU", binary);
	//查找轮廓
	vector<vector<Point>> contours;
	vector<Vec4i> hierachy;
	findContours(binary, contours, hierachy, RETR_EXTERNAL, CHAIN_APPROX_SIMPLE, Point());
	for (size_t t = 0; t < contours.size(); ++t) {
		
		double area = contourArea(contours[t]);
		double size = arcLength(contours[t], true);//只记录了闭合轮廓

		cout << "第" << t << "个轮廓的面积为" << area << endl;
		cout << "第" << t << "个轮廓的周长为" << size << endl << endl;
		//根据面积和周长过滤,然后绘制轮廓或绘制外接图形
		if (area > 100 || size > 100)continue;

		//轮廓
		//drawContours(image, contours, t, Scalar(0, 0, 255), 2, 8);
		//外接矩形
		/*Rect box = boundingRect(contours[t]);
		rectangle(image, box, Scalar(0, 0, 255), 2, 8);*/

		
		RotatedRect rt = minAreaRect(contours[t]);
		//椭圆
		ellipse(image, rt, Scalar(0, 0, 255), 2, 8);
		//有角度的矩形绘制
		Point2f pts[4];
		rt.points(pts);
		for (int i = 0; i < 4; ++i) {
			line(image, pts[i], pts[(i + 1) % 4], Scalar(0, 0, 255), 2, 8);
		}
		//角度获取
		cout << rt.angle << endl;
	}
	imshow("contours", image);

}

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 您好,以下是计算轮廓直径的 OpenCV C 代码示例: ```c #include <opencv2/opencv.hpp> #include <iostream> using namespace cv; using namespace std; int main() { Mat image = imread("image.jpg", IMREAD_GRAYSCALE); if (image.empty()) { cout << "Could not open or find the image" << endl; return -1; } // Find contours vector<vector<Point>> contours; findContours(image, contours, RETR_EXTERNAL, CHAIN_APPROX_SIMPLE); // Find the largest contour int maxContourIndex = ; double maxContourArea = ; for (int i = ; i < contours.size(); i++) { double contourArea = contourArea(contours[i]); if (contourArea > maxContourArea) { maxContourIndex = i; maxContourArea = contourArea; } } // Calculate the diameter of the largest contour RotatedRect boundingBox = minAreaRect(contours[maxContourIndex]); double diameter = max(boundingBox.size.width, boundingBox.size.height); cout << "The diameter of the largest contour is: " << diameter << endl; return ; } ``` 希望对您有所帮助。 ### 回答2: 计算轮廓直径的代码主要步骤如下: 1. 导入OpenCV库文件,并引用相应的命名空间。 ```cpp #include <opencv2/opencv.hpp> using namespace cv; ``` 2. 读取图像,并将其转换为灰度图像。 ```cpp Mat image = imread("input.jpg"); cvtColor(image, image, COLOR_BGR2GRAY); ``` 3. 对图像进行二值化处理。 ```cpp threshold(image, image, 127, 255, THRESH_BINARY); ``` 4. 查找轮廓。 ```cpp vector<vector<Point>> contours; vector<Vec4i> hierarchy; findContours(image, contours, hierarchy, RETR_EXTERNAL, CHAIN_APPROX_SIMPLE); ``` 5. 计算每个轮廓的直径,并找到最大直径。 ```cpp double maxDiameter = 0; for (int i = 0; i < contours.size(); i++) { double diameter = sqrt(contourArea(contours[i]) / CV_PI) * 2; if (diameter > maxDiameter) { maxDiameter = diameter; } } ``` 6. 输出最大直径。 ```cpp cout << "轮廓直径:" << maxDiameter << endl; ``` 完整代码如下: ```cpp #include <opencv2/opencv.hpp> using namespace cv; int main() { Mat image = imread("input.jpg"); cvtColor(image, image, COLOR_BGR2GRAY); threshold(image, image, 127, 255, THRESH_BINARY); vector<vector<Point>> contours; vector<Vec4i> hierarchy; findContours(image, contours, hierarchy, RETR_EXTERNAL, CHAIN_APPROX_SIMPLE); double maxDiameter = 0; for (int i = 0; i < contours.size(); i++) { double diameter = sqrt(contourArea(contours[i]) / CV_PI) * 2; if (diameter > maxDiameter) { maxDiameter = diameter; } } cout << "轮廓直径:" << maxDiameter << endl; return 0; } ``` 请注意,以上代码仅为伪代码示例,实际应用中可能需要对图像进行预处理、参数调整等操作,以获取更准确的轮廓直径结果。具体情况需根据实际需进行相应的修改和优化。 ### 回答3: 计算轮廓直径的代码主要分为以下几个步骤: 1. 导入opencv库和其他必要的库文件。 ```c #include <opencv2/opencv.hpp> #include <iostream> ``` 2. 读取图像并转为灰度图像。 ```c cv::Mat src = cv::imread("input.jpg"); cv::Mat gray; cv::cvtColor(src, gray, cv::COLOR_BGR2GRAY); ``` 3. 进行二值化处理。 ```c cv::Mat binary; cv::threshold(gray, binary, 0, 255, cv::THRESH_BINARY | cv::THRESH_OTSU); ``` 4. 提取轮廓。 ```c std::vector<std::vector<cv::Point>> contours; cv::findContours(binary, contours, cv::RETR_EXTERNAL, cv::CHAIN_APPROX_SIMPLE); ``` 5. 计算每个轮廓的直径。 ```c for (int i = 0; i < contours.size(); i++) { cv::RotatedRect rect = cv::minAreaRect(contours[i]); float diameter = std::max(rect.size.width, rect.size.height); std::cout << "Contour " << i + 1 << " diameter: " << diameter << std::endl; } ``` 6. 显示结果。 ```c cv::imshow("Source Image", src); cv::imshow("Binary Image", binary); cv::waitKey(0); ``` 以上是使用opencv c计算轮廓直径的基本代码。其中,要注意图像读取的路径、二值化方法的选择,以及输出结果的形式等。根据实际需,可以对代码进行进一步的扩展和优化。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值