-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget_obj_by_color.py
99 lines (69 loc) · 2.44 KB
/
get_obj_by_color.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
import cv2
from realsense_wapper import realsense
def get_obj_bbox(img):
crop_offset_col = 300
crop_offset_row = 100
crop = img[crop_offset_row:, crop_offset_col:-60]
gray = cv2.cvtColor(crop, cv2.COLOR_BGR2GRAY)
blur = cv2.blur(gray, (4,4))
_, binary = cv2.threshold(blur, 200, 256, cv2.THRESH_BINARY)
# cntrs, _ = cv2.findContours(binary, 1, 2)
# cnt = cntrs[0]
x,y,w,h = cv2.boundingRect(binary)
x += crop_offset_col
y += crop_offset_row
#cv2.rectangle(img,(x,y),(x+w,y+h),(0,255,0),2)
#cv2.imshow('original', img)
#cv2.imshow('crop', crop)
# cv2.imshow('Gray image', gray)
# cv2.imshow('Blur', blur)
#cv2.imshow('binary', binary)
#cv2.imshow('bbox', img)
#cv2.waitKey(10)
return x, y, w, h
def check_gripper_bbox(img):
crop_offset_col = 300
crop_offset_row = 100
crop = img[crop_offset_row:crop_offset_row+100, crop_offset_col:-60]
gray = cv2.cvtColor(crop, cv2.COLOR_BGR2GRAY)
blur = cv2.blur(gray, (4,4))
_, binary = cv2.threshold(blur, 200, 256, cv2.THRESH_BINARY)
# cntrs, _ = cv2.findContours(binary, 1, 2)
# cnt = cntrs[0]
x,y,w,h = cv2.boundingRect(binary)
x += crop_offset_col
y += crop_offset_row
#cv2.rectangle(img,(x,y),(x+w,y+h),(0,255,0),2)
#cv2.imshow('original', img)
#cv2.imshow('crop', crop)
# cv2.imshow('Gray image', gray)
# cv2.imshow('Blur', blur)
#cv2.imshow('binary', binary)
#cv2.imshow('bbox', img)
#cv2.waitKey(10)
return x, y, w, h
if __name__ == '__main__':
cam = realsense()
while(True):
_, img = cam.get_frame_cv()
crop_offset_col = 300
crop_offset_row = 100
crop = img[crop_offset_row:, crop_offset_col:-60]
gray = cv2.cvtColor(crop, cv2.COLOR_BGR2GRAY)
blur = cv2.blur(gray, (4,4))
_, binary = cv2.threshold(blur, 200, 256, cv2.THRESH_BINARY)
# cntrs, _ = cv2.findContours(binary, 1, 2)
# cnt = cntrs[0]
x,y,w,h = cv2.boundingRect(binary)
x += crop_offset_col
y += crop_offset_row
cv2.rectangle(img,(x,y),(x+w,y+h),(0,255,0),2)
print("bbox: {}, {}, {}, {}".format(x,y,w,h))
#cv2.imshow('original', img)
cv2.imshow('crop', crop)
# cv2.imshow('Gray image', gray)
# cv2.imshow('Blur', blur)
cv2.imshow('binary', binary)
cv2.imshow('bbox', img)
cv2.waitKey(10)
#cv2.destroyAllWindows()