-
Notifications
You must be signed in to change notification settings - Fork 0
/
coordinate.py
114 lines (99 loc) · 3.13 KB
/
coordinate.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
from cv2 import cv2
import numpy as np
import math
from pyrealsense2 import pyrealsense2 as rs
import time
import png
import corner
from camera import Camera
from yolo6D.utils import get_camera_intrinsic
def getCoordinate(img, depth, M):
return cv2.reprojectImageTo3D(img, M, depth,)
def Depth2World(u, v, depth):
'''深度图(u,v为像素坐标)转世界坐标'''
return Pixel2ImagePlane(u, v) * depth
def Pixel2ImagePlane(u, v):
'''像素坐标转成像平面坐标'''
fx = 618.33
fy = 618.33
ppx = 309.9
ppy = 237.5
x = (u - ppx) / fx
y = (v - ppy) / fy
return (x, y, 1)
def World2Image(wrld):
'''世界坐标转像素坐标'''
fx = 618.33
fy = 618.33
ppx = 309.9
ppy = 237.5
x = wrld[0] / wrld[2]
y = wrld[1] / wrld[2]
x = x * fx + ppx
y = y * fy + ppy
return (x, y)
def ReadData():
fp = open('data1.txt', 'r')
lines = fp.readlines()
options = dict()
x = np.zeros((6, 1))
for line in lines:
line = line.strip()
if line == '':
continue
key, value = line.split('=')
key = key.strip()
value = value.strip()
options[key] = value
x[0] = options['red_low']
x[1] = options['red_high']
x[2] = options['green_low']
x[3] = options['green_high']
x[4] = options['blue_low']
x[5] = options['blue_high']
fp.close()
ca = np.zeros((3, 1))
ho = np.zeros((3, 1))
fp = open('data2.txt', 'r')
lines = fp.readlines()
for line in lines:
line = line.strip()
if line == '':
continue
key, value = line.split('=')
key = key.strip()
value = value.strip()
options[key] = value
ca[0] = options['canny_threshold_1']
ca[1] = options['canny_threshold_2']
ca[2] = options['sobel']
ho[0] = options['hough_rho']
ho[1] = int(options['hough_theta']) * math.pi/180
ho[2] = options['hough_threshold']
fp.close()
return x, ca, ho
if __name__ == '__main__':
camera = Camera()
camera.init()
time.sleep(2)
depth, img = camera.capture()
cv2.imwrite('JPEGImages/0.jpg', img)
cv2.imwrite('depth.jpg', depth)
x, ca, ho = ReadData()
lined , Table_2D = corner.square_desk(0, x, ca, ho)
affine_table_2D = np.float32(
[[0, 0], [0, 550], [550, 0], [550, 550]]) # 方桌边长550mm
# M = cv2.getPerspectiveTransform(Table_2D, affine_table_2D)
# print(getCoordinate(img, depth, M))
(u, v) = Table_2D[1]
# print(Depth2World(u, v, depth[int(u), int(v)]))
rs.rs2_deproject_pixel_to_point(get_camera_intrinsic(), (u, v), depth_of_pixel)
# From pixel to 3D point
depth_point = rs.rs2_deproject_pixel_to_point(depth_intrin, depth_pixel, depth_value)
print("\n\t 3D depth_point: " + str(depth_point))
# From 3D depth point to 3D color point
color_point = rs.rs2_transform_point_to_point(depth_to_color_extrin, depth_point)
print("\n\t 3D color_point: " + str(color_point))
# From color point to 2D color pixel
color_pixel = rs.rs2_project_point_to_pixel(color_intrin, color_point)
print("\n\t color_pixel: " + str(color_pixel))