赞
踩
【1】opencv中的坐标系
opencv中,以图像image建立坐标系:
原点(0, 0):图像image左上角像素点;
水平方向为x轴(即图像的宽、列column),竖直方向为y轴(即图像高、行row)。
cv2.read(‘图像’).shape --> 返回:(height, width, channel),先高后宽,即(y, x, channel),先y后x;
但是在图像操作输入坐标点时,输入的是(x, y),先x后y,即(w, h),如:
img_resize = cv2.resize(img, (w, h))
cv2.rectangle(img0, (x0, y0), (x1, y1), (0, 0, 255), 2)
cv2.rectangle(img0, (0, 0), (50, 300), (0, 0, 255), 2)
opecv中像素点的坐标:
----------------------------------->x
∣
∣ (x1,y1)−−−−−−------
∣ ∣ |
∣ ∣ |
∣ ∣ |
∣ ∣−−−−−−−−------(x2,y2)
|
∨
y
【2】读取image后为numpy ndarray类型,np与opencv中的坐标点(x, y)是转置T关系
如,把opencv坐标系中的ROI(0,0) -> (50,300)区域像素改为黄色:
img[0:300, 0:50] = [0, 255, 255] # 先y后x
【3】opencv坐标系coordinate与画图draw测试代码
import os import cv2 def draw_test(img): ''' draw rectangle: in opencv coordinate rectangle area: ROI(0,0) -> (50,300) (x1, y1) (x2, y2) is in opencv coordinate rectangle ''' img0 = img.copy() cv2.rectangle(img0, (0, 0), (50, 300), (0, 0, 255), 2) # cv2.rectangle(img0, (0, 0), (50, 300), (0, 0, 255), -1) cv2.imshow("imgdraw", img0) cv2.waitKey(1000) def resize_test(img): ''' input: img output: height zoom out to 480, weight zoom out according ratio in opencv coordinate ''' h, w = img.shape[0:2] hzoom = 480 wzoom = int(w * hzoom / h) imgzoom = cv2.resize(img, (wzoom, hzoom)) cv2.imshow("img zoom out", imgzoom) cv2.waitKey(1000) def roi_test(img): ''' input: img ''' img0 = img.copy() h,w = img0.shape[0:2] #in opencv coordinate rectangle area: ROI(0,0) -> (50,300), change to yello img0[0:300, 0:50] = [0, 255, 255] # numpy`s coordinate (x,y) is Transposed relation shape with opencv # get pix value of (25, 25), (400, 240) print("(25, 25) pix value:", img0[25, 25]) print("(400, 240) pix value:", img0[400, 240]) cv2.imshow("imgROI", img0) cv2.waitKey(1000) def coordinate_test(img): ''' input: img ''' height, width, ch = img.shape[:] # (row,col,ch) (height,width,ch) print("img type:%s, img size:%s" % (type(img), img.size)) # img.size : all pixels count print("image shape:%s, shape type:%s" % (img.shape, type(img.shape))) print("height:%s, width:%d, ch:%d" % (height, width, ch)) cv2.imshow("imgcoordinate", img) cv2.waitKey(1000) if __name__ == "__main__": # print(os.getcwd()) img = cv2.imread("zta.png") coordinate_test(img) roi_test(img) resize_test(img) draw_test(img) #img type:<class 'numpy.ndarray'>, img size:966036 #image shape:(722, 446, 3), shape type:<class 'tuple'> #height:722, width:446, ch:3 #(25, 25) pix value: [ 0 255 255] #(400, 240) pix value: [ 45 35 204]
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。