AMHM
AMHM

Reputation: 1

MATLAB get peak values from second x axis

I´m currently trying on overlaying DICOM echo and CT images based on ECG time and 3 landmarks. The ECG line is within the echo image and I have extracted it from the echo image based on color and it is then transferred to a plot.

Because the plot is based on a colorMask the y and x values are set as y = ecgTrace and x = timeVector, I want the x axis to show the total duration of the dicom image to be able to get the correct frame for each ecg wave. I was able to hide the "original" x axis and replace it with the totalDuration but I´m not able to get the peak values based on x axis 2.

This is how I get the values and plot the ecg Line:

colorMask = abs(double(echoImageRGB(:,:,1)) - clickedColor(1)) <= tolerance & ...
            abs(double(echoImageRGB(:,:,2)) - clickedColor(2)) <= tolerance & ...
            abs(double(echoImageRGB(:,:,3)) - clickedColor(3)) <= tolerance;

% Fill holes in the mask
colorMaskFilled = imfill(colorMask, 'holes');

% Extract the ECG trace from the mask. Since it's a binary image, we can find the top edge of the white line
% which corresponds to the ECG trace. We'll use the bottom-most white pixel in each column as the ECG signal.

% Preallocate a NaN array for the ECG trace
ecgTrace = nan(1, size(colorMaskFilled, 2));

% Loop through each column to find the ECG signal
for col = 1:size(colorMaskFilled, 2)
    % Find the rows in this column where the mask is white
    rows = find(colorMaskFilled(:, col));
    
    % If we find any white pixels, use the bottom-most one as the signal
    if ~isempty(rows)
        ecgTrace(col) = max(rows);
    end
end

% Invert the ECG trace for correct orientation
ecgTrace = size(colorMaskFilled, 1) - ecgTrace;

timeVector = linspace(0, totalDuration, length(ecgTrace));
% Normalize the ECG trace to have zero mean, if desired
ecgTrace = ecgTrace - mean(ecgTrace, 'omitnan');

figure;
ax1 = axes; % Primary axes
plot(ax1, timeVector, ecgTrace, 'b', 'LineWidth', 1.5); % Plot on primary axis
ax1.XColor = 'none'; % Make ax1 x-axis invisible
ax1.YColor = 'k';    % Keep y-axis visible if needed

% Create secondary axis
ax2 = axes('Position', ax1.Position, 'Color', 'none', 'YColor', 'k');
linkaxes([ax1, ax2], 'y'); % Link the y-axes to synchronize vertical scaling

% Set ax2 as the primary visible x-axis
set(ax2, 'XLim', [0-startTime*2, max(totalDuration)], 'XColor', 'r'); % Set limits and color for visibility
xlabel(ax2, 'Total Duration (minutes)');

% Make ax2's x-axis line up with ax1's position
ax2.XAxisLocation = 'bottom';  % Moves the x-axis of ax2 to the bottom (normal position)
ax2.YAxisLocation = 'left';    % Ensures the y-axis is on the left

% Hide ax1 completely including y-axis if not needed
ax1.Visible = 'off';  % This makes ax1 completely invisible including ticks and labels

% Ensure all plot elements render correctly
ax2.Box = 'off';  % Turn off the boxing to prevent axis lines on all sides
ax2.Color = 'none';  % Ensures the background is transparent

% Set the figure background to white
set(gcf, 'Color', 'w');

I have tried this, but it´s all just a big mess...

figure;
ax1 = axes; % Create invisible primary axes for plotting
plot(ax1, timeVector, ecgTrace, 'b', 'LineWidth', 1.5); % Plot ECG trace on invisible ax1
ax1.Visible = 'off'; % Hide ax1 completely

% Set up ax2 as the main visible axis for interaction
ax2 = axes('Position', ax1.Position, 'XAxisLocation', 'bottom', 'Color', 'none', 'YColor', 'k');
linkaxes([ax1, ax2], 'y'); % Ensure y-axes are synchronized
ax2.XLim = [0, max(timeVector)/60]; % Set x-axis limits based on total duration in minutes
xlabel(ax2, 'Total Duration (minutes)');
ylabel(ax2, 'Amplitude');
ax2.XColor = 'r'; % Make ax2 visually distinct

% Find peaks in the ECG trace
[peakAmplitudes, peakIndices] = findpeaks(ecgTrace, 'MinPeakHeight', 10);  % Adjust MinPeakHeight as needed

% Convert peak indices from the time base (seconds) to minutes
peakTimesSeconds = timeVector(peakIndices);  % Get time in seconds for each peak
peakTimesMinutes = peakTimesSeconds / 60;    % Convert these times to minutes

% Display the peak times and amplitudes for ax2
disp('Peak Times on ax2 (Total Duration in minutes):');
disp(peakTimesMinutes);
disp('Peak Amplitudes:');
disp(peakAmplitudes);

% Plot these peaks on ax2 for visualization
hold on;
plot(ax2, peakTimesMinutes, peakAmplitudes, 'rp', 'MarkerSize', 10, 'MarkerFaceColor', 'red');  % Show peaks on ax2
title('Visualization of ECG Peaks on Total Duration Axis');

Upvotes: 0

Views: 35

Answers (0)

Related Questions