c# 中的矢量<double> 区域(contour.size())?中心质量,面积。粒子分析

vector<double> areas(contour.size()) in c#? mass of center, area. Particle Analyze

你好,我想用 EmguCV 分析粒子。我使用它显然是因为我使用 C#。我发现了一些似乎对我来说很完美的代码,但它是用 C++ 编写的,所以我试图在 C# 中获取它 运行。现在我被卡住了,因为我没有找到类似于这段代码的东西,它在 c#

中工作

另外我想说的是我是一个菜鸟程序员,c#对我来说也是新手

这是 C++ 中的代码

Mat frame = imread("particles.png", CV_LOAD_IMAGE_GRAYSCALE);

threshold(frame, frame, 127, 255, THRESH_BINARY);

vector<vector<Point> > contours;
vector<Vec4i> hierarchy;

findContours(frame, contours, hierarchy, CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE, Point(0, 0));

vector<double> areas(contours.size());

for (int i = 0; i < contours.size(); i++)
    areas[i] = contourArea(contours[i]);

vector<Point2d> mass_centres(contours.size());

for (int i = 0; i < contours.size(); i++)
{
    const Moments mu = moments(contours[i], false);
    mass_centres[i] = Point2d(mu.m10 / mu.m00, mu.m01 / mu.m00);
}

cout << "Num particles: " << contours.size() << endl;

for (int i = 0; i < contours.size(); i++)
    cout << "area " << (i + 1) << ": " << areas[i] << endl;

for (int i = 0; i < contours.size(); i++)
    cout << "centre " << (i + 1) << ": " << mass_centres[i].x << " " << mass_centres[i].y << endl;

return 0;

}

到目前为止,这就是我用 C# 编写的内容

        Mat frame = CvInvoke.Imread(file2, ImreadModes.Grayscale);

        CvInvoke.Threshold(frame, frame, 127, 255, ThresholdType.Binary);

        Emgu.CV.Util.VectorOfVectorOfPoint contours = new Emgu.CV.Util.VectorOfVectorOfPoint();

        Mat hierarchy = new Mat();

        CvInvoke.FindContours(frame, contours, hierarchy, RetrType.Tree, ChainApproxMethod.ChainApproxSimple);

现在我被困在这里,我现在无法替代此代码:

vector<double> areas(contours.size());

for (int i = 0; i < contours.size(); i++)
    areas[i] = contourArea(contours[i]);

vector<Point2d> mass_centres(contours.size());

for (int i = 0; i < contours.size(); i++)
{
    const Moments mu = moments(contours[i], false);
    mass_centres[i] = Point2d(mu.m10 / mu.m00, mu.m01 / mu.m00);
}

您要查找的函数是CvInvoke.ContourArea and CvInvoke.Moments

代码可以这样写:

List<double> areas = new List<double>();
foreach(Emgu.CV.Util.VectorOfPoint contour in contours)
{
    areas.Add(Emgu.CV.CvInvoke.ContourArea(contour));
} 

VectorOfPoint mass_centers = new VectorOfPoint();
foreach(Emgu.CV.Util.VectorOfPoint contour in contours)
{
    Emgu.CV.Structure.MCVMoments mu = Emgu.CV.CvInvoke.Moments(contour); 
    mass_centers.Push(Emgu.CV.Point2D<double>(mu.M10 / mu.M00, mu.M01 / mu.M00));
} 

希望对您有所帮助。

我用下面的代码解决了我的问题。

        double[] area = new double[contours.Size];

        for (int i = 0; i < contours.Size; i++)
        {
            area[i] = CvInvoke.ContourArea(contours[i]);
        }


        double[] mass_centerX = new double[contours.Size];
        double[] mass_centerY = new double[contours.Size];


        for (int i = 0; i < contours.Size; i++)
        {
            Moments mu = CvInvoke.Moments(contours[i], false);

            mass_centerX[i] = mu.M10 / mu.M00;
            mass_centerY[i] = mu.M01 / mu.M00;

            mass_centerX[i] = Math.Round(mass_centerX[i], 2);
            mass_centerY[i] = Math.Round(mass_centerY[i], 2);

        }