Azzera filtri
Azzera filtri

Trying to output coordinates from a CSV file and plot a CoG

6 visualizzazioni (ultimi 30 giorni)
Hi, I am trying to plot the CoG of 3 body landmarks stored in coordinates within a CSV file. However before I can plot the CoG, I need to import the CSV coordinates successfully for which I am struggling to acheive. Please see the attached matlab file and respond with any advice. Thanks. I am running Matlab on windows 11.
  12 Commenti
Mathieu NOE
Mathieu NOE il 9 Gen 2023
hello
seems to me you have another post dedicated to this video processing issue (is not my expertise area I apologize)
maybe Image Analyst is again keen to help you on that topic
David Gill
David Gill il 9 Gen 2023
@Image Analyst With Mathieu's help, I have written a function to plot the CoG for the 3 landmarks on each frame and ouput a scatter graph for the average CoG position. Do you know how I could get the code to plot the CoG for each frame and then complile these CoG-labelled frames in an outputted Mp4 (with identical resolution and frame rate to the input video). I have attached the script I have been working on. The issue with this code is processing time, it pops up the scatter graph for each frame (I have over 600 frames); I just need the outputted Mp4, not the scatter graphs for each frame. any thoughts? Thanks in advance.

Accedi per commentare.

Risposta accettata

Image Analyst
Image Analyst il 9 Gen 2023
@David Gill try this:
% Demo to track and plot the weighted center of gravity of a video and create an output video.
% Demo by Image Analyst.
clc; % Clear the command window.
fprintf('Beginning to run %s.m ...\n', mfilename);
close all; % Close all figures (except those of imtool.)
clearvars;
workspace; % Make sure the workspace panel is showing.
format short g;
format compact;
fontSize = 14;
% Set the name and path of the input video file
inputVideoFileName = "G:\Whisker Tracking\DLC\3_landmarks_for_whiskers-David Gill-2022-12-13\object_cropDLC_resnet50_3_landmarks_for_whiskersDec13shuffle1_1030000_labeled.mp4";
% Set the name and path of the output video file
outputVideoFileName = 'G:\Whisker Tracking\DLC\outputCoG.mp4';
% Or, if you don't have your own video, use the sample video that ships with MATLAB.
% inputVideoFileName = 'rhinos.avi';
% outputVideoFileName = 'rhinos 2.avi';
% Read the input video file
inputVideo = VideoReader(inputVideoFileName)
% Create the output video file
outputVideo = VideoWriter(outputVideoFileName);
open(outputVideo);
% Define the region of interest to be the whole image.
mask = true(inputVideo.Height, inputVideo.Width);
% Extract the frames from the input video
hFig = figure;
frameCounter = 0;
while hasFrame(inputVideo)
thisFrame = readFrame(inputVideo);
frameCounter = frameCounter + 1;
imshow(thisFrame);
if size(thisFrame, 3) > 1
grayImage = rgb2gray(thisFrame);
else
grayImage = thisFrame;
end
% Get Weighted Centroid.
props = regionprops(mask, grayImage, 'WeightedCentroid');
xCentroid(frameCounter) = props.WeightedCentroid(1);
yCentroid(frameCounter) = props.WeightedCentroid(2);
% Plot the body landmarks and CoG on the frame
hold on;
scatter(xCentroid(frameCounter), yCentroid(frameCounter), 30, 'red', 'filled', 'o');
text(xCentroid(frameCounter), yCentroid(frameCounter), ...
' COG', 'VerticalAlignment', 'middle', 'Color', 'r', ...
'FontSize', fontSize, 'FontWeight','bold');
caption = sprintf('Frame #%d', frameCounter);
title(caption, 'FontSize', fontSize)
hold off;
drawnow; % Force immediate update
% Burn graphics into an output image. Requires the Computer Vision Toolbox.
outputFrame = insertMarker(thisFrame, [xCentroid(frameCounter), yCentroid(frameCounter)] ,'+','Color', 'r','size',30);
% Write the frame to the output video
writeVideo(outputVideo, outputFrame);
end
% Close the input video file
% close(inputVideo);
% Close the output video file
close(outputVideo);
% Close that figure and bring up another one where we can show the tracked centroid.
close(hFig);
hFig = figure;
subplot(2, 1, 1);
imshow(thisFrame);
hold on;
plot(xCentroid, yCentroid, 'r-', 'LineWidth', 2);
fontSize = 20
title('Path of Center of Gravity in Red', 'FontSize', fontSize)
% Plot centroid in next axes.
subplot(2, 1, 2);
plot(xCentroid, 'r-', 'LineWidth', 2);
hold on;
plot(yCentroid, 'g-', 'LineWidth', 2);
grid on;
legend('x centroid column', 'y centroid row', 'Location', 'west')
xlabel('Frame Number', 'FontSize', fontSize)
ylabel('Column (for x) or Row (for y)', 'FontSize', fontSize)
title('Weighted Center of Gravity (Weighted Centroid)', 'FontSize', fontSize)
% Maximize the figure.
hFig.WindowState = "maximized";
% Open the output video
if ispc
winopen(outputVideoFileName);
end
message = sprintf('Done processing %d frame.', frameCounter);
uiwait(helpdlg(message))
  11 Commenti
David Gill
David Gill il 14 Gen 2023
in what way can you tell x1 looks suspicious?

Accedi per commentare.

Più risposte (0)

Community Treasure Hunt

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

Start Hunting!

Translated by