日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python利用opencv标注bounding box

發布時間:2023/12/4 python 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python利用opencv标注bounding box 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

http://blog.csdn.net/xieqiaokang/article/details/60780608

1. 函數

用 OpenCV 標注 bounding box 主要用到下面兩個工具——cv2.rectangle() 和 cv2.putText()。用法如下:

# cv2.rectangle() # 輸入參數分別為圖像、左上角坐標、右下角坐標、顏色數組、粗細 cv2.rectangle(img, (x,y), (x+w,y+h), (B,G,R), Thickness)# cv2.putText() # 輸入參數為圖像、文本、位置、字體、大小、顏色數組、粗細 cv2.putText(img, text, (x,y), Font, Size, (B,G,R), Thickness)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

2. Example

import cv2fname = '001.jpg' img = cv2.imread(fname) # 畫矩形框 cv2.rectangle(img, (212,317), (290,436), (0,255,0), 4) # 標注文本 font = cv2.FONT_HERSHEY_SUPLEX text = '001' cv2.putText(img, text, (212, 310), font, 2, (0,0,255), 1) cv2.imwrite('001_new.jpg', img)

documents:

https://docs.opencv.org/3.1.0/dc/da5/tutorial_py_drawing_functions.html

Goal

  • Learn to draw different geometric shapes with OpenCV
  • You will learn these functions : cv2.line(), cv2.circle() , cv2.rectangle(), cv2.ellipse(), cv2.putText() etc.

Code

In all the above functions, you will see some common arguments as given below:

  • img : The image where you want to draw the shapes
  • color : Color of the shape. for BGR, pass it as a tuple, eg: (255,0,0) for blue. For grayscale, just pass the scalar value.
  • thickness : Thickness of the line or circle etc. If **-1** is passed for closed figures like circles, it will fill the shape. default thickness = 1
  • lineType : Type of line, whether 8-connected, anti-aliased line etc. By default, it is 8-connected. cv2.LINE_AA gives anti-aliased line which looks great for curves.

Drawing Line

To draw a line, you need to pass starting and ending coordinates of line. We will create a black image and draw a blue line on it from top-left to bottom-right corners.

1?import numpy as np 2?import cv2 3? 4?# Create a black image 5?img = np.zeros((512,512,3), np.uint8) 6? 7?# Draw a diagonal blue line with thickness of 5 px 8?cv2.line(img,(0,0),(511,511),(255,0,0),5)

Drawing Rectangle

To draw a rectangle, you need top-left corner and bottom-right corner of rectangle. This time we will draw a green rectangle at the top-right corner of image.

1?cv2.rectangle(img,(384,0),(510,128),(0,255,0),3)

Drawing Circle

To draw a circle, you need its center coordinates and radius. We will draw a circle inside the rectangle drawn above.

1?cv2.circle(img,(447,63), 63, (0,0,255), -1)

Drawing Ellipse

To draw the ellipse, we need to pass several arguments. One argument is the center location (x,y). Next argument is axes lengths (major axis length, minor axis length). angle is the angle of rotation of ellipse in anti-clockwise direction. startAngle and endAngle denotes the starting and ending of ellipse arc measured in clockwise direction from major axis. i.e. giving values 0 and 360 gives the full ellipse. For more details, check the documentation of cv2.ellipse(). Below example draws a half ellipse at the center of the image.

1?cv2.ellipse(img,(256,256),(100,50),0,0,180,255,-1)

Drawing Polygon

To draw a polygon, first you need coordinates of vertices. Make those points into an array of shape ROWSx1x2 where ROWS are number of vertices and it should be of type int32. Here we draw a small polygon of with four vertices in yellow color.

1?pts = np.array([[10,5],[20,30],[70,20],[50,10]], np.int32) 2?pts = pts.reshape((-1,1,2)) 3?cv2.polylines(img,[pts],True,(0,255,255)) Note
If third argument is False, you will get a polylines joining all the points, not a closed shape.
cv2.polylines() can be used to draw multiple lines. Just create a list of all the lines you want to draw and pass it to the function. All lines will be drawn individually. It is a much better and faster way to draw a group of lines than calling cv2.line() for each line.

Adding Text to Images:

To put texts in images, you need specify following things.

  • Text data that you want to write
  • Position coordinates of where you want put it (i.e. bottom-left corner where data starts).
  • Font type (Check cv2.putText() docs for supported fonts)
  • Font Scale (specifies the size of font)
  • regular things like color, thickness, lineType etc. For better look, lineType = cv2.LINE_AA is recommended.

We will write OpenCV on our image in white color.

1?font = cv2.FONT_HERSHEY_SIMPLEX 2?cv2.putText(img,'OpenCV',(10,500), font, 4,(255,255,255),2,cv2.LINE_AA)

Result

So it is time to see the final result of our drawing. As you studied in previous articles, display the image to see it.

image

Additional Resources

  • The angles used in ellipse function is not our circular angles. For more details, visit this discussion.
  • Exercises

  • Try to create the logo of OpenCV using drawing functions available in OpenCV.

  • 總結

    以上是生活随笔為你收集整理的python利用opencv标注bounding box的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。