|
| 1 | + |
| 2 | +import cv2 |
| 3 | +import matplotlib |
| 4 | +from matplotlib import colors |
| 5 | +from matplotlib import pyplot as plt |
| 6 | +import numpy as np |
| 7 | +from __future__ import division |
| 8 | + |
| 9 | + |
| 10 | +def show(image): |
| 11 | + # Figure size in inches |
| 12 | + plt.figure(figsize=(15, 15)) |
| 13 | + |
| 14 | + # Show image, with nearest neighbour interpolation |
| 15 | + plt.imshow(image, interpolation='nearest') |
| 16 | + |
| 17 | +def show_hsv(hsv): |
| 18 | + rgb = cv2.cvtColor(hsv, cv2.COLOR_HSV2BGR) |
| 19 | + show(rgb) |
| 20 | + |
| 21 | +def show_mask(mask): |
| 22 | + plt.figure(figsize=(10, 10)) |
| 23 | + plt.imshow(mask, cmap='gray') |
| 24 | + |
| 25 | +def overlay_mask(mask, image): |
| 26 | + rgb_mask = cv2.cvtColor(mask, cv2.COLOR_GRAY2RGB) |
| 27 | + img = cv2.addWeighted(rgb_mask, 0.5, image, 0.5, 0) |
| 28 | + show(img) |
| 29 | + |
| 30 | + |
| 31 | +def find_biggest_contour(image): |
| 32 | + image = image.copy() |
| 33 | + im2,contours, hierarchy = cv2.findContours(image, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE) |
| 34 | + |
| 35 | + contour_sizes = [(cv2.contourArea(contour), contour) for contour in contours] |
| 36 | + biggest_contour = max(contour_sizes, key=lambda x: x[0])[1] |
| 37 | + |
| 38 | + mask = np.zeros(image.shape, np.uint8) |
| 39 | + cv2.drawContours(mask, [biggest_contour], -1, 255, -1) |
| 40 | + return biggest_contour, mask |
| 41 | + |
| 42 | +def circle_countour(image, countour): |
| 43 | + image_with_ellipse = image.copy() |
| 44 | + ellipse = cv2.fitEllipse(countour) |
| 45 | + |
| 46 | + cv2.ellipse(image_with_ellipse, ellipse, (0,255,0), 2) |
| 47 | + return image_with_ellipse |
| 48 | + |
| 49 | + |
| 50 | +image = cv2.imread('./Downloads/ferrari.jpg') |
| 51 | + |
| 52 | + |
| 53 | + |
| 54 | +image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) |
| 55 | +max_dimension = max(image.shape) |
| 56 | +scale = 700/max_dimension |
| 57 | +image = cv2.resize(image, None, fx=scale,fy=scale) |
| 58 | + |
| 59 | + # Convert from RGB to HSV |
| 60 | +image_blur = cv2.GaussianBlur(image, (7, 7), 0) |
| 61 | +image_blur_hsv = cv2.cvtColor(image_blur, cv2.COLOR_RGB2HSV) |
| 62 | + |
| 63 | +# filter by color |
| 64 | +min_red = np.array([0, 100, 80]) |
| 65 | +max_red = np.array([10, 256, 256]) |
| 66 | +mask1 = cv2.inRange(image_blur_hsv, min_red, max_red) |
| 67 | + |
| 68 | +# filter by brigness |
| 69 | +min_red = np.array([170, 100, 80]) |
| 70 | +max_red = np.array([180, 256, 256]) |
| 71 | +mask2 = cv2.inRange(image_blur_hsv, min_red, max_red) |
| 72 | + |
| 73 | +mask = mask1 + mask2 |
| 74 | + |
| 75 | + |
| 76 | + |
| 77 | + |
| 78 | + |
| 79 | +kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (15, 15)) |
| 80 | +mask_closed = cv2.morphologyEx(mask, cv2.MORPH_CLOSE, kernel) |
| 81 | +mask_clean = cv2.morphologyEx(mask_closed, cv2.MORPH_OPEN, kernel) |
| 82 | + |
| 83 | + |
| 84 | + |
| 85 | + |
| 86 | +big_contour, red_mask = find_biggest_contour(mask_clean) |
| 87 | + |
| 88 | +overlay = overlay_mask(red_mask, image) |
| 89 | + |
| 90 | +circled = circle_countour(overlay, big_contour) |
| 91 | + |
| 92 | +show(circled) |
| 93 | + |
| 94 | +bgr = cv2.cvtColor(circled, cv2.COLOR_RGB2BGR) |
| 95 | + |
| 96 | + |
0 commit comments