opencv安裝教程python,python opencv cv2.rectangle 參數含義

 2023-11-19 阅读 41 评论 0

摘要:因為做程序圖像剪切一直不太明白是怎么切片的,這里就用 cv2.rectangle 這個函數來看一下 opencv 是怎么計量圖像的坐標軸的。 opencv安裝教程python?cv2.rectangle 這個函數的作用是在圖像上繪制一個簡單的矩形。 opencv 官網上給出的 cv2.rectangle 函數定義 如下&#x

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

opencv安裝教程python?cv2.rectangle 這個函數的作用是在圖像上繪制一個簡單的矩形。

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

open函數python?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
在這里插入圖片描述

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
在這里插入圖片描述
然后根據 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
∣|
∣x1,y1??????|\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 |???????????????????????????????????
∣∣????????x2,y2|\space \space | --------x_2,y_2??????????x2?,y2?
∣|
∨\vee
yyy

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)

在這里插入圖片描述
需要注意的是這里的 (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)

在這里插入圖片描述

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)

在這里插入圖片描述

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 就相當于坐標全部除以 212^121,shift 為 2 就相當于坐標全部除以 222^222

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)

在這里插入圖片描述

版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。

原文链接:https://808629.com/184613.html

发表评论:

本站为非赢利网站,部分文章来源或改编自互联网及其他公众平台,主要目的在于分享信息,版权归原作者所有,内容仅供读者参考,如有侵权请联系我们删除!

Copyright © 2022 86后生记录生活 Inc. 保留所有权利。

底部版权信息