因為做程序圖像剪切一直不太明白是怎么切片的,這里就用 cv2.rectangle 這個函數來看一下 opencv 是怎么計量圖像的坐標軸的。
opencv安裝教程python?cv2.rectangle 這個函數的作用是在圖像上繪制一個簡單的矩形。
opencv 官網上給出的 cv2.rectangle 函數定義 如下:
open函數python?Python: cv2.rectangle(img, pt1, pt2, color[, thickness[, lineType[, shift]]]) → None
這感覺說的不詳細,不知道是不是我找的有問題。
我們比較關系的是 pt1 和 pt2 這兩個參數是什么含義。下面我就用一個程序為大家說明,我們程序用的圖如下
圖來自 https://blog.csdn.net/lonelyrains/article/details/50388999
我們可以看到這個圖十分的規整,你把它下下來后就可以發現它是 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 參數一般用 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 參數表示矩形邊框的厚度,如果為負值,如 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)
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 為 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)
版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。
工作时间:8:00-18:00
客服电话
电子邮件
admin@qq.com
扫码二维码
获取最新动态