说明
1、MinAreaRect()函数可以寻找最小外接矩形;返回一个旋转矩形,旋转矩形的旋转角度一定是负(angle < 0),表示逆时针旋转,顺时针为正;


2、最小外接矩形可以用来进行矩形的旋转校正(应用);
Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Emgu.CV;
using Emgu.Util;
using Emgu.CV.Util;
using Emgu.CV.Structure;
using Emgu.CV.CvEnum;
using System.Drawing;
namespace lesson25
{
class Program
{
static void Main(string[] args)
{
//旋转矩形
//Mat src = CvInvoke.Imread("mask2.jpg");
//Mat dst = src.Clone();
//Mat grayimg = new Mat();
//CvInvoke.Imshow("input", src);
//CvInvoke.CvtColor(src, grayimg, ColorConversion.Bgr2Gray);
//CvInvoke.Threshold(grayimg, grayimg, 100, 255, ThresholdType.Binary);
//VectorOfVectorOfPoint contours = new VectorOfVectorOfPoint();
//VectorOfRect hierarchy = new VectorOfRect();
//CvInvoke.FindContours(grayimg, contours, hierarchy, RetrType.Tree, ChainApproxMethod.ChainApproxNone);
//CvInvoke.DrawContours(dst, contours, -1, new MCvScalar(255, 255, 0), 2);
//for (int i = 0; i < contours.Size; i++)
//{
// RotatedRect rotatedRect = CvInvoke.MinAreaRect(contours[i]); //计算最小外接矩形
// PointF[] pointFs = new PointF[4]; //存储旋转矩形的4个顶点
// pointFs = rotatedRect.GetVertices();
// CvInvoke.PutText(dst, "rotated rectangle", new Point((int)pointFs[1].X, (int)pointFs[1].Y), FontFace.HersheyDuplex,
// 1.2, new MCvScalar(0, 0, 255), 2);
// for (int j = 0; j < 4; j++)
// {
// CvInvoke.Line(dst, new Point((int)pointFs[j].X, (int)pointFs[j].Y), new Point((int)pointFs[(j + 1) % 4].X, (int)pointFs[(j + 1) % 4].Y),
// new MCvScalar(0, 0, 255), 2);
// }
//}
//CvInvoke.Imshow("result", dst);
//CvInvoke.WaitKey(0);
///测量物体宽高
//Mat src = CvInvoke.Imread("cup.jpg");
//Mat dst = src.Clone();
//Mat grayImg = new Mat();
//CvInvoke.Imshow("input", src);
//CvInvoke.CvtColor(src, grayImg, ColorConversion.Bgr2Gray);
//CvInvoke.Threshold(grayImg, grayImg, 100, 255, ThresholdType.BinaryInv);
1537



被折叠的 条评论
为什么被折叠?



