Question

Task 1 OpenCV: Basic I/O of Digital Images (1 point) In cv2, you can use cv2.imread() and cv2.imwrite() to perform basic I/O of digital images. These two functions support several common

image formats (including pag and.jpg). The function return of cv2.imread() is a 3-D numpy.ndamay (dim, dim, 3) for color images. If you want to read a gray image, 0 should be provided as the second argument to this function, the function return is then a 2-D numpy.ndarray. You may use this mechanism to convert an input color image to a gray image as a 2-D ndarray. cv2.imshow() function does not work with Google Colab. Therefore, you can use plt imshow() function. On the other hand, cv2.imshow() works properly with Jupyter notebook. Special attention should be given to cv2 imerad() and cv2 imwrite(), because both functions convert a RGB order to a BGR order, and vice versa. Write down the following statements: import cv2 import matplotlib.pyplot as plt # In cv2, a RGB image is read as a BGR array ing_bgr = cv2.imread ("Island.png") img_rgb cv2.cvtColor (ing bgr, ev2.COLOR_BGR2RGB) plt.subplot (1, 2, 1) plt.inshow(img_bgr) plt.title('img_bgr') plt.subplot (1,2, 2) plt.imshow(img_rgb) plt.title('img_rgb") # In cv2, a BGR array is written as a RGB image cv2.imwrite('img_bgr.png', ing_bgr) cv2.imwrite('ing_rgb.png", img_rgb) The running result should look like the following figure (but the content is different): img_bgr img_rgb 0 100 200 300 400 500 400 100 200 Demo 300 500 200 200 Check the content of the output files: img_bgr.png and img_rgb.png. Does it surprise you?/nTask 2 OpenCV: Gray Image (1 point) You can use cv2.cviColor() function to convert a color image to a gray image. Two parameters should be provided for this function: the first one is a numpy.ndarray and the second parameter could be cv2.COLOR_BGRZGRAY OF cv2.COLOR_RGBZGRAY, depending on whether the ndarray is in the format BGR or RGB. The result of cv2.cviColor is a two-dimension ndarray. Since it is a 2-D anay, the issue of color ordering (BGR or RGB) disappears. With cv2.imwrite(), 8-bit single-channel or 3-channel (with BGR channel order) images can be saved. Write the following code and run it: import numpy as np img_bgr_gray cv2.cvtColor (img_bgr, cv2.COLOR_BGRZGRAY) img_rgb_gray cv2.cvtColor (img_rgb, cv2.COLOR_RGBZGRAY) print (img_bgr.shape) print (img_rgb_gray.shape) plt.subplot (1, 2, 1) plt.inshow(img_bgr_gray, cmap="gray") plt.title("img_bgr_gray¹) plt.subplot (1,2, 2) plt.inshow(img_rgb_gray, cmap="gray") plt.title("img_rgb_gray¹) cv2.imwrite('ing_bgr_gray.png', ing_bgr_gray) cv2.imwrite('ing_rgb_gray.png', img_rgb_gray) t = cv2.imread('img_bgr_gray.png') print (t.shape) s cv2.imread('img_bgr_gray.png",0) print (s.shape) cv2.inshow('gray', ing_bgr_gray) cv2.waitkey (0) The running result should look like the following figure (but the content is different): img bgr gray D 100 200 300 400 500 200 400 100 200 Demo 300 400 500 img_rgb_gray 200 400 Double-click the result files (img_bgr_gray.png and img_rgb_gray.png). Are these two image files in gray scale?/nTask 3 OpenCV: Rotation and Scaling of a Digital Image (1 point) cv2.rotate() method is used to rotate a 2D array in multiples of 90 degrees. Cv2.resize() allows you to change the size of an image to another size in the format of tuple (sizex, sizey). Write the following code: #rotation and scaling img3a = cv2.rotate(img_rgb, cv2.cv2 ROTATE_90_CLOCKWISE) img3b = cv2.rotate(img_rgb, cv2.ROTATE_180) img3c=cv2.rotate(img_rgb, cv2 ROTATE_90_COUNTERCLOCKWISE) img3d=cv2.resize(img_rgb, (600, 480)) plt figure(figsize=(12, 12)) # size in inch plr subplot(1,4, 1) plr.imshow(img3a) pirtile(ROTATE_90_CLOCKWISE) plr subplot(1,4, 2) plr.imshow(img3b) ple(ROTATE_180) plr subplot(1,4,3) plt.imshow(img3c) pirrite("ROTATE_90_CLOCKWISE') pir.subplot(1,4, 4) plr.imshow(img3d) pirrite("Resize to 600x480) The running result should look like the following figure: ROTATE 90 CLOCKWISE 100 200 300 400 500 0 200 400 100 200 300 500 0 3 ROTATE 180 200 400 100 200 De 300 400 500 ROTATE_90_CLOCKWISE 0 200 400 100 200 300 400 Resize to 600x480 200 400/nTask 4 OpenCV: Crop an Image (1 point) Basically, we can use subindex of arrays to perform an image cropping. Write the following code: #crop an image sizex, sizey, img_rgb.shape img4a ing_rgb[int (sizex 0.25) :int (sizex 0.75), int (sizey 0.25) :int ( 0.75) 1 plt.inshow (img4a) The running result should look like the following figure: 100 150 200 250 8 Demo 300 8 150 200 250/nTask 5 Imutils: Rotate an Image at Any Angle (1 point) Imutils package may be used to rotate at any angle. Write the following code: * Rotation at any angle import imutils img5a=imutils.rotate(img_rgb, 32) # 32 degree plr.imshow(img5a) The running result should look like the following figure: 0 100 200 300 400 500 Demo 100 200 300 400 500/nTask 6 OpenCV: Color Segmentation (1 point) HSV color system can be used to carry out color segmentation. Here, HSV refers to hue, saturation, and value. In Open CV, the value ranges of these three components are 0-180, 0-255, and 0-255, respectively. Below is a typical Hue-Saturation graph of the HSV color map, where the horizontal axis represents hue and the vertical axis is saturation. 2017/11/28 21:08:04 CST so (1)-80-180, 0-25, 26) 100 150 200 250 1929 30 40 50 (2)-50-180, S: 255, 255) " 100 110 129 130 140 150 169 179 Cv2.inRange() can be used to construct a mask and cv2 bitwise_and() functions facilitates a color segmentation via the mask. Design an OpenCV code with the image file, flower.jpg, and show the running results, including the mast and segmentation result. The color segmentation will generate a new image in which only the red-color flower remains with the background being filtered out. Task 7 OpenCV: Segmentation of a Red Traffic Sign (1 point) OpenCV is used to handle red color traffic signs. Try it by yourself and show the result of color segmentation. Task 8 OpenCV: Segmentation of a Yellow Traffic Sign (1 point) Use a similar approach as in Task 7 to process a yellow-color traffic sign. The input image file is "Yellow_traffic_sign.png." Write your code and show the running result of the segmentation of this yellow traffic sign. Task 9 Pillow: Image Rotation (1 point) Use PIL to read in the image ("Island.png") and rotate it by 30 degree. Task 10 Pillow: Image Flip (1 point) Use PIL to read in the image ("Island.png") and flip it horizontally. Task 11 Pillow: Box Blur of Image (1 point) Use PIL to read in the image ("Island.png") and blur it with a parameter = 5. Task 12 Pillow: Contour of Image (1 point) Use PIL to read in the image ("Lenna.png") and utilize CONTOUR filter to produce a new image.

Question image 1Question image 2Question image 3Question image 4Question image 5Question image 6