This week we recapped on the theory we we learned last week and got some practical experience with OpenCV.
We got to explore various colour spaces along with some basic manipulation of images that we loaded into and exported from the system, as was reflected within the lab tasks.
Task 1
|
# 1. Read in any image # Also saves read image as "imagecopy" import numpy as np import cv2 from matplotlib import pyplot as plt from matplotlib import image as image import easygui I = cv2.imread("colors.jpg") cv2.imwrite("imagecopy.jpg",I) cv2.imshow("image", I) key = cv2.waitKey(0) |
Task 2
|
# 2. Convert the image to YUV import numpy as np import cv2 from matplotlib import pyplot as plt from matplotlib import image as image import easygui I = cv2.imread("colors.jpg") YUV = cv2.cvtColor(I, cv2.COLOR_BGR2YUV) cv2.imwrite("imageYUV.jpg",I) cv2.imshow("cvtColor", I) key = cv2.waitKey(0) |
Task 3
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
|
# 3. Extract the y Channel (Luminance) import numpy as np import cv2 from matplotlib import pyplot as plt from matplotlib import image as image import easygui I = cv2.imread("colors.jpg") YUV = cv2.cvtColor(I, cv2.COLOR_BGR2YUV) cv2.imshow("Y", YUV[:, :, 0]) # cv2.imshow("U", YUV[:, :, 1]) # cv2.imshow("V", YUV[:, :, 2]) key = cv2.waitKey(0) # Alternative method # y,u,v = cv2.split(YUV) # cv2.imshow("Y", y) # cv2.imshow("U", u) # cv2.imshow("V", v) # key = cv2.waitKey(0) |
Task 4
|
# 4. Convert the original image to grayscale (intensity) import numpy as np import cv2 from matplotlib import pyplot as plt from matplotlib import image as image import easygui I = cv2.imread("colors.jpg") J = cv2.cvtColor(I, cv2.COLOR_BGR2GRAY) cv2.imshow("image", J) key = cv2.waitKey(0) |
Task 5
|
# 5. Show the two on the screen using OpenCV's imshow function import numpy as np import cv2 from matplotlib import pyplot as plt from matplotlib import image as image import easygui I = cv2.imread("colors.jpg") J = cv2.cvtColor(I, cv2.COLOR_BGR2GRAY) cv2.imshow("image1", J) cv2.imshow("image2", I) key = cv2.waitKey(0) |