please help me to resolve this this

16 views (last 30 days)
Arshama
Arshama on 6 Feb 2025
Commented: Cris LaPierre on 23 Feb 2025 at 2:02
I have been trying to calculate the bubble velocity using the MATLAB code attached below. However, the velocity computed from the video appears to be significantly lower than expected. My goal is to compare the results with the analytical solution, but the discrepancy is quite large.
Could you please help me identify potential issues with the calculation? I suspect that there may be an error in the pixel-to-mm conversion, centroid tracking, or frame rate considerations. Any insights or suggestions for improving the accuracy of the computed velocity would be greatly appreciated.
Looking forward to your guidance.
clc;
clearvars;
% Load the video
videoFile = '0.3_video.mp4'; % Replace with actual video file name
vid = VideoReader(videoFile);
% Parameters
pixelsPerMm = 1 / 0.090; % Conversion factor (0.090 mm per pixel) - adjust as needed
timeInterval = 1 / vid.FrameRate; % Time step per frame
% Initialize variables
centroids = [];
velocities = [];
% Process each frame
while hasFrame(vid)
frame = readFrame(vid);
% Convert to grayscale if the video is in RGB
if size(frame, 3) == 3
frame = im2gray(frame);
end
% Apply thresholding (adjust threshold value if needed)
binaryFrame = imbinarize(frame, 'adaptive', 'Sensitivity', 0.5);
binaryFrame = bwareaopen(binaryFrame, 20); % Remove small noise
% Find the largest detected object (assuming it's the bubble)
stats = regionprops(binaryFrame, 'Centroid', 'Area');
if ~isempty(stats)
[~, largestIdx] = max([stats.Area]);
centroid = stats(largestIdx).Centroid;
centroids = [centroids; centroid];
else
centroids = [centroids; NaN NaN]; % If no detection, store NaN
end
end
% Compute velocity
for i = 2:size(centroids, 1)
if all(~isnan(centroids(i, :))) && all(~isnan(centroids(i-1, :)))
distancePixels = norm(centroids(i, :) - centroids(i-1, :));
distanceMm = distancePixels / pixelsPerMm; % Convert pixels to mm
velocity = distanceMm / timeInterval; % Compute velocity in mm/s
velocities = [velocities; velocity];
else
velocities = [velocities; NaN];
end
end
% Display results
disp('Computed Velocities (mm/s):');
disp(velocities);
% Plot velocity over time
figure;
plot(1:length(velocities), velocities, '-o', 'MarkerFaceColor', 'b');
xlabel('Frame Number');
ylabel('Velocity (mm/s)');
title('Bubble Velocity from Video Analysis');
grid on;
  6 Comments
Image Analyst
Image Analyst on 17 Feb 2025 at 19:32
@Arshama Your last set of code, which you said computes the velocity, does not compute the velocity. It just computes binaryFrame, not velocity. Please show the code where you compute velocity. The formula should be something like
distanceMovedInMm = distanceMovedInPixels / pixelsPerMm;
velocity = distanceMovedInMm / timeBetweenFrames;
Make sure you're computing timeBetweenFrames correctly. Also, did you ever see my answer below? Scroll down.
Cris LaPierre
Cris LaPierre on 17 Feb 2025 at 20:30
Edited: Cris LaPierre on 17 Feb 2025 at 21:55
@Image Analyst I found the code you are looking for in the original post.
% Compute velocity
for i = 2:size(centroids, 1)
if all(~isnan(centroids(i, :))) && all(~isnan(centroids(i-1, :)))
distancePixels = norm(centroids(i, :) - centroids(i-1, :));
distanceMm = distancePixels / pixelsPerMm; % Convert pixels to mm
velocity = distanceMm / timeInterval; % Compute velocity in mm/s
velocities = [velocities; velocity];
else
velocities = [velocities; NaN];
end
end

Sign in to comment.

Answers (1)

Image Analyst
Image Analyst on 11 Feb 2025
You're taking the largest blob in each frame. Display that blob all by itself to make sure that it's the same bubble blob rising and not switching to a different bubble. For example, what happens when the largest bubble rises above the top of the frame? It will switch to a different bubble with a vastly different centroid.
What alternative method are you using to determine the the image analysis method is giving wrong values? And are you sure the 1/.09 calibration factor is correct?
Have you looked at your mode frame to make sure it's just the background and there are no blobs that could be identified in it? Likewise, look at each frame after thresholding to make sure it's taking the exact bubble you think it should be.
  2 Comments
Image Analyst
Image Analyst on 18 Feb 2025 at 3:23
The video you attached cannot be read by Windows 11 because it says it uses an unsupported encoding settings. Please try again with a different zip compression program. I can't help much more until you do.
By the way, instead of doing all that to get the centroid of the largest blob and using bwareaopen to eliminate small blobs, just call bwareafilt(binaryImage, 1) to extract the largest blob only.
Until I can see the video, my guess is that you're only getting the edges of the bubble, not a complete bubble, or else you're not looking at the same bubble each time.
Cris LaPierre
Cris LaPierre on 23 Feb 2025 at 2:02
I found I could load the video in MATLAB Online, if that helps.
Windows does not natively support the High 4:4:4 Profile.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!