赞
踩
一旦有了标注图像,标注视频帧似乎也同样简单。这是因为视频中的每一帧都被表示为图像。我们将在这里演示如何用几何形状和文本标注图像,示例代码如下:
Python
# Import dependencies import cv2 # Read Images img = cv2.imread('sample.jpg') # Display Image cv2.imshow('Original Image',img) cv2.waitKey(0) # Print error message if image is null if img is None: print('Could not read image') # 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++
// Import dependencies #include <opencv2/opencv.hpp> #include <iostream> // Using namespaces to nullify use of c::function(); syntax and std::function(); syntax using namespace std; using namespace cv; int main() { // Read Images Mat img = imread("sample.jpg"); // Display Image imshow("Original Image", img); waitKey(); // Print Error message if image is null if (img.empty()) { cout << "Could not read image" << endl; } // Draw line on image Mat imageLine = img.clone(); Point pointA(200,80); Point pointB(450,80); line(imageLine, pointA, pointB, Scalar(255, 255, 0), 3, 8, 0); imshow("Lined Image", imageLine); waitKey(); }
在上面示例中,使用OpenCV中的line()函数,用颜色线标注图像。在调用line()函数之前,使用以下命令创建原始图像的副本:
副本将确保对图像所做的任何更改都不会影响原始图像。在C++中,首先为原始图像的副本创建一个矩阵。
下面是line()函数的语法:
line(image, start_point, end_point, color, thickness)
从点 A ( x 1 , y 1 ) A(x_1,y_1) A(x1,y1)到点 B ( x 2 , y 2 ) B(x_2,y_2) B(x2,y2)画一条线,其中A和B表示图像中的任意两点。
如下代码所示:
Python
#Make copy of the 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))
cv2.imshow('Image Line', imageLine)
cv2.waitKey(0)
C++
// Make copy of the 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", line_image);
waitKey();
接下来,使用OpenCV中的circle()函数为图像添加一个圆。看看它的语法:
circle(image, center_coordinates, radius, color, thickness)
在本例中,对图像进行标注,并在美女的脸周围添加一个红色圆圈。然后使用imshow()函数显示带标注的图像。
Python
# Make a copy of image
imageCircle = img.copy()
# define the center of circle
circle_center = (415,450)
# define the radius of the circle
radius =100
# Draw a circle using the circle() Function
cv2.circle(imageCircle, circle_center, radius, (0, 0, 255), thickness=3, lineType=cv2.LINE_AA)
# Display the result
cv2.imshow("Image Circle",imageCircle)
cv2.waitKey(0)
C++
// Make a copy of image
Mat circle_image = img.clone();
// define the center of circle
Point circle_center(415,190);
// define the radius of circle
int radius = 100;
// Draw a circle using the circle() Function
circle(circle_image, circle_center, radius, Scalar(0, 0, 255), 3, 8, 0);
// Display the result
imshow("Circle on Image", circle_image);
waitKey();
刚刚完成了对带有红色圆圈的图像的标注。如果现在想用纯色填充这个圆圈怎么办?这很简单。只需将thickness参数更改为-1,如下代码所示。
Python
# make a copy of the original image
imageFilledCircle = img.copy()
# define center of the circle
circle_center = (415,450)
# define the radius of the circle
radius =100
# draw the filled circle on input image
cv2.circle(imageFilledCircle, circle_center, radius, (255, 0, 0), thickness=-1, lineType=cv2.LINE_AA)
# display the output image
cv2.imshow('Image with Filled Circle',imageFilledCircle)
cv2.waitKey(0)
C++
// make a copy of the original image
Mat Filled_circle_image = img.clone();
// define the center of circle
Point circle_center(415,190);
// define the radius of the circle
int radius = 100;
//Draw a Filled Circle using the circle() Function
circle(Filled_circle_image, circle_center, radius, Scalar(255, 0, 0), -1, 8, 0);
// display the output image
imshow("Circle on Image", circle_image);
waitKey();
现在,使用OpenCV中的rectangle()函数在图像上绘制一个矩形。查看其语法:
rectangle(image, start_point, end_point, color, thickness)
在rectangle()函数中,为矩形的角提供起点(左上)和终点(右下)。
现在通过这个示例代码,在美女的脸上用红色矩形标注图像。
Python
# make a copy of the original image
imageRectangle = img.copy()
# define the starting and end points of the rectangle
start_point =(350,400)
end_point =(500,550)
# draw the rectangle
cv2.rectangle(imageRectangle, start_point, end_point, (0, 0, 255), thickness= 3, lineType=cv2.LINE_8)
# display the output
cv2.imshow('imageRectangle', imageRectangle)
cv2.waitKey(0)
C++
// make a copy of the original image
Mat rect_image = image.clone();
// Define the starting and end points for the rectangle
Point start_point(300,115);
Point end_point(475,225);
// Draw a rectangle using the rectangle() function
rectangle(rect_image, start_point, end_point, Scalar(0,0,255), 3, 8, 0);
imshow("Rectangle on Image", rect_image);
waitKey();
使用OpenCV中的ellipse()函数在图像上绘制椭圆。ellipse()函数的语法与圆的语法非常相似。除了,您需要指定以下值而不是半径:
。
ellipse(image, centerCoordinates, axesLength, angle, startAngle, endAngle, color, thickness)
在下面的示例代码中,使用以下内容对图像进行标注:
OpenCV中的绘图功能非常相似,因此很容易掌握。此外,它们还提供可选参数,以便可以自由定义许多基本几何形状的位置和方向。
Python
# make a copy of the original image
imageEllipse = img.copy()
# define the center point of ellipse
ellipse_center = (415,190)
# define the major and minor axes of the ellipse
axis1 = (100,50)
axis2 = (125,50)
# draw the ellipse
#Horizontal
cv2.ellipse(imageEllipse, ellipse_center, axis1, 0, 0, 360, (255, 0, 0), thickness=3)
#Vertical
cv2.ellipse(imageEllipse, ellipse_center, axis2, 90, 0, 360, (0, 0, 255), thickness=3)
# display the output
cv2.imshow('ellipse Image',imageEllipse)
cv2.waitKey(0)
C++
// make a copy of the original image
Mat imageEllipse = img.clone();
// define the center point of ellipse
Point ellipse_center(415,190);
// define the major and minor axes of the ellipse
Point axis1(100, 50);
Point axis2(125, 50);
// Draw an ellipse using the ellipse() function
//Horizontal
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);
// display the output
imshow("Ellipses on Image", imageEllipse);
waitKey();
在上面示例中,将前面的代码修改为:
为此,进行以下更改:
Python
# make a copy of the original image
halfEllipse = img.copy()
# define the center of half ellipse
ellipse_center = (415,190)
# define the axis point
axis1 = (100,50)
# draw the Incomplete/Open ellipse, just a outline
cv2.ellipse(halfEllipse, ellipse_center, axis1, 0, 180, 360, (255, 0, 0), thickness=3)
# if you want to draw a Filled ellipse, use this line of code
cv2.ellipse(halfEllipse, ellipse_center, axis1, 0, 0, 180, (0, 0, 255), thickness=-2)
# display the output
cv2.imshow('halfEllipse',halfEllipse)
cv2.waitKey(0)
C++
//make a copy of the original image
Mat halfEllipse = image.clone();
// define the center of half ellipse
Point ellipse_center(415,190);
//define the axis point
Point axis1(100, 50);
// draw the Half Ellipse, just the outline
ellipse(halfEllipse, ellipse_center, axis1, 0, 180, 360, Scalar(255, 0, 0), 3, 8, 0);
// if you want to draw a Filled ellipse, use this line of code
ellipse(halfEllipse, ellipse_center, axis1, 0, 0, 180, Scalar(0, 0, 255), -2, 8, 0);
// display the output
imshow("Half-Ellipses on Image", halfEllipse);
waitKey();
最后,尝试用文本标注图像。为此,使用OpenCV中的putText()函数。看看它的语法,然后是参数:
putText(image, text, org, font, fontScale, color)
OpenCV支持Hershey字体集合中的几种字体样式,以及斜体字体。查看此列表:
Column |
---|
FONT_ HERSHEY_SIMPLEX=0 |
FONT_ HERSHEY_PLAIN=1, |
FONT_ HERSHEY_DUPLEX=2, |
FONT_ HERSHEY_ COMPLEX=3, |
FONT_ HERSHEY_TRIPLEX=4 |
FONT_HERSHEY_COMPLEX_SMALL=5, |
FONT_ HERSHEY_SCRIPT_SIMPLEX=6 |
FONT_ HERSHEY_SCRIPT_COMPLEX=7 |
FONT_ITALIC=16 |
Python
# make a copy of the original image
imageText = img.copy()
#let's write the text you want to put on the image
text = 'This is a black silk beauty!'
#org: Where you want to put the text
org = (50,350)
# write the text on the input image
cv2.putText(imageText, text, org, fontFace = cv2.FONT_HERSHEY_COMPLEX, fontScale = 1.5, color = (250,225,100))
# display the output image with text over it
cv2.imshow("Image Text",imageText)
cv2.waitKey(0)
cv2.destroyAllWindows()
C++
// make a copy of the original image
Mat imageText = img.clone();
// Write text using putText() function
putText(imageText, "This is a black silk beauty!", Point(50,350), FONT_HERSHEY_COMPLEX, 1.5, Scalar(250,225,100));
imshow("Text on Image", imageText);
waitKey(0);
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。