天天看點

python opencv cv2.rectangle 參數含義圖檔pt1 和 pt2 參數color 參數thickness 參數lineType 參數shift 參數

因為做程式圖像剪切一直不太明白是怎麼切片的,這裡就用 cv2.rectangle 這個函數來看一下 opencv 是怎麼計量圖像的坐标軸的。

cv2.rectangle 這個函數的作用是在圖像上繪制一個簡單的矩形。

opencv 官網上給出的 cv2.rectangle 函數定義 如下:

Python: cv2.rectangle(img, pt1, pt2, color[, thickness[, lineType[, shift]]]) → None

  • img – Image.
  • pt1 – Vertex of the rectangle.
  • pt2 – Vertex of the rectangle opposite to pt1 .
  • color – Rectangle color or brightness (grayscale image).
  • thickness – Thickness of lines that make up the rectangle. Negative values, like CV_FILLED , mean that the function has to draw a filled rectangle.
  • lineType – Type of the line. See the line() description.
    • 8 (or omitted) - 8-connected line.
    • 4 - 4-connected line.
    • CV_AA - antialiased line.
  • shift – Number of fractional bits in the point coordinates.

這感覺說的不詳細,不知道是不是我找的有問題。

文章目錄

  • 圖檔
  • pt1 和 pt2 參數
  • color 參數
  • thickness 參數
  • lineType 參數
  • shift 參數

圖檔

我們比較關系的是 pt1 和 pt2 這兩個參數是什麼含義。下面我就用一個程式為大家說明,我們程式用的圖如下

圖來自 https://blog.csdn.net/lonelyrains/article/details/50388999

python opencv cv2.rectangle 參數含義圖檔pt1 和 pt2 參數color 參數thickness 參數lineType 參數shift 參數

pt1 和 pt2 參數

我們可以看到這個圖十分的規整,你把它下下來後就可以發現它是 1200×750 的。是以每一個人物的大小就是 240×375,我們就利用這個規整性來探究一下那兩個參數是什麼意思。

import cv2
img = cv2.imread(r"C:\Users\Administrator\Desktop\20151223182909577.png")
print(img.shape)  # 圖檔大小
cv2.rectangle(img, (240, 0), (480, 375), (0, 255, 0), 2)
cv2.imshow("fff", img)
           

輸出

(750, 1200, 3)

3 是指 3 通道,表示這個圖檔寬度是 1200 像素,高度是 750像素。

參考 Accessing Image Properties

python opencv cv2.rectangle 參數含義圖檔pt1 和 pt2 參數color 參數thickness 參數lineType 參數shift 參數

然後根據 stackoverflow 的圖示 https://stackoverflow.com/questions/23720875/how-to-draw-a-rectangle-around-a-region-of-interest-in-python

import cv2
cv2.rectangle(img, (x1, y1), (x2, y2), (255,0,0), 2)

x1,y1 ------
|          |
|          |
|          |
--------x2,y2
           

我們就可以很容易的得出結論 cv2.rectangle 的 pt1 和 pt2 參數分别代表矩形的左上角和右下角兩個點,而且 x 坐标軸是水準方向的,y 坐标軸是垂直方向的。

− − − − − − − − − − − − − − > x -------------->x −−−−−−−−−−−−−−>x

∣ | ∣

∣    x 1 , y 1 − − − − − − |\space \space x_1,y_1 ------ ∣  x1​,y1​−−−−−−

∣    ∣                                   ∣ |\space \space |\space \space \space \space \space \space \space \space \space \space \space \space \space \space \space \space \space \space \space \space \space \space \space \space \space \space \space \space \space \space \space \space \space | ∣  ∣                                 ∣

∣    ∣                                   ∣ |\space \space |\space \space \space \space \space \space \space \space \space \space \space \space \space \space \space \space \space \space \space \space \space \space \space \space \space \space \space \space \space \space \space \space \space | ∣  ∣                                 ∣

∣    ∣                                   ∣ |\space \space |\space \space \space \space \space \space \space \space \space \space \space \space \space \space \space \space \space \space \space \space \space \space \space \space \space \space \space \space \space \space \space \space \space | ∣  ∣                                 ∣

∣    ∣ − − − − − − − − x 2 , y 2 |\space \space | --------x_2,y_2 ∣  ∣−−−−−−−−x2​,y2​

∣ | ∣

∨ \vee ∨

y y y

color 參數

color 參數一般用 RGB 值指定,表示矩形邊框的顔色。RGB 對應的顔色可以使用 https://www.sioe.cn/yingyong/yanse-rgb-16/ 檢視。

import cv2
img = cv2.imread(r"C:\Users\Administrator\Desktop\20151223182909577.png")
print(img.shape)
cv2.rectangle(img, (240, 0), (480, 375), (0, 0, 255), 2)
cv2.imshow("fff", img)
           
python opencv cv2.rectangle 參數含義圖檔pt1 和 pt2 參數color 參數thickness 參數lineType 參數shift 參數

需要注意的是這裡的 (0, 0, 255) 三個分别對應 B G R。(不太懂為什麼)

thickness 參數

thickness 參數表示矩形邊框的厚度,如果為負值,如

CV_FILLED

,則表示填充整個矩形。

import cv2
img = cv2.imread(r"C:\Users\Administrator\Desktop\20151223182909577.png")
print(img.shape)
cv2.rectangle(img, (240, 0), (480, 375), (0, 255, 0), -1)
cv2.imshow("fff", img)
           
python opencv cv2.rectangle 參數含義圖檔pt1 和 pt2 參數color 參數thickness 參數lineType 參數shift 參數
import cv2
img = cv2.imread(r"C:\Users\Administrator\Desktop\20151223182909577.png")
print(img.shape)
cv2.rectangle(img, (240, 0), (480, 375), (0, 255, 0), 10)
cv2.imshow("fff", img)
           
python opencv cv2.rectangle 參數含義圖檔pt1 和 pt2 參數color 參數thickness 參數lineType 參數shift 參數

lineType 參數

line() function 中有這樣一段說明:

The function line draws the line segment between pt1 and pt2 points in the image. The line is clipped by the image boundaries. For non-antialiased lines with integer coordinates, the 8-connected or 4-connected Bresenham algorithm is used. Thick lines are drawn with rounding endings. Antialiased lines are drawn using Gaussian filtering. To specify the line color, you may use the macro CV_RGB(r, g, b)

這個參數看上去是指定 Bresenham 算法是 4 連通的還是 8 連通的,涉及到了計算機圖形學的知識。如果指定為

CV_AA

,則是使用高斯濾波器畫反鋸齒線。

shift 參數

shift 參數表示點坐标中的小數位數,但是我感覺這個參數是在将坐标右移 shift 位一樣。shift 為 1 就相當于坐标全部除以 2 1 2^1 21,shift 為 2 就相當于坐标全部除以 2 2 2^2 22

import cv2
img = cv2.imread(r"C:\Users\Administrator\Desktop\20151223182909577.png")
print(img.shape)
cv2.rectangle(img, (240*2*2, 0), (480*2*2, 375*2*2), (0, 255, 0), 2, shift=2)
cv2.imshow("fff", img)
           
python opencv cv2.rectangle 參數含義圖檔pt1 和 pt2 參數color 參數thickness 參數lineType 參數shift 參數