forked from Garima-tl/AutonomousNavigation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Assignment_1_YatharthSharma.py
87 lines (61 loc) · 2.27 KB
/
Assignment_1_YatharthSharma.py
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# -*- coding: utf-8 -*-
"""robo_task1.ipynb
Automatically generated by Colab.
Original file is located at
https://colab.research.google.com/drive/1slKTcWp4D6mbCvZfyesiGG3anx9oCSNQ
"""
# Run on collab, as cv2_imshow is used instead of cv2.imshow
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import cv2
from google.colab.patches import cv2_imshow
img = cv2.imread("tennis_ball.jpeg")
cv2_imshow(img)
hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
print("Shape of HSV image:", hsv.shape)
lower_yellow = np.array([30, 100, 100], dtype=np.uint8)
upper_yellow = np.array([40, 255, 255], dtype=np.uint8)
print("Shape of lower_yellow:", lower_yellow.shape)
print("Shape of upper_yellow:", upper_yellow.shape)
mask = cv2.inRange(hsv, lower_yellow, upper_yellow)
# Bitwise-AND mask and original image
result = cv2.bitwise_and(img,img, mask= mask)
# display the mask and masked image
cv2_imshow(mask)
cv2.waitKey(0)
cv2_imshow(result)
cv2.waitKey(0)
cv2.destroyAllWindows()
# Step 6: Convert the masked image to grayscale
gray = cv2.cvtColor(result, cv2.COLOR_BGR2GRAY)
# Step 7: Apply Gaussian Blur to the grayscale image
blurred = cv2.GaussianBlur(gray, (9, 9), 2)
edges = cv2.Canny(blurred, 50, 150)
blurred
"""### max_circle should be defined to get better results"""
circles = cv2.HoughCircles(edges, cv2.HOUGH_GRADIENT, dp=1, minDist=20,
param1=50, param2=30, minRadius=0, maxRadius=0)
# if circles is not None:
# circles = np.uint16(np.around(circles))
# for i in circles[0, :]:
# # Draw the outer circle
# cv2.circle(img, (i[0], i[1]), i[2], (0, 255, 0), 2)
# # Draw the center of the circle
# cv2.circle(img, (i[0], i[1]), 2, (0, 0, 255), 3)
final_img = img.copy()
max_circles = 3
if circles is not None:
# Convert circles to integer coordinates for drawing
circles = np.uint16(np.around(circles))
# Limit the number of circles drawn (based on max_circles)
circles_to_draw = circles[0, :max_circles]
for i in circles_to_draw:
# Draw the outer circle
cv2.circle(final_img, (i[0], i[1]), i[2], (0, 255, 0), 2)
# Draw the center of the circle
cv2.circle(final_img, (i[0], i[1]), 2, (0, 0, 255), 3)
cv2_imshow(final_img)
cv2.waitKey(0)
cv2.destroyAllWindows()
img