Basic HTML Version

|  | Table of Contents |  | View Full Version

Page 168 - 2615_KEC
P. 168

    Program 3: To convert the colourful image to grayscale image.

                          Program3.py
                       File  Edit  Format    Run   Options   Window    Help

                       import cv2
                       image = cv2.imread("Flower.jpg")
                       gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
                       cv2.imshow("Gray Flower", gray)
                       print(image.shape)

                          Output

                       (541, 589, 3)

                          Output

                      CROPPING AN IMAGE

                      Cropping  an image  means selecting  and  keeping only  a  specific  portion  of the  image  while
                      removing the rest. It is done by choosing a particular range of rows and columns from the image
                      matrix. Cropping helps focus on an important area, remove unwanted parts or highlight a specific
                      object. In image processing, this is achieved by slicing the image array using pixel coordinates.
                      In OpenCV, cropping is very simple. An image is stored as a NumPy array, so cropping means

                      selecting a portion of that array using indexing.
                      Syntax:
                      cropped_image = image[y1:y2, x1:x2]

                      Where:
                         y1   starting row (top)
                         y2   ending row (bottom)
                         x1   starting column (left)

                         x2   ending column (right)

                      166   Premium Edition-VIII

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