因为做程序图像剪切一直不太明白是怎么切片的,这里就用 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
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 坐标轴是垂直方向的。
− − − − − −