Transformation along with more image adjustments such as cropping, rotating, scaling. We also covered image Math such as adding, dividing, subtraction and multiplication. Finally we had in introduction to Kernels, yikes…
Code for the lab tasks:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
# For finding the size of an image import numpy as np import cv2 from matplotlib import pyplot as plt from matplotlib import image as image import easygui # I = cv2.imread("orange.png") J = cv2.imread("water.jpg") # Getting the size of the image: size = np.shape(J) # size = np.shape(J) print size # orange.png 360L, 630L, 3L # water.jpg 360L, 630L, 3L |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
1. Open image "Wartime.jpg"; 2. View its histogram alongside the image; 3. Use histogram equalisation to improve its contrast; 4. View the new histogram alongside the new image. import numpy as np import cv2 from matplotlib import pyplot as plt from matplotlib import image as image import easygui I = cv2.imread("wartime.jpg") G = cv2.cvtColor(I, cv2.COLOR_BGR2GRAY) plt.figure() plt.subplot(2,2,1) plt.imshow(I) plt.subplot (2,2,3) H =cv2.equalizeHist(G) plt.imshow(H) Values = G.ravel() plt.subplot(2,2,2) plt.hist(Values,bins =256,range=[0,256]); Values = H.ravel() plt.subplot(2,2,4) plt.hist(Values,bins =256,range=[0,256]); plt.show() |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
# 1. Open any image; # 2. Crop out the fourth quadrant of the image; # 3. Rotate this cropped section by 45 degrees 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") C = I[90:180,135:270] M = cv2.getRotationMatrix2D(center=(150,5), angle=45, scale=1) R = cv2.warpAffine(C, M = M, dsize=(270,180)) cv2.imshow("image", R) key = cv2.waitKey(0) |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
# 1. Open images Orange and Water; # 2. Scale each image to 50 % by multiplying each by 0.5; # 3. Add the two scaled images to get a composite image; # 4. Adjust the scaling to give a nicer output. import numpy as np import cv2 from matplotlib import pyplot as plt from matplotlib import image as image import easygui I = cv2.imread("orange.png") J = cv2.imread("water.jpg") # doesn't accept float so couldn't mulitple by 0.5 h, w, d = I.shape S = cv2.resize(I, dsize=(2*w, 2*h)) # doesn't accept float so couldn't mulitple by 0.5 h, w, d = J.shape T = cv2.resize(J, dsize=(2*w, 2*h)) M = cv2.add(S,T) cv2.imshow("M", M) cv2.imshow("S", S) cv2.imshow("T", T) key = cv2.waitKey(0) |