Page 34 - PowerPoint Presentation
P. 34

Accessing and Modifying pixel values












                • Let's load a color image first:



                • >>> import numpy as np                                                                     >>> px = img[100,100]

                                                                                                             >>> print( px )
                • >>> import cv2 as cv                                                                       [157 166 200]


                • >>> img = cv.imread('messi5.jpg')                                                          # accessing only blue pixel

                                                                                                             >>> blue = img[100,100,0]
                • >>> assert img is not None, "file could                                                    >>> print( blue )

                   not be read, check with os.path.exists()"                                                 157

                                                                                                             You can modify the pixel values the same way.
                • You can access a pixel value by its row                                                    >>> img[100,100] = [255,255,255]


                   and column coordinates. For BGR image,  >>> print( img[100,100] )

                   it returns an array of Blue, Green, Red                                                   [255 255 255]

                   values. For grayscale image, just

                   corresponding intensity is returned.
   29   30   31   32   33   34   35   36   37   38   39