MATLAB从第二个x轴获得峰值

我目前正在尝试基于心电图时间和3个界标来叠加DICOM回波和CT图像。心电图线在回波图像中,我根据颜色从回波图像中提取了它,然后将其转移到绘图中。
因为绘图是基于colorMask的,y和x值设置为y=ecgTrace和x=timeVector,所以我希望x轴显示dicom图像的总持续时间,以便能够获得每个心电波形的正确帧。我可以隐藏“原始”x轴,并将其替换为totalDuration,但我无法获得基于x轴2的峰值。
这就是我获取值并绘制心电图线的方法:

% 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');

我试过这个,但都一团糟。。。

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');
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值