opencvsharp icon indicating copy to clipboard operation
opencvsharp copied to clipboard

EstimateAffine2D can't find right answer

Open taiji1985 opened this issue 1 year ago • 0 comments

Summary of your issue

I make a test of EstimateAffine2D in OpencvSharp . but can't get the right answer , while python code work well.

read the example code please.

Environment

Microsoft Visual Studio Community 2022 nuget OpenCvSharp4.Windows 4.8.0.20230708

What did you do when you faced the problem?

try python code , but i get right answer.

Example code:

        public static string MatToString(Mat m) {
            //打印Mat
            StringBuilder sb = new StringBuilder();
            //打印大小
            sb.AppendLine($"Mat({m.Rows}x{m.Cols})=[");
            for (int i = 0; i < m.Rows; i++) {
                for (int j = 0; j < m.Cols; j++) {
                    float t = m.Get<float>(i, j);
                    //保留小数点后两位,整个字符串占8位
                    sb.Append(t.ToString("F2").PadLeft(8));
                    sb.Append(" ");
                }
                sb.AppendLine();
            }
            sb.AppendLine("]");
            return sb.ToString();
        }
        public static void testA() {
            Point2f[] srcPoints = new Point2f[9];
            string[] x = "0,0,0,1,0,2,1,0,1,1,1,2,2,0,2,1,2,2".Split(',');

            for (int i = 0; i < x.Length/2; i++) {
                srcPoints[i] = new Point2f(float.Parse(x[i*2]), float.Parse(x[i*2+1]));
                Console.WriteLine(srcPoints[i]);
            }

            Mat ret2=new Mat();
            InputArray input = InputArray.Create(srcPoints);
            var m = Cv2.EstimateAffine2D(input, input,  ret2);
            Console.WriteLine(MatToString(m));
            Console.WriteLine(MatToString(ret2));

        }

I make nine points of a grid , and use this array as first two parameters of EstimateAffine2D . It show return a eye array like this :

1 0 0 0 1 0

I write the python code ,i work well .

import cv2
import numpy as np


world_pts = np.zeros((9, 2), dtype=np.float32)
screen_pts = np.zeros((9, 2), dtype=np.float32)

screen_pts[0] = [0, 0]
screen_pts[1] = [0, 1]
screen_pts[2] = [0, 2]
screen_pts[3] = [1, 0]
screen_pts[4] = [1, 1]
screen_pts[5] = [1, 2]
screen_pts[6] = [2, 0]
screen_pts[7] = [2, 1]
screen_pts[8] = [2, 2]

#print(screen_pts)
# world_pts = screen_pts*150+200
world_pts = screen_pts

m,_ = cv2.estimateAffine2D(screen_pts,world_pts)

print(m)

Output:

C# output: 

(x:0 y:0)
(x:0 y:1)
(x:0 y:2)
(x:1 y:0)
(x:1 y:1)
(x:1 y:2)
(x:2 y:0)
(x:2 y:1)
(x:2 y:2)
Mat(2x3)=[
    0.00     0.00     0.00
    0.00     0.00     0.00
]

Mat(9x1)=[
    0.00
    0.00
    0.00
    0.00
    0.00
    0.00
    0.00
2361256000000000000000.00
    0.00
]

python output:


[[ 1. -0. -0.]
 [-0.  1. -0.]]

What did you intend to be?

Did i give the right param ,or How i give the params? it is a bug ?

taiji1985 avatar Aug 10 '23 03:08 taiji1985