Skip to content

Commit c155e23

Browse files
committed
updated mesh-mapping files
1 parent ae38144 commit c155e23

9 files changed

+3887
-4621
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,132 +1,222 @@
1-
%% Tomaso Muzzu - UCL - 09/03/2021
2-
% edits by EH (March 2021).
3-
4-
%% Mesh mapping modification from Matlab
5-
% adapting meshmapping file obtained with Bonsai script.
6-
% Original is the name of the output of the Bonsai script
7-
8-
%% Variable assignment
9-
10-
inputFileName = 'BonVision_MeshMap.csv';
11-
outputFileName = 'MeshMapping_MatlabOutput.csv';
12-
DisplayDim = [1280 800]; % [horizontal_pixels, vertical_pixels]
13-
AngularSpan = [240 120]; % [azimuth_deg, elevation_deg]
14-
azimuthInterpValue = 6;
15-
elevationInterpValue = 6;
16-
plotFlag = true;
17-
18-
%% import csv file generated by Bonsai
19-
20-
[x_pix,y_pix,az_deg,el_deg] = importfile(inputFileName);
21-
22-
%% Perform interpolation
23-
% Create arrary with 5 columns as follows:
24-
% [pixel_x, pixel_y, newpixel_x, newpixel_y, brightness]
25-
MeshMapping = [x_pix,y_pix,az_deg,el_deg, ones(size(x_pix,1),1)];
26-
27-
% normalise coordinates
28-
MeshMapping = sortrows(MeshMapping,3);
29-
MeshMapping(:,3) = MeshMapping(:,3)/max(MeshMapping(:,3));
30-
MeshMapping(:,4) = MeshMapping(:,4)/max(MeshMapping(:,4));
31-
32-
if plotFlag
33-
figure
34-
subplot(121)
35-
plot(MeshMapping(:,1),MeshMapping(:,2),'or');
36-
title('Original mesh mapping file')
37-
end
38-
39-
FX = scatteredInterpolant(MeshMapping(:,3),MeshMapping(:,4),MeshMapping(:,1),'natural');
40-
FY = scatteredInterpolant(MeshMapping(:,3),MeshMapping(:,4),MeshMapping(:,2),'natural');
41-
FI = scatteredInterpolant(MeshMapping(:,3),MeshMapping(:,4),MeshMapping(:,5),'natural');
42-
43-
AzRange = linspace(0,1,AngularSpan(1)/azimuthInterpValue);
44-
ElRange = linspace(0,1,AngularSpan(2)/elevationInterpValue);
45-
[newDegAz, newDegEl ]= meshgrid(AzRange,ElRange);
46-
posX = FX(newDegAz,newDegEl);
47-
posY = FY(newDegAz,newDegEl);
48-
intensity = FI(newDegAz,newDegEl);
49-
50-
dataCell = [posX(:) -posY(:) newDegAz(:) newDegEl(:) intensity(:)];
51-
MeshNormInterp = sortrows(dataCell,3);
52-
53-
if plotFlag
54-
subplot(122)
55-
plot(MeshMapping(:,1),MeshMapping(:,2),'ro');
56-
hold on
57-
plot(MeshNormInterp(:,1),-1.*MeshNormInterp(:,2),'k.')
58-
legend({'original data', 'interpolated data'})
59-
title('Interpolated mesh map file')
60-
end
61-
62-
%% Generatefor 2019a onwards
63-
%writematrix(MeshNormInterp,outputFileName)
64-
65-
%% for older vesions
66-
dlmwrite(outputFileName, MeshNormInterp, 'precision', 32)
67-
68-
69-
70-
%% function for importing csv (autogenerated using matlab)
71-
function [x_pix,y_pix,az_deg,el_deg] = importfile(filename, startRow, endRow)
72-
%IMPORTFILE Import numeric data from a text file as column vectors.
73-
% [X_PIX,Y_PIX,AZ_DEG,EL_DEG] = IMPORTFILE(FILENAME) Reads data from text
74-
% file FILENAME for the default selection.
75-
%
76-
% [X_PIX,Y_PIX,AZ_DEG,EL_DEG] = IMPORTFILE(FILENAME, STARTROW, ENDROW)
77-
% Reads data from rows STARTROW through ENDROW of text file FILENAME.
78-
%
79-
% Example:
80-
% [x_pix,y_pix,az_deg,el_deg] = importfile('MeshMap_Corrected2021-03-17T17_09_50.csv',1, 65);
81-
%
82-
% See also TEXTSCAN.
83-
84-
% Auto-generated by MATLAB on 2021/03/22 12:04:45
85-
86-
%% Initialize variables.
87-
delimiter = ',';
88-
if nargin<=2
89-
startRow = 1;
90-
endRow = inf;
91-
end
92-
93-
%% Format for each line of text:
94-
% column1: double (%f)
95-
% column2: double (%f)
96-
% column3: double (%f)
97-
% column4: double (%f)
98-
% For more information, see the TEXTSCAN documentation.
99-
formatSpec = '%f%f%f%f%[^\n\r]';
100-
101-
%% Open the text file.
102-
fileID = fopen(filename,'r');
103-
104-
%% Read columns of data according to the format.
105-
% This call is based on the structure of the file used to generate this
106-
% code. If an error occurs for a different file, try regenerating the code
107-
% from the Import Tool.
108-
dataArray = textscan(fileID, formatSpec, endRow(1)-startRow(1)+1, 'Delimiter', delimiter, 'TextType', 'string', 'HeaderLines', startRow(1)-1, 'ReturnOnError', false, 'EndOfLine', '\r\n');
109-
for block=2:length(startRow)
110-
frewind(fileID);
111-
dataArrayBlock = textscan(fileID, formatSpec, endRow(block)-startRow(block)+1, 'Delimiter', delimiter, 'TextType', 'string', 'HeaderLines', startRow(block)-1, 'ReturnOnError', false, 'EndOfLine', '\r\n');
112-
for col=1:length(dataArray)
113-
dataArray{col} = [dataArray{col};dataArrayBlock{col}];
114-
end
115-
end
116-
117-
%% Close the text file.
118-
fclose(fileID);
119-
120-
%% Post processing for unimportable data.
121-
% No unimportable data rules were applied during the import, so no post
122-
% processing code is included. To generate code which works for
123-
% unimportable data, select unimportable cells in a file and regenerate the
124-
% script.
125-
126-
%% Allocate imported array to column variable names
127-
x_pix = dataArray{:, 1};
128-
y_pix = dataArray{:, 2};
129-
az_deg = dataArray{:, 3};
130-
el_deg = dataArray{:, 4};
131-
132-
end
1+
%% Tomaso Muzzu - UCL - 09/03/2021
2+
% edits by EH (March 2021).
3+
% edits by EH (August 2021) - convert to cartesian co-ordinates and
4+
% linearly spaced pixel interpolation
5+
6+
%% Mesh mapping modification from Matlab
7+
% adapting meshmapping file obtained with Bonsai script.
8+
% Original is the name of the output of the Bonsai script
9+
10+
%% Variable assignment
11+
12+
inputFileName = 'Tron_MeshMap_Corrected2023-06-16T15_28_09.csv';
13+
outputFileName = 'Tron_MeshMapping3D_MatlabOutput.bin';
14+
15+
DisplayDim = [1280 800]; % [horizontal_pixels, vertical_pixels]
16+
AngularSpan = [240 120]; % [azimuth_deg, elevation_deg]
17+
azimuthInterpValue = 6;
18+
elevationInterpValue = 6;
19+
plotFlag = true;
20+
21+
%% import csv file generated by Bonsai
22+
23+
[x_pix,y_pix,az_deg,el_deg] = importfile(inputFileName);
24+
25+
26+
%% Perform interpolation
27+
% Create arrary with 5 columns as follows:
28+
% [pixel_x, pixel_y, newpixel_x, newpixel_y, brightness]
29+
MeshMapping = [x_pix,y_pix,az_deg,el_deg, ones(size(x_pix,1),1)];
30+
31+
% normalise coordinates
32+
MeshMapping = sortrows(MeshMapping,3);
33+
MeshMapping(:,3) = MeshMapping(:,3)/max(MeshMapping(:,3));
34+
MeshMapping(:,4) = MeshMapping(:,4)/max(MeshMapping(:,4));
35+
36+
FX = scatteredInterpolant(MeshMapping(:,3),MeshMapping(:,4),MeshMapping(:,1),'natural');
37+
FY = scatteredInterpolant(MeshMapping(:,3),MeshMapping(:,4),MeshMapping(:,2),'natural');
38+
FI = scatteredInterpolant(MeshMapping(:,3),MeshMapping(:,4),MeshMapping(:,5),'natural');
39+
40+
AzRange = linspace(0,1,AngularSpan(1)/azimuthInterpValue);
41+
ElRange = linspace(0,1,AngularSpan(2)/elevationInterpValue);
42+
[newDegAz, newDegEl ]= meshgrid(AzRange,ElRange);
43+
posX = FX(newDegAz,newDegEl);
44+
posY = FY(newDegAz,newDegEl);
45+
intensity = FI(newDegAz,newDegEl);
46+
47+
48+
dataCell = [posX(:) -posY(:) newDegAz(:) newDegEl(:) intensity(:)];
49+
MeshNormInterp = sortrows(dataCell,3);
50+
51+
if plotFlag
52+
figure
53+
subplot(121)
54+
plot(MeshMapping(:,1),MeshMapping(:,2),'or');
55+
title('Original mesh mapping file')
56+
subplot(122)
57+
plot(MeshMapping(:,1),MeshMapping(:,2),'ro');
58+
hold on
59+
plot(MeshNormInterp(:,1),-1.*MeshNormInterp(:,2),'k.')
60+
legend({'original data', 'interpolated data'})
61+
title('Interpolated mesh map file')
62+
end
63+
64+
65+
%% get linearly spaced coordinates in pixel-space
66+
67+
newX = (posX+1)/2;
68+
newY = 1-((posY+1)/2);
69+
min_x = min(posX(:)); max_x = max(posX(:));
70+
min_y = min(posY(:)); max_y = max(posY(:));
71+
72+
%x_coords = round(min_x,3):0.001:round(max_x,3);
73+
%y_coords = round(min_y,3):0.001:round(max_y,3);
74+
75+
[Xq, Yq] = meshgrid(0:0.001:1,0:0.001:1);
76+
77+
Az_interp = griddata(newX,newY,newDegAz,Xq,Yq,'natural');
78+
El_interp = griddata(newX,newY,newDegEl,Xq,Yq,'natural');
79+
80+
81+
%% convert back to real angles
82+
max_Az = 120;
83+
min_Az = -120;
84+
max_El = 90;
85+
min_El = -30;
86+
87+
Az_interp_angles = Az_interp.*(max_Az-min_Az) + min_Az;
88+
El_interp_angles = El_interp.*(min_El-max_El) + max_El;
89+
90+
91+
figure
92+
subplot(121)
93+
imagesc(Az_interp_angles), colorbar
94+
title('Azimuth map')
95+
subplot(122)
96+
imagesc(El_interp_angles), colorbar
97+
title('Elevation map')
98+
99+
100+
%% convert from spherical to cartesian coordinates
101+
102+
% azimuth and elevation need some pre-processing
103+
% Az_interp_angles = -1.*Az_interp_angles; % currently uncommented to
104+
% improve Tron meshmapping alignment
105+
[x,y,z] = sph2cart(deg2rad(Az_interp_angles), deg2rad(El_interp_angles),1);
106+
107+
figure
108+
subplot(1,3,1)
109+
imagesc(x); colorbar; title('x vector')
110+
subplot(1,3,2)
111+
imagesc(y);colorbar;title('y vector')
112+
subplot(1,3,3)
113+
imagesc(z);colorbar;title('z vector')
114+
115+
%% save as a binary
116+
117+
% M = cat(3,x,y,z);
118+
M = cat(3,-x,y,z);
119+
120+
% % M = cat(3,-x,-y,z);ggg
121+
% %M = cat(3,z,y,x);
122+
%
123+
% %fid = fopen('MeshMap3dMatrix_xy.bin', 'w');
124+
fid = fopen(outputFileName, 'w');
125+
126+
fwrite(fid,M,'double');
127+
fclose(fid);
128+
129+
%% save as a binary (float32 version)
130+
131+
% % M = cat(3,x,y,z);
132+
% % M = permute(cat(3,-x,y,z), [3 1 2]);
133+
% M = cat(3,-x,y,z);
134+
% M = reshape(M,[3,size(M,1),size(M,1)]);
135+
% M = reshape(M,[],1);
136+
% M = single(M);
137+
%
138+
% % M = permute(cat(3,-x,y,z), [2 3 1]);
139+
%
140+
% % M=M';
141+
% % M = cat(3,-x,-y,z);
142+
% %M = cat(3,z,y,x);
143+
% % M = cat(3,-x,y,z);
144+
% %fid = fopen('MeshMap3dMatrix_xy.bin', 'w');
145+
% fid = fopen(outputFileName, 'w');
146+
%
147+
% fwrite(fid,M,'float32');
148+
% fclose(fid);
149+
150+
%% Save output file 2019a onwards
151+
% writematrix(MeshNormInterp,outputFileName)
152+
153+
%% for older vesions
154+
%dlmwrite(outputFileName, MeshNormInterp, 'precision', 32)
155+
156+
157+
158+
159+
160+
%% function for importing csv (autogenerated using matlab)
161+
function [x_pix,y_pix,az_deg,el_deg] = importfile(filename, startRow, endRow)
162+
%IMPORTFILE Import numeric data from a text file as column vectors.
163+
% [X_PIX,Y_PIX,AZ_DEG,EL_DEG] = IMPORTFILE(FILENAME) Reads data from text
164+
% file FILENAME for the default selection.
165+
%
166+
% [X_PIX,Y_PIX,AZ_DEG,EL_DEG] = IMPORTFILE(FILENAME, STARTROW, ENDROW)
167+
% Reads data from rows STARTROW through ENDROW of text file FILENAME.
168+
%
169+
% Example:
170+
% [x_pix,y_pix,az_deg,el_deg] = importfile('MeshMap_Corrected2021-03-17T17_09_50.csv',1, 65);
171+
%
172+
% See also TEXTSCAN.
173+
174+
% Auto-generated by MATLAB on 2021/03/22 12:04:45
175+
176+
%% Initialize variables.
177+
delimiter = ',';
178+
if nargin<=2
179+
startRow = 1;
180+
endRow = inf;
181+
end
182+
183+
%% Format for each line of text:
184+
% column1: double (%f)
185+
% column2: double (%f)
186+
% column3: double (%f)
187+
% column4: double (%f)
188+
% For more information, see the TEXTSCAN documentation.
189+
formatSpec = '%f%f%f%f%[^\n\r]';
190+
191+
%% Open the text file.
192+
fileID = fopen(filename,'r');
193+
194+
%% Read columns of data according to the format.
195+
% This call is based on the structure of the file used to generate this
196+
% code. If an error occurs for a different file, try regenerating the code
197+
% from the Import Tool.
198+
dataArray = textscan(fileID, formatSpec, endRow(1)-startRow(1)+1, 'Delimiter', delimiter, 'TextType', 'string', 'HeaderLines', startRow(1)-1, 'ReturnOnError', false, 'EndOfLine', '\r\n');
199+
for block=2:length(startRow)
200+
frewind(fileID);
201+
dataArrayBlock = textscan(fileID, formatSpec, endRow(block)-startRow(block)+1, 'Delimiter', delimiter, 'TextType', 'string', 'HeaderLines', startRow(block)-1, 'ReturnOnError', false, 'EndOfLine', '\r\n');
202+
for col=1:length(dataArray)
203+
dataArray{col} = [dataArray{col};dataArrayBlock{col}];
204+
end
205+
end
206+
207+
%% Close the text file.
208+
fclose(fileID);
209+
210+
%% Post processing for unimportable data.
211+
% No unimportable data rules were applied during the import, so no post
212+
% processing code is included. To generate code which works for
213+
% unimportable data, select unimportable cells in a file and regenerate the
214+
% script.
215+
216+
%% Allocate imported array to column variable names
217+
x_pix = dataArray{:, 1};
218+
y_pix = dataArray{:, 2};
219+
az_deg = dataArray{:, 3};
220+
el_deg = dataArray{:, 4};
221+
222+
end

0 commit comments

Comments
 (0)