-
Notifications
You must be signed in to change notification settings - Fork 0
/
monocular_extraction.py
126 lines (92 loc) · 3.3 KB
/
monocular_extraction.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
import cv2
import matplotlib.pyplot as plt
import torch
import numpy as np
img_name = "newdata/natural/natural/test.jpg"
# model_type = "DPT_Large" # MiDaS v3 - Large (highest accuracy, slowest inference speed)
# #model_type = "DPT_Hybrid" # MiDaS v3 - Hybrid (medium accuracy, medium inference speed)
# #model_type = "MiDaS_small" # MiDaS v2.1 - Small (lowest accuracy, highest inference speed)
# midas = torch.hub.load("intel-isl/MiDaS", model_type)
# """Move model to GPU if available"""
# device = torch.device("cuda") if torch.cuda.is_available() else torch.device("cpu")
# midas.to(device)
# midas.eval()
# """Load transforms to resize and normalize the image for large or small model"""
# midas_transforms = torch.hub.load("intel-isl/MiDaS", "transforms")
# if model_type == "DPT_Large" or model_type == "DPT_Hybrid":
# transform = midas_transforms.dpt_transform
# else:
# transform = midas_transforms.small_transform
# """Load image and apply transforms"""
# img = cv2.imread(img_name)
# plt.imshow(img)
# plt.show()
# img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
# input_batch = transform(img).to(device)
# """Predict and resize to original resolution"""
# with torch.no_grad():
# prediction = midas(input_batch)
# prediction = torch.nn.functional.interpolate(
# prediction.unsqueeze(1),
# size=img.shape[:2],
# mode="bicubic",
# align_corners=False,
# ).squeeze()
# output = prediction.cpu().numpy()
# """Show result"""
# plt.imshow(output)
# plt.show()
img_bgr = cv2.imread(img_name, 1)
img_rgb = cv2.cvtColor(img_bgr, cv2.COLOR_BGR2RGB)
# img_rgb = img_bgr
print("Image shape: {}".format(img_rgb.shape))
plt.imshow(img_bgr)
plt.show()
#replace below with adaptive thresholding in HS space (not V apparently)
# Convert BGR to HSV
hsv = cv2.cvtColor(img_bgr, cv2.COLOR_BGR2HSV)
plt.imshow(hsv)
plt.show()
# define range of blue color in HSV
lower_neon = np.array([20,55,190])
upper_neon = np.array([88,255,255])
# Threshold the HSV image to get only blue colors
mask = cv2.inRange(hsv, lower_neon, upper_neon)
# Bitwise-AND mask and original image
res = cv2.bitwise_and(img_bgr,img_bgr, mask= mask)
plt.imshow(mask)
plt.show()
plt.imshow(res)
plt.show()
# Convert the image to grayscale
gray = cv2.cvtColor(res, cv2.COLOR_BGR2GRAY)
plt.imshow(gray, 'gray')
plt.show()
# Threshold the grayscale image to extract the black object
_, thresh = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY_INV)
plt.imshow(thresh, 'gray')
plt.show()
contours, _ = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
# Loop through the contours and find the contour with the largest area, which corresponds to the black object
max_area = 0
max_contour = None
for contour in contours:
area = cv2.contourArea(contour)
if area > max_area:
max_area = area
max_contour = contour
with_contours = cv2.drawContours(thresh,contours,-1,(0,255,0),3)
plt.imshow(with_contours)
plt.show()
depth_map = output
print(depth_map)
print(type(depth_map))
if max_contour is not None:
x, y, w, h = cv2.boundingRect(max_contour)
center_x = x + w//2
center_y = y + h//2
depth = depth_map[center_y, center_x]
print("Depth of black object: ", depth)
else:
print("Black object not found in the image.")
#conversion depth estimate - 13cm for test