Page 259 - Ai Book - 10
P. 259

Suppose, we will get an image in Jupyter notebook with the help of the following code:

                import cv2                                                                Output
                from matplotlib import pyplot as plt

                import numpy as np
                image = cv2.imread(‘F:\Softwares\Setups\
                DataScience\Deepa\img1.jpg’)
                plt.imshow(cv2.cvtColor(image,cv2.
                COLOR_BGR2RGB))
                plt.title(‘Summer Camp 2021’)
                plt.axis(‘on’)
                plt.show()

        Now, we want to copy a specific part of an image i.e. Dance Callout box. To do this, we can write the following
        code:

                import cv2                                                                  Output
                from matplotlib import pyplot as plt
                import numpy as np
                image = cv2.imread(‘F:\Softwares\Setups\
                DataScience\Deepa\img1.jpg’)
                extract_part = image[150:300,50:260]
                plt.imshow(cv2.cvtColor(extract_part,
                cv2.COLOR_BGR2RGB))
                plt.axis(‘on’)
                plt.show()
        Now , we want to insert the extracted part in the image at a particular location. To do this, we can write the
        following code:


                import cv2                                                                  Output
                from matplotlib import pyplot as plt
                import numpy as np
                image = cv2.imread(‘F:\Softwares\Setups\
                DataScience\Deepa\img1.jpg’)
                extract_part = image[150:300,50:260]
                image[50:200,0:210] = extract_part
                plt.imshow(cv2.cvtColor(image, cv2.COLOR_
                BGR2RGB))
                plt.axis(‘on’)
                plt.show()

        Resizing an Image

        Resizing an image is an important part of image processing technique because on resizing an image, its pixel
        information is changed. For example, when an image is reduced in size, any unneeded pixel information will be
        discarded by the photo editor like Photoshop. When an image is enlarged, the photo editor that you use must
        create and add new pixel information on the basis of its best guesses to achieve a larger size which typically
        results in either a very pixelated or very soft and blurry looking image. In case of AI systems, Resizing images is
        important especially when we want to train a model having the same size and aspect ratio.

                                                                                                             133
                                                                                                             133
   254   255   256   257   258   259   260   261   262   263   264