Mean shift clustering – issue with finding the center of my clusters
Hi all, as you can see from the attached image, I cannot detect the center of my dots (in blu) by using the mean shift clustering. I will report the code below and I want to point out that I got the same result also chaining the bandwidht with any kind of number. Thanks a lot for helping me.
my code:
%%
% Import the data
% Prompt the user to choose a file
[filename, filepath] = uigetfile(‘*.txt’, ‘Select a text file’);
file_name = filename;
remove = ‘.txt’;
file_name_clean = strrep(file_name, remove, ”);
%%
% Plotting
plot_name = [‘Intensity_’ file_name_clean ‘.svg’];
% Import data from text file
opts = delimitedTextImportOptions("NumVariables", 28);
opts.DataLines = [2, Inf];
opts.Delimiter = "t";
opts.VariableNames = ["channel_name", "x", "y", "x_c", "y_c"];
opts.SelectedVariableNames = ["x", "y"]; % Only select the x and y columns
opts.VariableTypes = ["string", "double", "double", "double", "double"];
opts.ExtraColumnsRule = "ignore";
opts.EmptyLineRule = "read";
% Construct the full file path
file_path = fullfile(filepath, file_name);
data = readmatrix(file_path, opts);
% Perform Mean Shift clustering
bandwidth = 50; % bandwidth parameter for Mean Shift
[cluster_centers, data2cluster, cluster2dataCell] = MeanShiftCluster(data, bandwidth);
% Plotting the data with logarithmic x-axis and error bars for averages and standard deviations
figure;
plot(data(:,2), data(:,1), ‘.’, ‘MarkerSize’, 10, ‘DisplayName’, ‘XY coordinates’);
hold on;
% Set x-axis limit starting from 0
xlim([0, max(data(:,2))]);
% Set y-axis limit starting from 0
ylim([0, max(data(:,1))]);
% Plot cluster centers
hold on;
plot(cluster_centers(:,2), cluster_centers(:,1), ‘kx’, ‘MarkerSize’, 15, ‘LineWidth’, 3, ‘DisplayName’, ‘Cluster Centers’);
hold off;
xlabel(‘X’);
ylabel(‘Y’);
title(‘Mean Shift Clustering’);
legend(‘XY coordinates’, ‘Cluster Centers’);Hi all, as you can see from the attached image, I cannot detect the center of my dots (in blu) by using the mean shift clustering. I will report the code below and I want to point out that I got the same result also chaining the bandwidht with any kind of number. Thanks a lot for helping me.
my code:
%%
% Import the data
% Prompt the user to choose a file
[filename, filepath] = uigetfile(‘*.txt’, ‘Select a text file’);
file_name = filename;
remove = ‘.txt’;
file_name_clean = strrep(file_name, remove, ”);
%%
% Plotting
plot_name = [‘Intensity_’ file_name_clean ‘.svg’];
% Import data from text file
opts = delimitedTextImportOptions("NumVariables", 28);
opts.DataLines = [2, Inf];
opts.Delimiter = "t";
opts.VariableNames = ["channel_name", "x", "y", "x_c", "y_c"];
opts.SelectedVariableNames = ["x", "y"]; % Only select the x and y columns
opts.VariableTypes = ["string", "double", "double", "double", "double"];
opts.ExtraColumnsRule = "ignore";
opts.EmptyLineRule = "read";
% Construct the full file path
file_path = fullfile(filepath, file_name);
data = readmatrix(file_path, opts);
% Perform Mean Shift clustering
bandwidth = 50; % bandwidth parameter for Mean Shift
[cluster_centers, data2cluster, cluster2dataCell] = MeanShiftCluster(data, bandwidth);
% Plotting the data with logarithmic x-axis and error bars for averages and standard deviations
figure;
plot(data(:,2), data(:,1), ‘.’, ‘MarkerSize’, 10, ‘DisplayName’, ‘XY coordinates’);
hold on;
% Set x-axis limit starting from 0
xlim([0, max(data(:,2))]);
% Set y-axis limit starting from 0
ylim([0, max(data(:,1))]);
% Plot cluster centers
hold on;
plot(cluster_centers(:,2), cluster_centers(:,1), ‘kx’, ‘MarkerSize’, 15, ‘LineWidth’, 3, ‘DisplayName’, ‘Cluster Centers’);
hold off;
xlabel(‘X’);
ylabel(‘Y’);
title(‘Mean Shift Clustering’);
legend(‘XY coordinates’, ‘Cluster Centers’); Hi all, as you can see from the attached image, I cannot detect the center of my dots (in blu) by using the mean shift clustering. I will report the code below and I want to point out that I got the same result also chaining the bandwidht with any kind of number. Thanks a lot for helping me.
my code:
%%
% Import the data
% Prompt the user to choose a file
[filename, filepath] = uigetfile(‘*.txt’, ‘Select a text file’);
file_name = filename;
remove = ‘.txt’;
file_name_clean = strrep(file_name, remove, ”);
%%
% Plotting
plot_name = [‘Intensity_’ file_name_clean ‘.svg’];
% Import data from text file
opts = delimitedTextImportOptions("NumVariables", 28);
opts.DataLines = [2, Inf];
opts.Delimiter = "t";
opts.VariableNames = ["channel_name", "x", "y", "x_c", "y_c"];
opts.SelectedVariableNames = ["x", "y"]; % Only select the x and y columns
opts.VariableTypes = ["string", "double", "double", "double", "double"];
opts.ExtraColumnsRule = "ignore";
opts.EmptyLineRule = "read";
% Construct the full file path
file_path = fullfile(filepath, file_name);
data = readmatrix(file_path, opts);
% Perform Mean Shift clustering
bandwidth = 50; % bandwidth parameter for Mean Shift
[cluster_centers, data2cluster, cluster2dataCell] = MeanShiftCluster(data, bandwidth);
% Plotting the data with logarithmic x-axis and error bars for averages and standard deviations
figure;
plot(data(:,2), data(:,1), ‘.’, ‘MarkerSize’, 10, ‘DisplayName’, ‘XY coordinates’);
hold on;
% Set x-axis limit starting from 0
xlim([0, max(data(:,2))]);
% Set y-axis limit starting from 0
ylim([0, max(data(:,1))]);
% Plot cluster centers
hold on;
plot(cluster_centers(:,2), cluster_centers(:,1), ‘kx’, ‘MarkerSize’, 15, ‘LineWidth’, 3, ‘DisplayName’, ‘Cluster Centers’);
hold off;
xlabel(‘X’);
ylabel(‘Y’);
title(‘Mean Shift Clustering’);
legend(‘XY coordinates’, ‘Cluster Centers’); mean shift clustering MATLAB Answers — New Questions