将OpenCV与np.floats一起使用

时间:2018-10-02 15:48:28

标签: python opencv

为什么使用cv2.line()会接受np.float32类型转换,但是当使用cv2.circle时会不断询问整数。

这是真的吗,还是我做错了什么?

代码:

elif attribute.tag == "LineTo":
            nextPoint = attribute.attrib
            img = cv2.line(img,(np.float32(startPoint["X"]), np.float32(startPoint["Y"])),(np.float32(nextPoint["X"]), np.float32(nextPoint["Y"])),(255,255,255),3)
            startPoint = nextPoint

        elif attribute.tag == "ArcTo":
            endPoint = attribute.attrib
            xDiff = np.float32(startPoint["X"]) - np.float32(endPoint["X2"])
            yDiff = np.float32(startPoint["Y"]) - np.float32(endPoint["Y2"])

            if xDiff > 0:
                xDiff = xDiff/2
                middlePoint = np.float32((np.float32(startPoint["X"]) + xDiff)), np.float32(startPoint["Y"])
                print(middlePoint)
                cv2.circle(img, middlePoint, int(abs(xDiff)), (255,255,255), 3)

            elif xDiff < 0:
                xDiff = xDiff/2
                middlePoint = np.float32((np.float32(startPoint["X"]) - xDiff)), np.float32(startPoint["Y"])
                print(middlePoint)
                cv2.circle(img, middlePoint, int(abs(xDiff)), (255,255,255), 3)

            elif yDiff > 0:
                yDiff = yDiff/2
                middlePoint = np.float32(startPoint["X"]), np.float32((np.float32(startPoint["Y"]) + yDiff))
                print(middlePoint)
                cv2.circle(img, middlePoint, int(abs(yDiff)), (255,255,255), 3)

            elif yDiff< 0:
                yDiff = yDiff/2
                middlePoint = np.float(startPoint["X"]), np.float((np.float32(startPoint["Y"]) - yDiff))
                print(middlePoint)
                cv2.circle(img, middlePoint, int(abs(yDiff)), (255,255,255), 3)

错误:

Traceback (most recent call last):
  File "beamer.1.py", line 144, in <module>
    startProjection()
  File "beamer.1.py", line 123, in startProjection
    cv2.circle(img, middlePoint, yDiff, (255,255,255), 3)
TypeError: integer argument expected, got float

2 个答案:

答案 0 :(得分:1)

由于这些方法接受不同的参数,因此请看以下:

方法行()

void cv::line (
    InputOutputArray img,
    Point pt1,
    Point pt2,
    const Scalar & color,
    int thickness = 1,
    int lineType = LINE_8,
    int shift = 0 
)

方法circle()

void cv::circle(
    InputOutputArray img,
    Point center,
    int radius,
    const Scalar &      color,
    int thickness = 1,
    int lineType = LINE_8,
    int shift = 0 
)

为方便起见,定义了以下类型别名:

typedef Point_<int> Point2i;
typedef Point2i Point;
typedef Point_<float> Point2f;
typedef Point_<double> Point2d;

答案 1 :(得分:1)

只需查看文档即可回答此问题。 line得两分。 circle需要一个点和一个半径。虽然可以使用浮点数来指定点,但opencv要求半径为int