This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #Determining most frequently occurring color pixel by pixel | |
| def most_common_used_color(img): | |
| # Get width and height of Image | |
| height, width, depth = img.shape | |
| # Initialize Variable | |
| r_total = 0 | |
| g_total = 0 | |
| b_total = 0 | |
| count = 0 | |
| # Iterate through each pixel |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #Get color from single pixel in image | |
| #Make list of pixel coordinates based on image shape | |
| y = range(0, height, 25) | |
| x = range(0, width, 25) | |
| #Combine lists above into a list of tuples | |
| merged_list = tuple(zip(x, y)) | |
| #Initialize the plot | |
| fig,axs = plt.subplots(1, len(y), figsize=(30,30)) | |
| i = 0 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #Select ROI from image | |
| imagedraw = cv2.selectROI('select',rgb_img) | |
| cv2.waitKey(0) | |
| cv2.destroyWindow('select') | |
| #cropping the area of the image within the bounding box using imCrop() function | |
| roi_image = rgb_img[int(imagedraw[1]):int(imagedraw[1]+imagedraw[3]), | |
| int(imagedraw[0]):int(imagedraw[0]+imagedraw[2])] | |
| fig,axs = plt.subplots(1,1, figsize=(5,5)) | |
| axs.imshow(roi_image) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #Remove green background/field from image prior to clustering | |
| green = np.array([60,255,255]) #This is green in HSV | |
| loGreen = np.array([30,25,25]) #low green threshold | |
| hiGreen = np.array([90,255,255]) #Upper green threshold | |
| loBlue = np.array([0,25,25]) #low red threshold | |
| hiBlue = np.array([30,255,255]) #Upper red threshold | |
| loRed = np.array([120,25,25]) #low blue threshold | |
| hiRed = np.array([180,255,255]) #Upper blue threshold |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #cv2.Canny(image, minVal, maxVal) | |
| img_gray = cv2.cvtColor(rgb_img, cv2.COLOR_RGB2GRAY) | |
| thresholds = [(5,150), (100,150), (200,225)] | |
| fig,axs = plt.subplots(1,4, figsize=(30,15)) | |
| i = 0 | |
| axs[i].imshow(rgb_img) | |
| for (minVal, maxVal) in thresholds: | |
| edge_img = cv2.Canny(img_gray, minVal, maxVal, apertureSize = 3, L2gradient = False) | |
| axs[i+1].imshow(edge_img) | |
| i += 1 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| params = [(3, 20, 5, 5), (9, 20, 40, 20), (15, 20, 160, 60)] | |
| fig,axs = plt.subplots(4, 3, figsize=(30,30)) | |
| i = 0 | |
| for (k, diameter, sigmaColor, sigmaSpace) in params: | |
| simpleblur_image = cv2.blur(rgb_img, (k,k)) | |
| gaussblur_image = cv2.GaussianBlur(rgb_img, (k,k), 0) | |
| medianblur_image = cv2.medianBlur(rgb_img, k) | |
| bilateralblur_image = cv2.bilateralFilter(rgb_img, diameter, sigmaColor, sigmaSpace) | |
| axs[0,i].imshow(simpleblur_image) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| gray_img = cv2.cvtColor(rgb_img, cv2.COLOR_RGB2GRAY) | |
| bgr_img = cv2.cvtColor(rgb_img, cv2.COLOR_RGB2BGR) | |
| hsv_img = cv2.cvtColor(rgb_img, cv2.COLOR_RGB2HSV) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #cv2.addWeighted(source_img1, alpha, source_img2, beta, gamma) | |
| alpha = [0.75, 0.5, 0.25] | |
| beta = [0, 1 , 10] | |
| gamma = [0, 10 ,100] | |
| #Vary alpha | |
| alpha_img1 = cv2.addWeighted(rgb_img, alpha[0], rgb_img, beta[0], gamma[0]) | |
| alpha_img2 = cv2.addWeighted(rgb_img, alpha[1], rgb_img, beta[0], gamma[0]) | |
| alpha_img3 = cv2.addWeighted(rgb_img, alpha[2], rgb_img, beta[0], gamma[0]) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #Resizing an image | |
| #cv2.resize(src, dsize[, dst[, fx[, fy[, interpolation]]]]) | |
| xscale = [0.75,0.5,0.25] | |
| yscale = [0.75,0.5,0.25] | |
| rimg1 = cv2.resize(rgb_img, (0,0), fx=xscale[0], fy=yscale[0]) | |
| rimg2 = cv2.resize(rgb_img, (0,0), fx=xscale[1], fy=yscale[1]) | |
| rimg3 = cv2.resize(rgb_img, (0,0), fx=xscale[2], fy=yscale[2]) | |
| fig,axs = plt.subplots(1,3, figsize=(30,15)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #Need to find the starting/ending column and row index first for the desired cropping | |
| cropIni = [0.15,0.3,0.45] | |
| #Crop width and height of image by 15% each | |
| startRow1 = int(height*cropIni[0]) ;startCol1 = int(width*cropIni[0]) | |
| endRow1 = int(height*(1-cropIni[0])) ;endCol1 = int(width*(1-cropIni[0])) | |
| #Crop width and height of image by 30% each | |
| startRow2= int(height*cropIni[1]) ;startCol2 = int(width*cropIni[1]) | |
| endRow2 = int(height*(1-cropIni[1])) ;endCol2 = int(width*(1-cropIni[1])) |