forked from Khushi38/Underwater_Line_Following
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFinal_Line_Follow
More file actions
63 lines (56 loc) · 2.48 KB
/
Final_Line_Follow
File metadata and controls
63 lines (56 loc) · 2.48 KB
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
import cv2
import numpy as np
def linefollow():
cap = cv2.VideoCapture(0)
while(cap.isOpened()):
# Capture frame-by-frame
ret, img = cap.read()
if ret == True:
width = int(img.shape[1] * 40/ 100)
height = int(img.shape[0] * 40 / 100)
dim = (width, height)
img = cv2.resize(img, dim, interpolation = cv2.INTER_AREA)
im = np.zeros((img.shape[0],img.shape[1]),dtype = int)
im = 0.6*img[:,:,0] + 0.2*img[:,:,1] + 0.2*img[:,:,2]
im = cv2.normalize(im.astype(int), None, 255,0, cv2.NORM_MINMAX, cv2.CV_8UC1)
#applying sharpnening
kernel_sharpening = np.array([[-1,-1,-1],
[-1, 9,-1],
[-1,-1,-1]])
sharpened = cv2.filter2D(im, -1, kernel_sharpening)
#applying blur
img3 = cv2.medianBlur(sharpened,5)
ret, threshed_img = cv2.threshold(img3,40, 255, cv2.THRESH_BINARY)
width = threshed_img.shape[0]
height = threshed_img.shape[1]
img5 = threshed_img[5:width-5,5:height-5]
img5 = cv2.dilate(img5,(9,9),iterations=2)
img5 = 255-img5
#cv2.imshow("contours",img5)
image, contours, hierarchy = cv2.findContours(img5,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
areas = [cv2.contourArea(c) for c in contours]
if len(areas) >= 0 :
max_index= np.argmax(areas)
contours1 = contours[max_index]
if len(contours1) > 0 :
x1,y1,w1,h1 = cv2.boundingRect(contours1)
#print(x1,y1,w1,h1)
if (x1 >= 0 and y1>=0 and w1 >=0 and h1>=0 ):
#frame =cv2.line(frame, (x1+int(w1/2), 200), (x1+int(w1/2), 250),(255,0,0),3)
frame =cv2.rectangle(img,(int(x1),int(y1)),(int(x1+w1), int(y1+h1)),(0,255,255),5)
# compute the center of the contour
M = cv2.moments(contours1)
cX = int(M["m10"] / M["m00"])
cY = int(M["m01"] / M["m00"])
cv2.drawContours(frame,contours1, -1, (0,255,0), 2)
frame = cv2.circle(frame, (cX,cY), 10, (255,255,255),3)
cv2.imshow("contours1",frame)
err=cX-(width/2)
print(err)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
else:
break
cv2.destroyAllWindows()
#return(cX)
linefollow()