当前位置:   article > 正文

[1] opecv: opecv中的坐标系_opencv坐标系

opencv坐标系

opencv中的坐标系

【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]
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/不正经/article/detail/119719
推荐阅读
相关标签
  

闽ICP备14008679号