Get RGB value opencv python

前端 未结 5 891
暖寄归人
暖寄归人 2020-12-16 17:28

I am loading an image into python e.g.

image = cv2.imread(\"new_image.jpg\")

How can i acccess the RGB values of image?

5条回答
  •  余生分开走
    2020-12-16 17:52

    You can do

    image[y, x, c]
    

    or equivalently image[y][x][c].

    and it will return the value of the pixel in the x,y,c coordinates. Notice that indexing begins at 0. So, if you want to access the third BGR (note: not RGB) component, you must do image[y, x, 2] where y and x are the line and column desired.

    Also, you can get the methods available in Python for a given object by typing dir(). For example, after loading image, run dir(image) and you will get some usefull commands:

    'cumprod', 'cumsum', 'data', 'diagonal', 'dot', 'dtype', 'dump', 'dumps', 'fill',
    'flags', 'flat', 'flatten', 'getfield', 'imag', 'item', 'itemset', 'itemsize', 
    'max', 'mean', 'min', ...
    

    Usage: image.mean()

提交回复
热议问题