OpenCV在轮廓周围拟合最小矩形或多边形(C#)

API:

  1. ApproxPolyDP:拟合最小多边形
public static Point[] ApproxPolyDP(IEnumerable<Point> curve, double epsilon, bool closed);

参数:
curve:轮廓点集合;
epsilon:主要表示输出的精度,就是另个轮廓点之间最大距离数;
closed:表示输出的多边形是否封闭,true:封闭

  1. BoundingRect/MinAreaRect:拟合最小矩形(不旋转或旋转)
//拟合最小矩形,不旋转
public static Rect BoundingRect(InputArray curve);

//拟合最小矩形旋转
public static RotatedRect MinAreaRect(IEnumerable<Point> points);

图像处理流程:
一般视图像质量,对图像可先进行模糊处理(均值模糊),二值化后,提取图像边缘,再提取图像轮廓(一般只提取外轮廓),最后调用相关API提取图像几何特征

演示:


picFile = fileDialog.FileName;
inputMat = Cv2.ImRead(picFile, ImreadModes.Grayscale);
displayMat = Cv2.ImRead(picFile, ImreadModes.AnyColor);
outMat = new Mat(new Size(inputMat.Cols, inputMat.Rows), inputMat.Type());

//二值化
Cv2.Threshold(inputMat, inputMat,106 ,255, ThresholdTypes.Binary);

//提取边缘
Cv2.Canny(inputMat, outMat, 30, 90);


//仅查找外轮廓
Cv2.FindContours(outMat, out OpenCvSharp.Point[][] contours, out HierarchyIndex[] outputArray, RetrievalModes.External, ContourApproximationModes.ApproxNone);


//绘制轮廓
Scalar color = new Scalar(255, 0, 0);
for (int i = 0; i < contours.Length; i++)
{
    Mat rMat = new Mat();


    //拟合最小多边形
    contours[i] = Cv2.ApproxPolyDP(contours[i], 3, true);

    Rect rect = new Rect();
    RotatedRect rotatedRect = new RotatedRect();
    //拟合轮廓周围最小矩形--不旋转
    rect = Cv2.BoundingRect(contours[i]);

    //拟合轮廓周围最小矩形--旋转
    rotatedRect = Cv2.MinAreaRect(contours[i]);

    //获取旋转矩形四个顶点
    var rectPoints = rotatedRect.Points();

    //绘制矩形
    Cv2.Line(displayMat, ((Point)rectPoints[0]), ((Point)rectPoints[1]), color, 4);
    Cv2.Line(displayMat, ((Point)rectPoints[1]), ((Point)rectPoints[2]), color, 4);
    Cv2.Line(displayMat, ((Point)rectPoints[2]), ((Point)rectPoints[3]), color, 4);
    Cv2.Line(displayMat, ((Point)rectPoints[3]), ((Point)rectPoints[0]), color, 4);



}

picBox_Display.Image = OpenCvSharp.Extensions.BitmapConverter.ToBitmap(inputMat);
picBox_After.Image = OpenCvSharp.Extensions.BitmapConverter.ToBitmap(displayMat);


原始图像
在这里插入图片描述

提取效果
在这里插入图片描述

补充说明:
本案例在.NET使用的OpenCV库为OpenCvSharp4

.NET 环境的OpenCv库

  • 2
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
拟合四边形,可以使用 OpenCV 中的函数 cv::approxPolyDP。该函数可以将给定的轮廓逼近为指定精度的多边形,因此可以用于拟合四边形。 以下是一个简单的示例代码,演示如何使用 cv::approxPolyDP 拟合四边形: ```c++ #include <opencv2/opencv.hpp> #include <iostream> int main() { cv::Mat image = cv::imread("input.jpg"); // 转换为灰度图像 cv::Mat gray; cv::cvtColor(image, gray, cv::COLOR_BGR2GRAY); // 二值化 cv::Mat binary; cv::threshold(gray, binary, 128, 255, cv::THRESH_BINARY); // 查找轮廓 std::vector<std::vector<cv::Point>> contours; cv::findContours(binary, contours, cv::RETR_EXTERNAL, cv::CHAIN_APPROX_SIMPLE); // 遍历每个轮廓 for (const auto& contour : contours) { // 拟合多边形 std::vector<cv::Point> polygon; cv::approxPolyDP(contour, polygon, 0.01 * cv::arcLength(contour, true), true); // 如果是四边形,则绘制轮廓 if (polygon.size() == 4) { cv::drawContours(image, std::vector<std::vector<cv::Point>>{polygon}, -1, cv::Scalar(0, 0, 255), 2); } } cv::imshow("output", image); cv::waitKey(0); return 0; } ``` 在上面的示例代码中,我们首先将输入图像转换为灰度图像,然后对其进行二值化。接下来,我们使用 cv::findContours 查找图像中的轮廓。对于每个轮廓,我们使用 cv::approxPolyDP 拟合多边形,并检查是否是四边形。如果是,则在原始图像上绘制轮廓。 请注意,这只是一个简单的示例代码,实际应用中可能需要进行更多的预处理和参数调整。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值