forked from alphanumericslab/ecg-image-kit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_ecg_sequence_extraction.m
73 lines (60 loc) · 2.83 KB
/
test_ecg_sequence_extraction.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
% Test script for image_to_sequence to convert ECG images into time-series
%
% This script reads ECG images and applies different methods provided by
% the image_to_sequence function to convert these images into time-series
% data. It processes each image using various sequence extraction methods
% and then visualizes the results for comparison.
%
% Reference:
% Reza Sameni, 2023, ECG-Image-Kit: A toolkit for ECG image analysis.
% Available at: https://github.com/alphanumericslab/ecg-image-kit
%
% Revision History:
% 2023: First release
clear
close all FORCE
clc
% Define the path to the folder containing ECG image segments
data_path = '../../sample-data/ecg-images/sample-segments/';
% Get a list of all files in the image folder
all_files = dir(fullfile(data_path, '*.*'));
% Loop over all files, reading and processing each image
for k = 1 : length(all_files)
file_name = all_files(k).name;
image_fname = fullfile(data_path, filesep, file_name);
try
% Read the image
img = imread(image_fname);
% Apply different sequence extraction methods to the image
z0 = image_to_sequence(img, 'dark-foreground', 'max_finder', [], false);
z1 = image_to_sequence(img, 'dark-foreground', 'hor_smoothing', 3);
z2 = image_to_sequence(img, 'dark-foreground', 'all_left_right_neighbors');
z3 = image_to_sequence(img, 'dark-foreground', 'combined_all_neighbors');
z4 = image_to_sequence(img, 'dark-foreground', 'moving_average', 3);
% Combine results from all methods
z_combined = median([z0 ; z1 ; z2 ; z3 ; z4], 1);
% Prepare for plotting
lgnd = {};
nn = 1 : size(img, 2);
img_height = size(img, 1);
% Display the original image and overlay the extracted sequences
figure
imshow(img)
hold on
plot(nn, img_height - z0, 'linewidth', 3); lgnd = cat(2, lgnd, {'max_finder'});
plot(nn, img_height - z1, 'linewidth', 3); lgnd = cat(2, lgnd, {'hor_smoothing'});
plot(nn, img_height - z2, 'linewidth', 3); lgnd = cat(2, lgnd, {'all_left_right_neighbors'});
plot(nn, img_height - z3, 'linewidth', 3); lgnd = cat(2, lgnd, {'combined_all_neighbors'});
plot(nn, img_height - z4, 'linewidth', 3); lgnd = cat(2, lgnd, {'moving_average'});
plot(nn, img_height - z_combined, 'linewidth', 3); lgnd = cat(2, lgnd, {'combined methods'});
% Add legend and title
legend(lgnd, 'interpreter', 'none');
title(['Paper ECG vs recovered signal for: ', file_name]);
set(gca, 'fontsize', 14)
% Uncomment the following line to save the superposed image, if needed
% saveas(gcf, [fname(1:end-4), '-rec.png']);
close all FORCE
catch
warning(['File ', file_name, ' not an image, or processing failed'])
end
end