使用 OpenCV 在图像上画线

OpenCV 提供了绘制几何形状的功能,例如直线、矩形、圆形等。

通过为每个点指定 x 和 y 坐标,该line函数可用于在两点之间绘制一条线。

import cv2
import numpy as np

img = np.zeros((100, 300, 3), dtype=np.uint8)

x1, y1 = 100, 20
x2, y2 = 200, 70
cv2.line(img, (x1, y1), (x2, y2), (0, 255, 0), 2)

cv2.imshow('Image', img)
cv2.waitKey(0)
cv2.destroyAllWindows()
#include <opencv2/opencv.hpp>

using namespace cv;

int main()
{
    Mat img = Mat::zeros(100, 300, CV_8UC3);

    int x1 = 100, y1 = 20;
    int x2 = 200, y2 = 70;
    line(img, Point(x1, y1), Point(x2, y2), Scalar(0, 255, 0), 2);

    imshow("Image", img);
    waitKey(0);
    destroyAllWindows();

    return 0;
}
package app;

import org.opencv.core.*;
import org.opencv.highgui.HighGui;
import org.opencv.imgproc.Imgproc;

public class Main
{
    static { System.loadLibrary(Core.NATIVE_LIBRARY_NAME); }

    public static void main(String[] args)
    {
        Mat img = Mat.zeros(100, 300, CvType.CV_8UC3);

        int x1 = 100, y1 = 20;
        int x2 = 200, y2 = 70;
        Imgproc.line(img, new Point(x1, y1), new Point(x2, y2), new Scalar(0, 255, 0), 2);

        HighGui.imshow("Image", img);
        HighGui.waitKey(0);
        HighGui.destroyAllWindows();

        System.exit(0);
    }
}

结果:

使用 OpenCV 在图像上画线

版权声明:本文内容转自互联网,本文观点仅代表作者本人。本站仅提供信息存储空间服务,所有权归原作者所有。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至1393616908@qq.com 举报,一经查实,本站将立刻删除。

(0)

相关推荐

发表回复

登录后才能评论