为图像和视频添加注释的目的不止一个,OpenCV使这个过程简单明了。
下来,一起看一如何使用它:
一旦学会了对图像进行注释,对视频帧的注释也是同样的道理。
006_annotating_images是OpenCV进行注释的示例程序。
确认OpenCV安装路径:
$ find /home/daniel/ -name "OpenCVConfig.cmake"
/home/daniel/OpenCV/installation/opencv-4.9.0/lib/cmake/opencv4/
/home/daniel/OpenCV/opencv/build/OpenCVConfig.cmake
/home/daniel/OpenCV/opencv/build/unix-install/OpenCVConfig.cmake
$ export OpenCV_DIR=/home/daniel/OpenCV/installation/opencv-4.9.0/lib/cmake/opencv4/
C++应用Demo工程结构:
006_annotating_images/CPP$ tree .
.
├── CMakeLists.txt
├── image_annotation.cpp
└── sample.jpg
0 directories, 3 files
C++应用Demo工程编译执行:
$ mkdir build
$ cd build
$ cmake ..
$ cmake --build . --config Release
$ cd ..
$ ./build/image_annotation
Python应用Demo工程结构:
006_annotating_images/Python$ tree .
.
├── image_annotation.py
├── requirements.txt
└── sample.jpg
0 directories, 3 files
Python应用Demo工程执行:
$ workoncv-4.9.0
$ python image_annotation.py
第一个参数:行 (高度, 自上而下递增)
第二个参数:列 (宽度, 自左往右递增)
C++:
// Draw line on image
Mat imageLine = img.clone();
// Draw the image from point A to B
Point pointA(200,80);
Point pointB(450,80);
line(imageLine, pointA, pointB, Scalar(255, 255, 0), 3, 8, 0);
imshow("Lined Image", imageLine);
waitKey();
Python:
# Draw line on image
imageLine = img.copy()
# Draw the image from point A to B
pointA = (200,80)
pointB = (450,80)
cv2.line(imageLine, pointA, pointB, (255, 255, 0), thickness=3, lineType=cv2.LINE_AA)
cv2.imshow('Image Line', imageLine)
cv2.waitKey(0)
C++:
// Draw Circle on image
Mat imageCircle = img.clone();
int radius = 100;
Point circle_center(415,190);
circle(imageCircle, circle_center, radius, Scalar(0, 0, 255), 3, 8, 0);
imshow("Circle on Image", imageCircle);
waitKey();
// Draw Filled Circle
Mat Filled_circle_image = img.clone();
circle(Filled_circle_image, circle_center, radius, Scalar(255, 0, 0), -1, 8, 0);
imshow("Circle on Image", Filled_circle_image);
waitKey();
Python:
# Draw Circle on image
imageCircle = img.copy()
circle_center = (415,190)
radius =100
cv2.circle(imageCircle, circle_center, radius, (0, 0, 255), thickness=3, lineType=cv2.LINE_AA)
cv2.imshow("Image Circle",imageCircle)
cv2.waitKey(0)
# Draw Filled Circle
Filled_circle_image = img.copy()
cv2.circle(Filled_circle_image, circle_center, radius, (255, 0, 0), thickness=-1, lineType=cv2.LINE_AA)
cv2.imshow('Image with Filled Circle',Filled_circle_image)
cv2.waitKey(0)
C++:
// Draw Rectangle
Mat imageRectangle = img.clone();
Point start_point(300,115);
Point end_point(475,225);
rectangle(imageRectangle, start_point, end_point, Scalar(0,0,255), 3, 8, 0);
imshow("Rectangle on Image", imageRectangle);
waitKey();
Python:
# Draw Rectangle
imageRectangle = img.copy()
start_point = (300,115)
end_point = (475,225)
cv2.rectangle(imageRectangle, start_point, end_point, (0, 0, 255), thickness= 3, lineType=cv2.LINE_8)
cv2.imshow('imageRectangle', imageRectangle)
cv2.waitKey(0)
C++:
// Draw Ellipse
Mat imageEllipse = img.clone();
// Horizontal
Point ellipse_center(415,190);
Point axis1(100, 50);
Point axis2(125, 50);
ellipse(imageEllipse, ellipse_center, axis1, 0, 0, 360, Scalar(255, 0, 0), 3, 8, 0);
// Vertical
ellipse(imageEllipse, ellipse_center, axis2, 90, 0, 360, Scalar(0, 0, 255), 3, 8, 0);
imshow("Ellipses on Image", imageEllipse);
waitKey();
Mat halfEllipse = img.clone();
// Half Ellipse
ellipse(halfEllipse, ellipse_center, axis1, 0, 180, 360, Scalar(255, 0, 0), 3, 8, 0);
// Filled ellipse
ellipse(halfEllipse, ellipse_center, axis1, 0, 0, 180, Scalar(0, 0, 255), -2, 8, 0);
imshow("halfEllipse", halfEllipse);
waitKey();
Python:
# Draw Ellipse
imageEllipse = img.copy()
ellipse_center = (415,190)
axis1 = (100,50)
axis2 = (125,50)
cv2.ellipse(imageEllipse, ellipse_center, axis1, 0, 0, 360, (255, 0, 0), thickness=3, lineType=cv2.LINE_AA)
cv2.ellipse(imageEllipse, ellipse_center, axis2, 90, 0, 360, (0, 0, 255), thickness=3, lineType=cv2.LINE_AA)
cv2.imshow('ellipse Image',imageEllipse)
cv2.waitKey(0)
halfEllipse = img.copy()
# Half Ellipse
cv2.ellipse(halfEllipse, ellipse_center, axis1, 0, 180, 360, (255, 0, 0), thickness=3, lineType=cv2.LINE_AA)
# Filled ellipse
cv2.ellipse(halfEllipse, ellipse_center, axis1, 0, 0, 180, (0, 0, 255), thickness=-2, lineType=cv2.LINE_AA)
cv2.imshow('HalfEllipse',halfEllipse)
cv2.waitKey(0)
C++:
// Put Text on Image
Mat imageText = img.clone();
// org: Where you want to put the text
Point org(50,350);
putText(imageText, "I am a Happy dog!", org, FONT_HERSHEY_COMPLEX, 1.5, Scalar(0,255,0), 3, 8, false);
imshow("Text on Image", imageText);
waitKey(0);
Python:
# Put Text on Image
imageText = img.copy()
text = 'I am a Happy dog!'
# org: Where you want to put the text
org = (50,350)
cv2.putText(imageText, text, org, fontFace = cv2.FONT_HERSHEY_COMPLEX, fontScale = 1.5,
color = (250,225,100),thickness = 3, lineType=cv2.LINE_AA)
cv2.imshow("Image Text",imageText)
cv2.waitKey(0)
通过以下5个API对图像进行操作,分别在图像上划线、画圆、矩形、椭圆,以及标注文字的方式进行注释。
【1】ubuntu22.04@laptop OpenCV Get Started
【2】ubuntu22.04@laptop OpenCV安装
【3】ubuntu22.04@laptop OpenCV定制化安装