-
Notifications
You must be signed in to change notification settings - Fork 0
/
DisparityMatlab.m
237 lines (186 loc) · 6.09 KB
/
DisparityMatlab.m
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
function [y,err] = DisparityMatlab(left,right)
%global d focalLength principalPoint imageSize reprojectionMatrix
global disparityswitch points3Dswitch ORBswitch
% a=left;
% b=right;
% left=b;
% right=a;
if disparityswitch==1
d=0.2;
focalLength = [1109, 1109]; % In pixels
principalPoint = [640, 360]; % In pixels [x, y]
imageSize = [720, 1280]; % In pixels [mrows, ncols]
baseline = d; % In meters
reprojectionMatrix = [1, 0, 0, -principalPoint(1);
0, 1, 0, -principalPoint(2);
0, 0, 0, focalLength(1);
0, 0, 1/baseline, 0];
intrinsics = cameraIntrinsics(focalLength,principalPoint,imageSize);
stereoParams = stereoParameters(intrinsics, intrinsics, eye(3), [-baseline, 0 0]);
[currILeft, currIRight, r] = rectifyStereoImages(left, right, stereoParams);
frameLeftGray = rgb2gray(currILeft);
frameRightGray = rgb2gray(currIRight);
frameLeftGray = rgb2gray(left);
frameRightGray = rgb2gray(right);
disparityMap = disparitySGM(frameLeftGray, frameRightGray);
%
% disparityRange = [0 48];
% disparityMap = disparitySGM(frameLeftGray, frameRightGray,'DisparityRange',disparityRange,'UniquenessThreshold',20);
% imshow(disparityMap, [0, 64]);
% title('Disparity Map');
% colormap jet
%
% J = imadjust(disparityMap,[1 60]);
%
% cmap = jet(256);
% % a(isnan(disparityMap))=0;
% % rgbImage = cat(3, a, a, a);
% J = ind2gray(RGB2,cmap);
% J = rgb2ind(RGB2,cmap);
% RGB2 = im2uint8(rgbImage);
% a=disparityMap;
% a(a==NaN) = 0;
%
% figure;
% imshow(stereoAnaglyph(currILeft,currIRight));
%SELECT POINTS BETWEEN 3.2 and 3.7 meters away from the camera.
% Z = xyzPoints(:,:,3);
% mask = repmat(Z > 3200 & Z < 3700,[1,1,3]);
% J1(~mask) = 0;
% imshow(J1,'InitialMagnification',50);
depth=focalLength(1)*d./disparityMap;
depth(isinf(depth)|isnan(depth)) = 100000; %I made zero inf or NaN take care!
%
% e=abs(trueground-depth);
%
% RGB2e =im2uint8(e / 350); %this value corta OJO
% cmap = gray(60); %this value corta OJO
% Je = ind2gray(RGB2e,cmap);
% rgbImagee = ind2rgb(Je, flipud(cmap));
% imshow(rgbImagee)
RGB2 =im2uint8(depth / 255); %this value corta OJO
cmap = hot(150); %this 150 corta OJO
J = ind2gray(RGB2,cmap);
rgbImage = ind2rgb(J, cmap);
if points3Dswitch==1
points3D = reconstructScene(disparityMap, reprojectionMatrix);
ptCloud = pointCloud(points3D, 'Color', right);
%pcshow(ptCloud)
% Create a streaming point cloud viewer
player3D = pcplayer([-15, 15], [-10, 10], [-0.3,50], 'VerticalAxis', 'y', 'VerticalAxisDir', 'down');
%player3D = pcplayer([-15, 15], [-10, 10], [-20,20], 'VerticalAxis', 'y', 'VerticalAxisDir', 'down');
% Visualize the point cloud
view(player3D, ptCloud);
[ptCloudB,inlierIndices,outlierIndices] = pcdenoise(ptCloud,'NumNeighbors',1,'Threshold',0.0005,'PreserveStructure',true);
ptCloudA = pcdownsample(ptCloudB,'gridAverage',0.5,'PreserveStructure',true);
ptCloudB.Count
ptCloudA.Count
player3D = pcplayer([-15, 15], [-10, 10], [-0.3,50], 'VerticalAxis', 'y', 'VerticalAxisDir', 'down');
view(player3D, ptCloudA);
end
% Deleted points in downsampling
% stepSize = floor(ptCloudB.Count/ptCloudA.Count);
% indices = 1:stepSize:ptCloudB.Count;
% ptCloudB = select(ptCloudB, indices);
%
%look for the points in meters radius from a point
% point = [0,0,3];
% radius = 5;
% [indices,dists] = findNeighborsInRadius(ptCloud,point,radius);
% ptCloudB = select(ptCloud,indices);
% figure
% pcshow(ptCloud)
% hold on
% plot3(point(1),point(2),point(3),'*')
% pcshow(ptCloudB.Location,'r')
% legend('Point Cloud','Query Point','Radial Neighbors','Location','southoutside','Color',[0 1 1])
% xlim([-15, 15])
% ylim([-10, 10])
% zlim([-0.3,50])
% hold off
% %clustering
% distThreshold = 1; %0.1 also works
% labels = segmentLidarData(ptCloud,distThreshold);
% figure
% hold on
% title('Segmented Clusters')%
% m = mode(labels(:))
% labels2=labels;
% labels2(labels2==m) = [] ;
% m2 = mode(labels2(:));
% labels3=labels2;
% labels3(labels3==m2) = [];
% m3 = mode(labels3(:))
% labels4=labels3;
% labels4(labels4==m3) = [];
% m4 = mode(labels4(:));
% labels5=labels4;
% labels5(labels5==m4) = [];
% m5 = mode(labels5(:));
% pc1 = select(ptCloud,find(labels == m2));
% pcshow(pc1.Location,'g')
% hold on
% pc2 = select(ptCloud,find(labels == m3));
% pcshow(pc2.Location,'b')
% pc3 = select(ptCloud,find(labels == m4));
% pcshow(pc3.Location,'r')
% pc4 = select(ptCloud,find(labels == m5));
% pcshow(pc4.Location,'y')
% xlim([-15, 15])
% ylim([-10, 10])
% zlim([-0.3,50])
% hold off
% figure
% pcshow(ptCloud.Location,'r')
% xlim([-15, 15])
% ylim([-10, 10])
% zlim([-0.3,50])
%
% %clustering
% groundPtsIdx = segmentGroundFromLidarData(ptCloud);
% ptCloudWithoutGround = select(ptCloud,~groundPtsIdx,'OutputSize','full');
% distThreshold = 1;
% [labels,numClusters] = segmentLidarData(ptCloudWithoutGround,distThreshold);
% numClusters = numClusters+1;
% labels(groundPtsIdx) = numClusters;labelColorIndex = labels+1;
% pcshow(ptCloud.Location,labelColorIndex)
% colormap([hsv(numClusters);[0 0 0]])
% title('Point Cloud Clusters')
% xlim([-15, 15])
% ylim([-10, 10])
% zlim([-0.3,50])
% [indices,dists] = findNeighborsInRadius(ptCloud,[0,0,0],40);
% ptCloudC = select(ptCloud,indices);
% minDistance = 0.03;
% [labels,numClusters] = pcsegdist(ptCloudC,minDistance);
% pcshow(ptCloudC.Location,labels)
% colormap(hsv(numClusters))
% title('Point Cloud Clusters')
%% Overlay depth and rgb
% %Overlay depth and rgb
% imgRgb=left;
% imgDepthAbs= double(depth);
% % Check dims.
% assert(ndims(imgRgb) == 3);
% assert(ndims(imgDepthAbs) == 2);
% % Check sizes.
% [H, W, D] = size(imgRgb);
% assert(D == 3);
% assert(all(size(imgDepthAbs) == [H, W]));
% % Check types.
% assert(isa(imgRgb, 'uint8'));
% assert(isa(imgDepthAbs, 'double'));
% imgDepth = imgDepthAbs - min(imgDepthAbs(:));
% imgDepth = imgDepth ./ max(imgDepth(:));
% imgDepth = uint8(imgDepth * 255);
% imgOverlay = reshape(imgRgb, [H*W 3]);
% imgOverlay(:,3) = imgOverlay(:,2);
% imgOverlay(:,2) = imgDepth(:);
% imgOverlay = reshape(imgOverlay, [H, W, 3]);
% figure
% imshow(imgOverlay)
%%
y=im2uint8(rgbImage);
else
y = uint8(ones(720,1280,3));
end