# How to remove bulbs in the mouse trajectory?

4 views (last 30 days)
Struggling in MATLAB on 3 Jul 2022
I am trying to remove the bulbs in red circles in the picture to get a smooth trajectory as shown in the picture. The mouse may be sitting or rotating in those bulbs. My goal is to find the number of pixels (I can define my own pixel size) moved in their trajectory in the smooth profile.
I tried to divide the 2-D histogram with 50x50 bins and count the number of bins which are nonzero. Then I smoothed X and Y and plotted a 2-D histogram with 100x100 bins. I expected the number of nonzero bins to be higher in image 1 so that the number of pixels is more there. So clearly this method is not working. I have attached my code here.
How should I approach this problem?

Image Analyst on 8 Jul 2022
Try this. Adjust parameters as indicated to get what you want.
% Demo by Image Analyst
% Initialization Steps.
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 20;
x = xcoordinates.ans;
y = ycoordinates.ans;
xmin = min(x); xmax = max(x);
ymin = min(y); ymax = max(y);
subplot(2, 1, 1);
plot(x, y, 'b.-');
grid on;
hold on;
windowSize = 10; % Window to scan the array with.
xBoxWidth = 2.5;
yBoxWidth = 2.5;
% Scan array looking for clustered points all within the defined box width.
bulbIndexes = false(numel(x), 1);
for k = 2 : numel(x) - windowSize
% See if all x and y points in the window are within the box.
xWindow = x(k : k + windowSize - 1);
yWindow = y(k : k + windowSize - 1);
if range(xWindow) < xBoxWidth && range(yWindow) < yBoxWidth
plot(x(k : k + windowSize - 1), y(k : k + windowSize - 1), 'r.', 'MarkerSize', 10)
bulbIndexes(k : k + windowSize - 1) = true;
end
end
numPointsToBeDeleted = sum(bulbIndexes);
fprintf('Going to delete %d out of %d points.\n', numPointsToBeDeleted, numel(x))
caption = sprintf('Going to delete the %d points in red', numPointsToBeDeleted);
title(caption, 'FontSize', fontSize)
% Make a copy of the x and y
xRepaired = x;
yRepaired = y;
% Delete bulb indexes from the copies.
xRepaired(bulbIndexes) = [];
yRepaired(bulbIndexes) = [];
% Plot the repaired signals with the points deleted.
subplot(2, 1, 2);
plot(xRepaired, yRepaired, 'b.-');
grid on;
caption = sprintf('Final, repaired signal with %d points', numel(xRepaired));
title(caption, 'FontSize', fontSize) Struggling in MATLAB on 8 Jul 2022
Beautiful! Thank you very much!!

Image Analyst on 3 Jul 2022
Edited: Image Analyst on 3 Jul 2022
Would a good algorithm be that if there is a stretch of (x,y) coordinates where the x values were all contained in a certain delta X range then we can replace the range with the interpolated path from the start of the window to the end of the window?
Another way would be to write the path to a digital image and then use morphological methods to get rid of the bulb. You'd need to specify a size for the image that wuold depend on how much spatial precision you require (like as long as we're within 1 mm of the actual location, we'll accept that).
Or you can try smoothing the trace with a Savitzky-Golay filter sgolayfilt, like in my attached demo.
Or how about if you did a cluster analysis like kmeans and if any points identified as being in the cluster were within a certain distance of the cluster centroid, find the first index and last index in the main array and interpolate a straight line between them.
##### 2 CommentsShowHide 1 older comment
Struggling in MATLAB on 7 Jul 2022
I am not sure how to implement kmeans method. Because we need to specify how many clusters are there, which would change from trial to trial.
Your example file of Savitzky-Golay filter seems too complicated for me. I am not sure what is the correlation beteen trajectory and image. I see that there is only one boundary. I am not familiar with image processing by the way.
I don't really understand your third suggestion.
I can relate to the first solution you have provided. Can you please elaborate on that?

R2022a

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!