please help me to resolve this this

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 Commenti

Please attach your video, or at least a sample of it, using the paperclip icon. You will need to zip it.
Also, please share the values you calculated analytically for comparison.
Arshama
Arshama il 7 Feb 2025
Modificato: Walter Roberson il 11 Feb 2025
i have attached the video as requested. i am using this code to binarized and removing background from the video:
clc;
clearvars;
% Load video file
video = VideoReader('0.7.avi'); % Replace with your actual video filename
% Parameters
frameRate = video.FrameRate; % Frames per second
% Read all frames and store in a 3D matrix
frames = [];
while hasFrame(video)
frame = im2gray(readFrame(video)); % Convert to grayscale
frames = cat(3, frames, frame); % Store each frame
end
% Calculate the mode image across all frames
modeFrame = mode(frames, 3);
% Create a VideoWriter object for the binarized video
outputVideo = VideoWriter('0.7_video.mp4', 'MPEG-4');
outputVideo.FrameRate = frameRate; % Set the frame rate
open(outputVideo);
% Process the video frame by frame
for k = 1:size(frames, 3)
% Read the current frame
currentFrame = frames(:, :, k);
% Subtract mode frame to isolate bubbles
diffFrame = imabsdiff(currentFrame, modeFrame); % Absolute difference
% Threshold to get binary image for bubbles
binaryImage = imbinarize(diffFrame,0.09);
binaryImage = imfill(binaryImage, 'holes'); % Fill holes in detected bubbles
binaryImage = bwareaopen(binaryImage, 100); % Remove small objects (noise)
% Convert binary image to uint8
binaryImageUint8 = uint8(binaryImage) * 255;
% Write the binarized frame to the output video
writeVideo(outputVideo, binaryImageUint8);
end
% Close the VideoWriter object
close(outputVideo);
disp('Binarized video of moving bubbles has been created.');
after this i used this code for calculating the velocity
clc;
clearvars;
% Load the video
videoFile = '0.3_video.mp4'; % Replace with your video file name
vid = VideoReader(videoFile);
% Pixel to mm conversion factor
pixelsPerMm = 11;
% Initialize variables
frameCount = 0;
centroids = [];
bubbleSizes = []; % To store sizes of the bubbles in pixels
se = strel('disk', 11); % Adjust disk size if needed
% Process each frame
while hasFrame(vid)
frame = readFrame(vid);
frameCount = frameCount + 1;
% Convert frame to grayscale if necessary
if size(frame, 3) == 3
frame = rgb2gray(frame);
end
% Convert frame to binary using a custom threshold
binaryFrame = imbinarize(frame, 0.3); % Adjust threshold if needed
% Perform morphological operations to clean the binary image
binaryFrame = imopen(binaryFrame, se); % Smooth edges
binaryFrame = bwareaopen(binaryFrame,50); % Remove small objects (noise)
end
imshow(binaryFrame)
this code showing the or calculating the velcity is 16 but my analytical velocity is in between 150 to 170 which is the huge difference . please resolve this as soon as possible . i would be highly thankfull to you. i am also attaching the analytical solution sheet as a screenshot
VLC for Mac is able to play the video (which is heavily watermarked)
DivX Player for Mac requires turning off hardware conversion, and then playes the video completely scrambled.
The conversion from pixels to mm seems to be the most important value in your calculation, but you haven't shared the real-world dimensions of your video, but based on the conversion factor you used, it should be
963*0.09
ans = 86.6700
Does that sound right?
@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.
@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

Accedi per commentare.

Risposte (1)

Image Analyst
Image Analyst il 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 Commenti

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.
I found I could load the video in MATLAB Online, if that helps.
Windows does not natively support the High 4:4:4 Profile.

Accedi per commentare.

Categorie

Scopri di più su MATLAB Support Package for USB Webcams in Centro assistenza e File Exchange

Richiesto:

il 6 Feb 2025

Commentato:

il 23 Feb 2025

Community Treasure Hunt

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

Start Hunting!

Translated by