Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/304.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何转换以下C++;代码转换为C#_C#_C++ - Fatal编程技术网

如何转换以下C++;代码转换为C#

如何转换以下C++;代码转换为C#,c#,c++,C#,C++,我相信它完成了陈述1和2,但没有解决最后一个陈述 [编辑] 根据下面的评论,我添加了一段源代码 var sortedArr1 = arr1.OrderBy(r => r.X).ThenBy(r=>r.Y).ToList(); 公共列表检测器(Mat src) { var lines=src.HoughLines(1,Cv2.PI/180,50,0,0); 材料标签=新材料(); 垫中心=新垫(); 列表数据=新列表(); 对于(uint i=0;ithis.selector=选择器

我相信它完成了陈述1和2,但没有解决最后一个陈述

[编辑] 根据下面的评论,我添加了一段源代码

var sortedArr1 = arr1.OrderBy(r => r.X).ThenBy(r=>r.Y).ToList();
公共列表检测器(Mat src)
{
var lines=src.HoughLines(1,Cv2.PI/180,50,0,0);
材料标签=新材料();
垫中心=新垫();
列表数据=新列表();
对于(uint i=0;iMath.Abs(r.Y/r.X)).ToArray();
List ans=新列表();
对于(uint i=0;i<2;i++)
{
浮点数x0=X点[i].X;
浮点数y0=dxypoints[i].Y;
对于(uint j=2;j<4;j++)
{
浮点x1=X点[j].X;
浮点数y1=X点[j].Y;
浮点数x=(y0*(x1*x1+y1*y1)-y1*(x0*x0+y0*y0))/(y0*x1-x0*y1);
浮点y=(x0*(x1*x1+y1*y1)-x1*(x0*x0+y0*y0))/(y1*x0-x1*y0);
ans.Add(新的点2d(x,y));
}
}
//点的顺序(左上、左下、右上、右下)
var sortedAns=ans.OrderBy(r=>r.X).ThenBy(r=>r.Y.ToArray();

//TODO:convert sort(arr1.begin()+2,arr1.end(),[](Point2f-lhs,Point2f-rhs){return-lhs.y有一个很有用的工具可以转换这样的短代码片段

请注意,免费版将每个文件的输出限制为100行(不限制文件数量)

访问:

如果要复制可以使用的确切行为,或者需要从范围的开始和长度进行排序。您将需要实现,而不是使用委托,但创建一个使用委托比较对象属性的实现非常简单。这应该允许您复制确切的行为:

 public List<Point2d> DetectCorners(Mat src)
    {
        var lines = src.HoughLines(1, Cv2.PI / 180, 50, 0, 0);
        Mat labels = new Mat();
        Mat centers = new Mat();
        List<Point2f> data = new List<Point2f>();
        for (uint i = 0; i < lines.Length; i++)
        {
            float rho = lines[i].Rho;
            float theta = lines[i].Theta;
            float x = rho * (float)Math.Cos(theta);
            float y = rho * (float)Math.Sin(theta);
            data.Add(new Point2f(x, y));
        }

        Cv2.Kmeans(InputArray.Create(data), 4, labels,
            new TermCriteria(CriteriaType.Eps & CriteriaType.Count, 10, 1.0), 5, KMeansFlags.PpCenters, centers);
        List<Point2f> fourPoints = new List<Point2f>();
        List<Point2f> xyPoints = new List<Point2f>();
        for (int i = 0; i < 4; i++)
        {
            float x = centers.At<float>(i, 0);
            float y = centers.At<float>(i, 1);
            float rho = (float)Math.Sqrt(x * x + y * y);
            float theta = (float)Math.Atan2(y, x);

            xyPoints.Add(new Point2f(x, y));
            fourPoints.Add(new Point2f(rho, theta));
        }
        
        var sortedXyPoints = xyPoints.OrderBy(r => Math.Abs(r.Y / r.X)).ToArray();
        List<Point2d> ans = new List<Point2d>();
        for (uint i = 0; i < 2; i++)
        {
            float x0 = sortedXyPoints[i].X;
            float y0 = sortedXyPoints[i].Y;
            for (uint j = 2; j < 4; j++)
            {
                float x1 = sortedXyPoints[j].X;
                float y1 = sortedXyPoints[j].Y;
                float x = (y0 * (x1 * x1 + y1 * y1) - y1 * (x0 * x0 + y0 * y0)) / (y0 * x1 - x0 * y1);
                float y = (x0 * (x1 * x1 + y1 * y1) - x1 * (x0 * x0 + y0 * y0)) / (y1 * x0 - x1 * y0);
                ans.Add(new Point2d(x, y));
            }
        }
        // order of points (top-left, bottom-left, top-right, bottom-right)
        var sortedAns = ans.OrderBy(r => r.X).ThenBy(r=>r.Y).ToArray();
        //TODO: convert sort(arr1.begin()+2, arr1.end(), [](Point2f lhs, Point2f rhs) { return lhs.y<rhs.y; }); to c#
        return new List<Point2d>(sortedAns);
    }
公共类KeyComparer:IComparer其中TKey:IComparable
{
专用只读函数选择器;
公钥比较器(Func选择器)=>this.selector=选择器;
公共int比较(tx,ty)=>选择器(x).CompareTo(选择器(y));
}
...
var xComparer=新的键比较器(p=>p.X);
var yComparer=新的键比较器(p=>p.Y);
myList.Sort(xComparer);
myList.Sort(0,2,yComparer);
排序(2,myList.Length-2,yComparer);

但是,如果不理解为什么要这样做,我会非常犹豫是否只使用这样的解决方案。我会看看是否可以找到所实现算法的描述,看看是否与实现匹配,或者寻找其他实现,看看它们是否使用相同的排序。

什么是“左上、左下、右上、右下"C++代码首先用x坐标对整个数组进行排序,然后用y坐标对前2个元素进行排序,然后用y坐标来确定所有其他元素。你确信它是你想做的吗?在一般情况下,这看起来很奇怪。你测试过这个代码吗?如果没有,你能提供一个C吗?这个问题不需要 C++ >代码>标签。边注释:这个C++代码看起来可疑(算法)。这将做什么?arr1的性质是什么?我们对这个数组了解多少?它是否总是有4个元素,其中2个元素的一个值为X,2个元素的另一个值为X?感谢JonasH,这与我在发布此问题后使用的方法类似。我确实了解算法的作用以及原因,但我相信我现在正在研究一个更好的选择。非常感谢您的澄清!
 public List<Point2d> DetectCorners(Mat src)
    {
        var lines = src.HoughLines(1, Cv2.PI / 180, 50, 0, 0);
        Mat labels = new Mat();
        Mat centers = new Mat();
        List<Point2f> data = new List<Point2f>();
        for (uint i = 0; i < lines.Length; i++)
        {
            float rho = lines[i].Rho;
            float theta = lines[i].Theta;
            float x = rho * (float)Math.Cos(theta);
            float y = rho * (float)Math.Sin(theta);
            data.Add(new Point2f(x, y));
        }

        Cv2.Kmeans(InputArray.Create(data), 4, labels,
            new TermCriteria(CriteriaType.Eps & CriteriaType.Count, 10, 1.0), 5, KMeansFlags.PpCenters, centers);
        List<Point2f> fourPoints = new List<Point2f>();
        List<Point2f> xyPoints = new List<Point2f>();
        for (int i = 0; i < 4; i++)
        {
            float x = centers.At<float>(i, 0);
            float y = centers.At<float>(i, 1);
            float rho = (float)Math.Sqrt(x * x + y * y);
            float theta = (float)Math.Atan2(y, x);

            xyPoints.Add(new Point2f(x, y));
            fourPoints.Add(new Point2f(rho, theta));
        }
        
        var sortedXyPoints = xyPoints.OrderBy(r => Math.Abs(r.Y / r.X)).ToArray();
        List<Point2d> ans = new List<Point2d>();
        for (uint i = 0; i < 2; i++)
        {
            float x0 = sortedXyPoints[i].X;
            float y0 = sortedXyPoints[i].Y;
            for (uint j = 2; j < 4; j++)
            {
                float x1 = sortedXyPoints[j].X;
                float y1 = sortedXyPoints[j].Y;
                float x = (y0 * (x1 * x1 + y1 * y1) - y1 * (x0 * x0 + y0 * y0)) / (y0 * x1 - x0 * y1);
                float y = (x0 * (x1 * x1 + y1 * y1) - x1 * (x0 * x0 + y0 * y0)) / (y1 * x0 - x1 * y0);
                ans.Add(new Point2d(x, y));
            }
        }
        // order of points (top-left, bottom-left, top-right, bottom-right)
        var sortedAns = ans.OrderBy(r => r.X).ThenBy(r=>r.Y).ToArray();
        //TODO: convert sort(arr1.begin()+2, arr1.end(), [](Point2f lhs, Point2f rhs) { return lhs.y<rhs.y; }); to c#
        return new List<Point2d>(sortedAns);
    }
public class KeyComparer<T, TKey> : IComparer<T> where TKey : IComparable<TKey>
{
    private readonly Func<T, TKey> selector;
    public KeyComparer(Func<T, TKey> selector) => this.selector = selector;
    public int Compare(T x, T y) => selector(x).CompareTo(selector(y));
}
...
var xComparer = new KeyComparer<Point, float>(p => p.X);
var yComparer = new KeyComparer<Point, float>(p => p.Y);

myList.Sort(xComparer);
myList.Sort(0, 2, yComparer);
myList.Sort(2, myList.Length -2, yComparer);