'AttributeError:'numpy.float32'对象没有属性'ctypes'

时间:2020-08-12 18:35:12

标签: c# numpy opencv

我试图根据从左上,右上等顺序排列的点来计算图像的尺寸。因此,我以后可以执行4点透视变换和变形。 (使用opencvSharp和numpy.net)

我想从中获取最大宽度和最大高度作为整数 NDarray,但此时它抛出 “ Python.Runtime.PythonException:'AttributeError:'numpy.float32'对象没有属性'ctypes'

不太确定如何作为一个新手来解决这个问题,这是我第一次使用此OpencvSharp和numpy.NET。

NDarray rect = OrderPoints(pts);
            // 4 Points order from top-left top- right etc..
            NDarray tL = rect[0],
                tR = rect[1],
                bR = rect[2],
                bL = rect[3];
      

                           // width of new image
            NDarray w1 = ((bR[0] - bL[0]) * (bR[0] - bL[0])) + ((bR[1] - bL[1]) * (bR[1] - bL[1])); // quietly throws exception here 
            NDarray w2 = ((tR[0] - tL[0]) * (tR[0] - tL[0])) + ((tR[1] - tL[1]) * (tR[1] - tL[1]));

             w1 = np.sqrt(w1);
             w2 = np.sqrt(w2);

            // get max width as an interger

             int[] width1 = w1.GetData<int>(); // same error blows up here
            int[] width2 = w2.GetData<int>();

            int maxWidth = Math.Max(width1[0], width2[0]);
          


            NDarray h1 = ((tR[0] - bR[0]) * (tR[0] - bR[0])) + ((tR[1] - bR[1]) * (tR[1] - bR[1]));
            NDarray h2 = (tL[0] - bL[0]) * (tL[0] - bL[0]) + (tL[1] - bL[1]) * (tL[1] - bL[1]);

           h1 = np.sqrt(h1);
            h2 = np.sqrt(h2);

            int[] height = h1.GetData<int>();
            int[] height2 = h2.GetData<int>();


            int maxHeight = Math.Max(height[0], height2[0]);

            // make a top - down view
            var dst = np.array(
                  new[,] {
                     { 0,0},
                     {maxWidth - 1, 0 },
                     {maxWidth - 1, maxHeight - 1 },
                     {0, maxHeight - 1 }
                      });
            
              var rectPts = rect.GetData<Point2f>();
               var dstarray = dst.GetData<Point2f>();


             Mat matr = Cv2.GetPerspectiveTransform(rectPts, dstarray);
            Mat output = new Mat();
            Cv2.WarpPerspective(image, output, matr, new Size(maxWidth, maxHeight)); 

            return output;

1 个答案:

答案 0 :(得分:0)

我设法提出了一个解决方案,方法是使用repr属性返回数据字符串,然后将其解析为float并转换为Int。

int width1 = (int)float.Parse(w1.repr);
int width2 = (int)float.Parse(w2.repr);