How to obtain the average diameter of a segment of a graph?
I have a graph structure that is formed after binarizing an image of blood vessels and obtaining the skeleton, then creating a graph structure. The graph looks like this:
K >> graph =
graph with properties:
Edges: [773×2 table]
Nodes: [773×0 table]
Which means I have 773 edges and each edge has 2 vertices. The graph edges look like this:
EndNodes Weight
__________ ______
1 2 1
1 53 1.4142
2 3 1
3 4 1
4 5 1
Now, at the end of the graph creation, I have other helper functions that help me create a diameters table with points that have been inferred on the binary image between the vessel walls. So, for each graph point the algorithm tries to look for walls and once found it saves the coordinates and other values in a table.
This is how the table gets created and how it looks like:
diam_tab = table();
for i = 1:n_line % for all the lines detected
row = cell(1,7);
row{1} = dt(i,:); % the diameter measured
row{2} = t; % the time dimension
row{3} = fs; % the frame sampling rate
row{4} = squeeze(permute(w1_t(i,:,:), [1, 3, 2])); % The value coordiantes of the first wall
row{5} = squeeze(permute(w2_t(i,:,:), [1, 3, 2])); % The value coordinates of the second wall
row{6} = line_type(i); % just a string, not relevant
row{7} = util.make_uuid(); % an UUID attached
diam_tab = [diam_tab; row];
end
And finally look like this:
y x fs z1 z2 type UUID
______________ ______________ ______ ________________ ________________ ________ ________________________________________
1×19682 double 1×19682 double 21.383 {2×19682 double} {2×19682 double} "vessel" {‘6ada1ae1-00f8-4d9b-aa7a-80c7be26aaa1’}
1×19682 double 1×19682 double 21.383 {2×19682 double} {2×19682 double} "vessel" {‘b8da617c-f55f-418c-88dc-5862c968a637’}
Now, what I want to do is, modify this table so that it doesn’t take all of the 773 rows but instead only like an average of the diameters over a certain segment of the graph. Let’s suppose say that the graph has a Y shape, I would like to obtain, every lets say 10 points an average diameter of that section.
Then, finally, the diam_tab table will not have 773 rows, but only the ones left after averaging over several sections of the graph, let’s say for a graph length of 100 the result would be 100 rows, but I want to get instead the average of the graph at sections of length for ex 10, which would leave me with 10 rows.
And finally, the diam_table would be updated to reflect these values, or at the very least replaced by a similar functioning table.
So far, this is what I turned up with in my code, which isn’t exactly working well as it seems to only get center points between two nodes and then breaks when I attach the created structure to the graph.
function diameters_table = segment_vessel_table(ts, diam_tab)
graph = ts.load_var(‘center_line’).graph;
[y, x] = find(ts.load_var(‘binary_image’));
node_coordinates = [x, y];
n_vessels = height(diam_tab);
midpoints = zeros(n_vessels, 2);
n_samples_graph = 5;
for i = 1:n_vessels
z1 = diam_tab.z1{i};
z2 = diam_tab.z2{i};
mids = (z1 + z2) / 2;
midpoints(i, 🙂 = mean(mids, 2);
end
idx = round(linspace(1, size(z1, 2), n_samples_graph));
sample_points = mean((z1(:, idx) + z2(:, idx)) / 2, 2);
graph = calculate_branch_centers(graph, node_coordinates);
distances = pdist2(midpoints, graph.Nodes.branch_centers);
[~, branch_idx] = min(distances, [], 2);
diam_tab.branch_id = branch_idx;
diameters_table = diam_tab;
end
function graph = calculate_branch_centers(graph, node_coordinates)
n_edges = height(graph.Edges);
branch_centers = zeros(n_edges, 2);
for i = 1:n_edges
node1 = graph.Edges.EndNodes(i, 1);
node2 = graph.Edges.EndNodes(i, 2);
p1 = node_coordinates(node1, :);
p2 = node_coordinates(node2, :);
branch_centers(i, 🙂 = (p1 + p2) / 2;
end
graph.branch_centers = branch_centers; % it breaks here
end
function [xs, x_names, fs] = load_vars(trial)
ts = trial.load_var("tseries");
diam_tab = ts.load_var("diameters");
diam_tab = segment_vessel_table(ts, diam_tab);
dt = diam_tab{:, "y"};
t = diam_tab.x;
t = t(1,:);
xs = TimeSeries(t, dt’);
x_names = "diameter";
% Freq bands is another part that works and not shown atm
for i = 1:size(freq_bands,1)
name = freq_bands{i,1};
xs = [xs; bands.(name)];
x_names = [x_names; name];
end
endI have a graph structure that is formed after binarizing an image of blood vessels and obtaining the skeleton, then creating a graph structure. The graph looks like this:
K >> graph =
graph with properties:
Edges: [773×2 table]
Nodes: [773×0 table]
Which means I have 773 edges and each edge has 2 vertices. The graph edges look like this:
EndNodes Weight
__________ ______
1 2 1
1 53 1.4142
2 3 1
3 4 1
4 5 1
Now, at the end of the graph creation, I have other helper functions that help me create a diameters table with points that have been inferred on the binary image between the vessel walls. So, for each graph point the algorithm tries to look for walls and once found it saves the coordinates and other values in a table.
This is how the table gets created and how it looks like:
diam_tab = table();
for i = 1:n_line % for all the lines detected
row = cell(1,7);
row{1} = dt(i,:); % the diameter measured
row{2} = t; % the time dimension
row{3} = fs; % the frame sampling rate
row{4} = squeeze(permute(w1_t(i,:,:), [1, 3, 2])); % The value coordiantes of the first wall
row{5} = squeeze(permute(w2_t(i,:,:), [1, 3, 2])); % The value coordinates of the second wall
row{6} = line_type(i); % just a string, not relevant
row{7} = util.make_uuid(); % an UUID attached
diam_tab = [diam_tab; row];
end
And finally look like this:
y x fs z1 z2 type UUID
______________ ______________ ______ ________________ ________________ ________ ________________________________________
1×19682 double 1×19682 double 21.383 {2×19682 double} {2×19682 double} "vessel" {‘6ada1ae1-00f8-4d9b-aa7a-80c7be26aaa1’}
1×19682 double 1×19682 double 21.383 {2×19682 double} {2×19682 double} "vessel" {‘b8da617c-f55f-418c-88dc-5862c968a637’}
Now, what I want to do is, modify this table so that it doesn’t take all of the 773 rows but instead only like an average of the diameters over a certain segment of the graph. Let’s suppose say that the graph has a Y shape, I would like to obtain, every lets say 10 points an average diameter of that section.
Then, finally, the diam_tab table will not have 773 rows, but only the ones left after averaging over several sections of the graph, let’s say for a graph length of 100 the result would be 100 rows, but I want to get instead the average of the graph at sections of length for ex 10, which would leave me with 10 rows.
And finally, the diam_table would be updated to reflect these values, or at the very least replaced by a similar functioning table.
So far, this is what I turned up with in my code, which isn’t exactly working well as it seems to only get center points between two nodes and then breaks when I attach the created structure to the graph.
function diameters_table = segment_vessel_table(ts, diam_tab)
graph = ts.load_var(‘center_line’).graph;
[y, x] = find(ts.load_var(‘binary_image’));
node_coordinates = [x, y];
n_vessels = height(diam_tab);
midpoints = zeros(n_vessels, 2);
n_samples_graph = 5;
for i = 1:n_vessels
z1 = diam_tab.z1{i};
z2 = diam_tab.z2{i};
mids = (z1 + z2) / 2;
midpoints(i, 🙂 = mean(mids, 2);
end
idx = round(linspace(1, size(z1, 2), n_samples_graph));
sample_points = mean((z1(:, idx) + z2(:, idx)) / 2, 2);
graph = calculate_branch_centers(graph, node_coordinates);
distances = pdist2(midpoints, graph.Nodes.branch_centers);
[~, branch_idx] = min(distances, [], 2);
diam_tab.branch_id = branch_idx;
diameters_table = diam_tab;
end
function graph = calculate_branch_centers(graph, node_coordinates)
n_edges = height(graph.Edges);
branch_centers = zeros(n_edges, 2);
for i = 1:n_edges
node1 = graph.Edges.EndNodes(i, 1);
node2 = graph.Edges.EndNodes(i, 2);
p1 = node_coordinates(node1, :);
p2 = node_coordinates(node2, :);
branch_centers(i, 🙂 = (p1 + p2) / 2;
end
graph.branch_centers = branch_centers; % it breaks here
end
function [xs, x_names, fs] = load_vars(trial)
ts = trial.load_var("tseries");
diam_tab = ts.load_var("diameters");
diam_tab = segment_vessel_table(ts, diam_tab);
dt = diam_tab{:, "y"};
t = diam_tab.x;
t = t(1,:);
xs = TimeSeries(t, dt’);
x_names = "diameter";
% Freq bands is another part that works and not shown atm
for i = 1:size(freq_bands,1)
name = freq_bands{i,1};
xs = [xs; bands.(name)];
x_names = [x_names; name];
end
end I have a graph structure that is formed after binarizing an image of blood vessels and obtaining the skeleton, then creating a graph structure. The graph looks like this:
K >> graph =
graph with properties:
Edges: [773×2 table]
Nodes: [773×0 table]
Which means I have 773 edges and each edge has 2 vertices. The graph edges look like this:
EndNodes Weight
__________ ______
1 2 1
1 53 1.4142
2 3 1
3 4 1
4 5 1
Now, at the end of the graph creation, I have other helper functions that help me create a diameters table with points that have been inferred on the binary image between the vessel walls. So, for each graph point the algorithm tries to look for walls and once found it saves the coordinates and other values in a table.
This is how the table gets created and how it looks like:
diam_tab = table();
for i = 1:n_line % for all the lines detected
row = cell(1,7);
row{1} = dt(i,:); % the diameter measured
row{2} = t; % the time dimension
row{3} = fs; % the frame sampling rate
row{4} = squeeze(permute(w1_t(i,:,:), [1, 3, 2])); % The value coordiantes of the first wall
row{5} = squeeze(permute(w2_t(i,:,:), [1, 3, 2])); % The value coordinates of the second wall
row{6} = line_type(i); % just a string, not relevant
row{7} = util.make_uuid(); % an UUID attached
diam_tab = [diam_tab; row];
end
And finally look like this:
y x fs z1 z2 type UUID
______________ ______________ ______ ________________ ________________ ________ ________________________________________
1×19682 double 1×19682 double 21.383 {2×19682 double} {2×19682 double} "vessel" {‘6ada1ae1-00f8-4d9b-aa7a-80c7be26aaa1’}
1×19682 double 1×19682 double 21.383 {2×19682 double} {2×19682 double} "vessel" {‘b8da617c-f55f-418c-88dc-5862c968a637’}
Now, what I want to do is, modify this table so that it doesn’t take all of the 773 rows but instead only like an average of the diameters over a certain segment of the graph. Let’s suppose say that the graph has a Y shape, I would like to obtain, every lets say 10 points an average diameter of that section.
Then, finally, the diam_tab table will not have 773 rows, but only the ones left after averaging over several sections of the graph, let’s say for a graph length of 100 the result would be 100 rows, but I want to get instead the average of the graph at sections of length for ex 10, which would leave me with 10 rows.
And finally, the diam_table would be updated to reflect these values, or at the very least replaced by a similar functioning table.
So far, this is what I turned up with in my code, which isn’t exactly working well as it seems to only get center points between two nodes and then breaks when I attach the created structure to the graph.
function diameters_table = segment_vessel_table(ts, diam_tab)
graph = ts.load_var(‘center_line’).graph;
[y, x] = find(ts.load_var(‘binary_image’));
node_coordinates = [x, y];
n_vessels = height(diam_tab);
midpoints = zeros(n_vessels, 2);
n_samples_graph = 5;
for i = 1:n_vessels
z1 = diam_tab.z1{i};
z2 = diam_tab.z2{i};
mids = (z1 + z2) / 2;
midpoints(i, 🙂 = mean(mids, 2);
end
idx = round(linspace(1, size(z1, 2), n_samples_graph));
sample_points = mean((z1(:, idx) + z2(:, idx)) / 2, 2);
graph = calculate_branch_centers(graph, node_coordinates);
distances = pdist2(midpoints, graph.Nodes.branch_centers);
[~, branch_idx] = min(distances, [], 2);
diam_tab.branch_id = branch_idx;
diameters_table = diam_tab;
end
function graph = calculate_branch_centers(graph, node_coordinates)
n_edges = height(graph.Edges);
branch_centers = zeros(n_edges, 2);
for i = 1:n_edges
node1 = graph.Edges.EndNodes(i, 1);
node2 = graph.Edges.EndNodes(i, 2);
p1 = node_coordinates(node1, :);
p2 = node_coordinates(node2, :);
branch_centers(i, 🙂 = (p1 + p2) / 2;
end
graph.branch_centers = branch_centers; % it breaks here
end
function [xs, x_names, fs] = load_vars(trial)
ts = trial.load_var("tseries");
diam_tab = ts.load_var("diameters");
diam_tab = segment_vessel_table(ts, diam_tab);
dt = diam_tab{:, "y"};
t = diam_tab.x;
t = t(1,:);
xs = TimeSeries(t, dt’);
x_names = "diameter";
% Freq bands is another part that works and not shown atm
for i = 1:size(freq_bands,1)
name = freq_bands{i,1};
xs = [xs; bands.(name)];
x_names = [x_names; name];
end
end graph, table, handles, image processing MATLAB Answers — New Questions