Basic HTML Version

|  | Table of Contents |  | View Full Version

Page 167 - 2615_KEC
P. 167

    This means a colour  image  is  treated  as a three-dimensional  array. Understanding  image
                     dimensions is important because many image processing operations depend on the size and

                     structure of the image.
                     image.shape is used to find the dimensions of an image when working with Python libraries like

                     NumPy or OpenCV.

                     When an image is loaded in Python, it is stored as a NumPy array. The .shape attribute tells us
                     the size of that array. It returns the height (number of rows), width (number of columns), colour

                     channels (Red, Green, Blue).

                     Program 2: To display the dimensions and channels of an image.

                         Program2.py
                      File  Edit  Format   Run    Options   Window    Help

                      import cv2
                      image = cv2.imread("Flower.jpg")
                      print(image.shape)

                         Output

                      (541, 589, 3)

                     CONVERTING AN IMAGE TO GRAYSCALE

                     Converting an image to grayscale means  removing  its colour  and  changing  it into shades of
                     grey. The three colour values, Red, Green and Blue, are combined into a single brightness value

                     between 0 and 255. This makes the image simpler and easier to process. The colourful images
                     can be converted to grayscale images by using cvtColor method.

                     Syntax:

                     gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

                     Where,

                     image is the original colour image

                     gray_image stores the converted grayscale image.

                     cvtColor() function changes the image from BGR colour format to grayscale format.

                                                                                                            OpenCV    165

| 162 163 164 165 166 167 168 169 170 171 172 |