gpt4 book ai didi

c# - 使用CVInvoke.AbsDiff方法从 mask 中检测椭圆

转载 作者:行者123 更新时间:2023-12-02 16:25:03 25 4
gpt4 key购买 nike

我的椭圆随着时间而增长。

enter image description here

为了检测椭圆,我使用了CvInvoke.AbsDiff方法。
我得到这样的图像
enter image description here

我想将此椭圆设置为fit-椭圆方法并获取其半径es。

这就是我采取的方法。

            CvInvoke.AbsDiff(First, img, grayscale);
CvInvoke.CvtColor(grayscale, grayscale, ColorConversion.Bgr2Gray);
CvInvoke.GaussianBlur(grayscale, grayscale, new System.Drawing.Size(11, 11), 15, 15);
CvInvoke.Threshold(grayscale, grayscale, Convert.ToInt16(Threshold), Convert.ToInt16(Threshold * 2), ThresholdType.Binary );
Mat element = CvInvoke.GetStructuringElement(Emgu.CV.CvEnum.ElementShape.Rectangle, new System.Drawing.Size(3, 3), new System.Drawing.Point(-1, -1));

CvInvoke.Dilate(grayscale, grayscale, element, new System.Drawing.Point(-1, 1), 5, BorderType.Constant, new MCvScalar(255, 255, 255));
CvInvoke.Canny(grayscale, grayscale, Threshold, MaxThreshold * 2, 3);
VectorOfVectorOfPoint contours = new VectorOfVectorOfPoint();
CvInvoke.FindContours(grayscale, contours, null, RetrType.Ccomp, ChainApproxMethod.ChainApproxTc89Kcos);


double area = 0;
double ContourArea = 0;
int contour = 0;
int CenterX;
int CenterY;
for (int i = 0; i < contours.Size; i++)
{

System.Drawing.Rectangle rec = CvInvoke.BoundingRectangle(contours[i]);

output.Draw(rec, new Bgr(255, 0, 255), 2);
CenterX = ((rec.Width) / 2) + rec.X;
CenterY = ((rec.Height) / 2) + rec.Y;
ContourArea = rec.Width * rec.Height; ;

if ((HWidth - CenterFactor) < CenterX && CenterX < (HWidth + CenterFactor) && (HHeight - CenterFactor) < CenterY && CenterY< (HHeight + CenterFactor) )
{
if (ContourArea < 1000000)
if (area < ContourArea)
{
area = ContourArea;
contour = i;

}
}
}
//if (contour == 0)
//{
// return arr;
//}
System.Drawing.Rectangle rect = CvInvoke.BoundingRectangle(contours[contour]);
output.Draw(rect, new Bgr(0, 255, 0), 3);

但是我并不是每次都能得到最好的椭圆。这就是我得到的轮廓 enter image description here

还有其他方法吗?

最佳答案

尽管此方法并不完全完美,但这可能是您可能采取的方向。

Mat input = CvInvoke.Imread(@"C:\Users\ajones\Desktop\Images\inputImg.png", ImreadModes.AnyColor);
Mat input2 = input.Clone();

Mat thresh = new Mat();
CvInvoke.GaussianBlur(input, thresh, new System.Drawing.Size(7, 7), 10, 10);

CvInvoke.Threshold(thresh, thresh, 3, 10, ThresholdType.Binary);

CvInvoke.Imshow("The Thresh", thresh);
CvInvoke.WaitKey(0);

Mat output = new Mat();
CvInvoke.CvtColor(thresh, output, ColorConversion.Bgr2Gray);

VectorOfVectorOfPoint contours = new VectorOfVectorOfPoint();

CvInvoke.FindContours(output, contours, null, RetrType.External, ChainApproxMethod.ChainApproxSimple);
CvInvoke.DrawContours(input, contours, -1, new MCvScalar(0, 255, 0), 3, LineType.FourConnected);

CvInvoke.Imshow("The Image", input);
CvInvoke.WaitKey(0);

int biggest = 0;
int index = 0;
for (int i = 0; i<contours.Size; i++)
{
if (contours[i].Size > biggest)
{
biggest = contours[i].Size;
index = i;
}
}

CvInvoke.DrawContours(input2, contours, index, new MCvScalar(0, 255, 0), 3, LineType.FourConnected);

CvInvoke.Imshow("The Image2", input2);
CvInvoke.WaitKey(0);

首先使用高斯滤镜模糊图像。
enter image description here

然后,使用二进制阈值。
enter image description here

然后,找到图像上的所有轮廓
enter image description here

最后,您要做的就是对轮廓进行排序,直到找到最大的轮廓为止。
enter image description here

就像我说的那样,它并不完全完美,但是我应该帮助您朝着正确的方向发展。

关于c# - 使用CVInvoke.AbsDiff方法从 mask 中检测椭圆,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61944191/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com