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 |
# 1. Open image Googly.jpg; # 2. Use thresholding to separate Googly and his friend from the background; # 3. Use statistical analysis to choose a threshold for better results; # 4. Try multiple thresholds on different channels. import numpy as np import cv2 from matplotlib import pyplot as plt from matplotlib import image as image import easygui I = cv2.imread("googly.jpg", cv2.IMREAD_GRAYSCALE) # I = cv2.imread("image.jpg") # RGB = cv2.cvtColor(I, cv2.COLOR_BGR2RGB) T = 100 T, B = cv2.threshold(I, thresh = T, maxval = 255, type = cv2.THRESH_BINARY) cv2.imshow("Googly", B) 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 |
# 1. Open image sudoku.jpg # 2. Use adaptive thresholding to create a black & white image; # 3. Modify the region size and constant for better results. import numpy as np import cv2 from matplotlib import pyplot as plt from matplotlib import image as image import easygui I = cv2.imread("sudoku.jpg", cv2.IMREAD_GRAYSCALE) J = cv2.adaptiveThreshold(I, maxValue = 255, adaptiveMethod = cv2.ADAPTIVE_THRESH_GAUSSIAN_C, thresholdType = cv2.THRESH_BINARY, blockSize = 5, C = 15) cv2.imshow("Sudoku", J) 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 29 30 31 32 33 34 35 36 37 38 39 40 |
# 1. Open image Orange.png; # 2. Using thresholding, create a mask with the orange as ROI; # 3. Use this mask to extract the orange from the image; # 4. Open Water.jpg; # 5. Use the inverse of the orange mask to cut an orange-shaped hole in # the water picture; # 6. Combine the orange and water masked images to create a # composite 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") Z = cv2.imread("water.jpg") G = cv2.cvtColor(I, cv2.COLOR_BGR2GRAY) J = cv2.bitwise_not(G) T = 20 T, B = cv2.threshold(J, thresh = T, maxval = 255, type = cv2.THRESH_BINARY) K = cv2.bitwise_not(B) ROI = cv2.bitwise_and(I,I,mask=B) ROI2 = cv2.bitwise_and(Z,Z,mask=K) ROI3 = cv2.bitwise_or(ROI,ROI2) cv2.imshow("Composite", ROI3) 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 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# 1. Open Trump.jpg; # 2. Use an appropriate colour space and range to segment out areas of # flesh (you will need to research this); # 3. Use morphological processes to clean up the ROI. import numpy as np import cv2 from matplotlib import pyplot as plt from matplotlib import image as image import easygui I = cv2.imread("trumpcopy.jpg") HSV = cv2.cvtColor(I, cv2.COLOR_BGR2HSV) # A Numpy array is created of upper and lower fleshtone values. lowerFleshValue = np.array([3, 48, 80]) upperFleshValue = np.array([20, 255, 255]) # Mask is created of all fleshtone values. fleshMask = cv2.inRange(HSV, lowerFleshValue, upperFleshValue) # Structuring element shape = cv2.getStructuringElement(cv2.MORPH_RECT,(6,6)) # Closing # newMask = cv2.dilate(fleshMask,shape) # newerMask = cv2.erode(newMask,shape) # Opening # newestMask = cv2.erode(newerMask,shape) # mostNewestMask = cv2.dilate(newestMask,shape) newMask = cv2.morphologyEx(fleshMask,cv2.MORPH_CLOSE,shape) newerMask = cv2.morphologyEx(newMask,cv2.MORPH_OPEN,shape) boundary = cv2.morphologyEx(newerMask,cv2.MORPH_GRADIENT,shape) C = cv2.bitwise_and(I,I,mask=newerMask) cv2.imshow("Original", I) cv2.imshow("HSV", HSV) cv2.imshow("Mask", fleshMask) cv2.imshow("Closing", newMask) cv2.imshow("Opening", newerMask) cv2.imshow("Boundary", boundary) cv2.imshow("Combined", C) key = cv2.waitKey(0) |