首页 > 解决方案 > 如何访问我创建的 Contour 的所有点?

问题描述

我为 webcamTexture 创建了 Mat 并在我确定的特定区域上绘制轮廓。我想访问轮廓上的所有坐标。因为我不仅要改变垫子的颜色,还要添加一些颜色。

我到达轮廓上的 x_min,x_max,x_min,x_max 点。当我将它们放在循环中并更改颜色时,我没有得到好的结果。

还有其他方法可以使用吗?

标签: c#imageopencvunity3d

解决方案


我创建了获取坐标函数来访问所有轮廓点。

ListMaxAndMins 表示最大和最小点 (x,y)

x_min,x_max,x_min,x_max 轮廓点。

垫图像意味着您的轮廓克隆。

您可以创建克隆轮廓。 cloneContour = Yourexistingcontour.clone()

您可以使用以下函数访问轮廓值的所有点。

    public List<Point> getCoordinates(List<int> _maxAndMins, Mat image)
    {
        int x_max = _maxAndMins[0];
        int x_min = _maxAndMins[1];
        int y_max = _maxAndMins[2];
        int y_min = _maxAndMins[3];

        List<Point> cordinates = new List<Point>();
        for (int i = x_min; i <= x_max; i++)
        {
            for (int j = y_min; j <= y_max; j++)
            {
                //Debug.Log("i:" + i + " j:" + j);
                if (image.get(j, i)[0] == 0 & image.get(j, i)[1] == 1)
                {
                    cordinates.Add(new Point(i, j));
                }
            }
        }
        return cordinates;
    }

此外,您可以使用以下函数获取作为访问点的颜色 (RGBA) 值。

public List<double[]> getColor(List<Point> cordinates, Mat image)
    {
        int r = 0, g = 0, b = 0, a = 0;

        List<double[]> colorlist = new List<double[]>();
        List<double[]> colorAvg = new List<double[]>();
        for (int i = 0; i < cordinates.Count; i++)
        {
            int x = (int)cordinates[i].x;
            int y = (int)cordinates[i].y;
            double[] color = image.get(x, y);

            colorlist.Add(color);
            r = (r + (int)colorlist[i][0]);
            g = (g + (int)colorlist[i][1]);
            b = (b + (int)colorlist[i][2]);
            a = (a + (int)colorlist[i][3]);              
            //Debug.Log("Color: "+ color.GetValue(0)+ "-"+ color.GetValue(1) + "-" + color.GetValue(2) + "-" + color.GetValue(3));
        }
        r = r / colorlist.Count;
        g = g / colorlist.Count;
        b = b / colorlist.Count;
        a = a / colorlist.Count;
        colorAvg.Add(new double[4] { r, g, b, a } );


        // Average R-G-B-A values

        Debug.Log("Avg R:" + colorAvg [0][0]);
        Debug.Log("Avg G:" + colorAvg [0][1]);
        Debug.Log("Avg B:" + colorAvg [0][2]);
        Debug.Log("Avg A:" + colorAvg [0][3]);

        return colorAvg ;
    }

推荐阅读