Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

there is no Mat (or Mat_ even) in cv2, instead it's numpy arrays all the way down.

>>> import cv2
>>> import numpy as np
>>> m =  cv2.imread("m2.bmp")
>>> np.shape(m)
(480, 640, 3)

you'd access them like img[y][x] = 3, or green = img[y][x][1] (for color), it's the same row,col convention as for cv::Mat

>>> m[2][2][1]
68

there's range operators too (if you need a submat / roi) : roi = img[y:Y, ,x:X]

>>> m[2:4, 3:6]
array([[[54, 63, 49],
        [52, 59, 47],
        [51, 58, 46]],

       [[41, 50, 36],
        [45, 52, 40],
        [47, 54, 42]]], dtype=uint8)

and you can even slice them, i.e to get only the last channel:

>>> m[:,:,2]
array([[ 0,  0,  0, ...,  0,  0,  0],
       [36, 39, 38, ...,  0,  0,  0],
       [59, 59, 54, ...,  0,  0,  0],
       ...,
       [ 0,  0,  0, ...,  0,  0,  0],
       [ 3,  3,  3, ...,  0,  0,  0],
       [ 3,  3,  3, ...,  0,  0,  0]], dtype=uint8)

there is no Mat (or Mat_ even) in cv2, instead it's numpy arrays all the way down.

>>> import cv2
>>> import numpy as np
>>> m =  cv2.imread("m2.bmp")
>>> np.shape(m)
(480, 640, 3)

you'd access them like img[y][x] = 3, or green = img[y][x][1] (for color), it's the same row,col convention as for cv::Mat

>>> m[2][2][1]
68

there's range operators too (if you need a submat / roi) : roi = img[y:Y, ,x:X]x:X]

>>> m[2:4, 3:6]
array([[[54, 63, 49],
        [52, 59, 47],
        [51, 58, 46]],

       [[41, 50, 36],
        [45, 52, 40],
        [47, 54, 42]]], dtype=uint8)

and you can even slice them, i.e to get only the last channel:

>>> m[:,:,2]
array([[ 0,  0,  0, ...,  0,  0,  0],
       [36, 39, 38, ...,  0,  0,  0],
       [59, 59, 54, ...,  0,  0,  0],
       ...,
       [ 0,  0,  0, ...,  0,  0,  0],
       [ 3,  3,  3, ...,  0,  0,  0],
       [ 3,  3,  3, ...,  0,  0,  0]], dtype=uint8)