Image Stitching using OpenCV and Python (Creating Panorama Project)

9 / 23

BGR to RGB and Grayscale

By default, a color image with Red, Green, and Blue channels will be read in reverse order; ie, Blue, Green, and Red by OpenCv.

We could fix this issue by using cv2.COLOR_BGR2RGB transformation effect on the image.

So, we shall define a function fixColor to return the RGB form of the given image.

Also, let us get the gray-scle form of the two images. Generally, because of the reduced complexity of Grayscale form over the RGB format, grayscale images are preferred to process the images.

Note:

  • cv2.cvtColor() method is used to convert an image from one color space to another.

  • cv2.COLOR_BGR2RGB returns image in RGB format, which was initially in BGR format as read by cv2.imread().

  • cv2.COLOR_BGR2GRAY returns image in Grayscale format, which was initially in BGR format as read by cv2.imread().

INSTRUCTIONS
  • Define fixColor function which takes an image as the input argument ad returns the RGB format of the image. Pass cv2.COLOR_BGR2RGB as an input argument along with image to the cv2.cvtColor method.

    def fixColor(image):
        return(cv2.cvtColor(image, << your code comes here >>))
    
  • Now, convert the img_right and img_left images into grayscale format using cv2.COLOR_BGR2GRAY and store them in img1 and img2 respectively.

    img1 = cv2.cvtColor(img_right, << your code comes here >>)
    img2 = cv2.cvtColor( << your code comes here >> , cv2.COLOR_BGR2GRAY)
    
  • Let us now display all the four images - left image and its grayscale image, right image and its grayscale image - together.

    We shall use fixColor to display the image in RGB format since OpenCV has initially read it in BGR format.

    Make sure to write all the below code in the same code cell to view all the plots together.

    plt.figure(figsize=(30,20))
    
    plt.subplot(2,2,1)
    plt.title("Left Image")
    plt.imshow(fixColor(img_left))
    
    plt.subplot(2,2,2)
    plt.title("Grayscale of Left Image")
    plt.imshow(img2)
    
    plt.subplot(2,2,3)
    plt.title("Right Image")
    plt.imshow(fixColor(img_right))
    
    plt.subplot(2,2,4)
    plt.title("Grayscale of Right Image")
    plt.imshow(img1)
    
    plt.tight_layout()
    
Get Hint See Answer


Note - Having trouble with the assessment engine? Follow the steps listed here

Loading comments...