If I want to choose one cycle. The cycle must have criteria : largest distance maximum & minimum, 3 point cross X axis. Thank you very much in advanced.
MATLAB: Find max & min
MATLABmax and min
Related Solutions
xcorr2 is not the best for finding the object unless you know the exact peak auto correlation value. A much more robust method is to use normxcorr2() where you simply have to look for the max. (If you look for the peak using xcorr2(), it will not necessarily lie where your template occurs in the image, it could be over any old bright part of the image). Here is a demo using normxcorr2:
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
imtool close all; % Close all imtool figures.
clear; % Erase all existing variables (which I know Jan hates).
workspace; % Make sure the workspace panel is showing.
format longg;format compact;fontSize = 20;% Check that user has the Image Processing Toolbox installed.
hasIPT = license('test', 'image_toolbox');if ~hasIPT % User does not have the toolbox installed.
message = sprintf('Sorry, but you do not seem to have the Image Processing Toolbox.\nDo you want to try to continue anyway?'); reply = questdlg(message, 'Toolbox missing', 'Yes', 'No', 'Yes'); if strcmpi(reply, 'No') % User said No, so exit.
return; endend% Read in a standard MATLAB color demo image.
folder = fullfile(matlabroot, '\toolbox\images\imdemos');baseFileName = 'peppers.png';% Get the full filename, with path prepended.
fullFileName = fullfile(folder, baseFileName);if ~exist(fullFileName, 'file') % Didn't find it there. Check the search path for it.
fullFileName = baseFileName; % No path this time.
if ~exist(fullFileName, 'file') % Still didn't find it. Alert user.
errorMessage = sprintf('Error: %s does not exist.', fullFileName); uiwait(warndlg(errorMessage)); return; endendrgbImage = imread(fullFileName);% Get the dimensions of the image. numberOfColorBands should be = 3.
[rows columns numberOfColorBands] = size(rgbImage);% Display the original color image.
subplot(2, 2, 1);imshow(rgbImage, []);axis on;title('Original Color Image', 'FontSize', fontSize);% Enlarge figure to full screen.
set(gcf, 'units','normalized','outerposition',[0 0 1 1]);smallSubImage = imcrop(rgbImage, [192 82 60 52]);subplot(2, 2, 2);imshow(smallSubImage, []);axis on;title('Template Image to Search For', 'FontSize', fontSize);% Search the red channel for a match.
correlationOutput = normxcorr2(smallSubImage(:,:,1), rgbImage(:,:,1));subplot(2, 2, 3);imshow(correlationOutput, []);title('Correlation Output', 'FontSize', fontSize);[maxCorrValue, maxIndex] = max(abs(correlationOutput(:)));[ypeak, xpeak] = ind2sub(size(correlationOutput),maxIndex(1));corr_offset = [(xpeak-size(smallSubImage,2)) (ypeak-size(smallSubImage,1))];subplot(2, 2, 4);imshow(rgbImage);hold on;rectangle('position',[corr_offset(1) corr_offset(2) 50 50],... 'edgecolor','g','linewidth',2);title('Template Image Found in Original Image', 'FontSize', fontSize);
Try this:
clc; % Clear the command window.
clear all;close all;workspace; % Make sure the workspace panel is showing.
format short g;format compact;fontSize = 22;fprintf('Beginning to run %s.m ...\n', mfilename);%--------------------------------------------------------------------------------------------------
% Load data from mat file.
s = load('data_compact.mat')data_compact = s.data;x = data_compact(:,1);% Plot data.
plot(x, data_compact(:,2), 'b-');xlim([x(1), x(end)]);grid on;hold on;%--------------------------------------------------------------------------------------------------% Smooth with a savitzky-golay filter. Polynomial order = 2, window width = 351 elements.
smoothY = sgolayfilt(data_compact(:, 2), 2, 351);plot(x, smoothY, 'r-');%--------------------------------------------------------------------------------------------------% Find peaks. Must be separated by 13000 elements.
[peakValues, indexesOfPeaks, widths, proms] = findpeaks(smoothY, 'MinPeakDistance',13000); % peaks
% Remove an occasional outlier that is below the midpoint.
meanSignal = mean(smoothY);outlierIndexes = peakValues < meanSignal;peakValues(outlierIndexes) = [];indexesOfPeaks(outlierIndexes) = [];% Plot peaks.
plot(x(indexesOfPeaks), peakValues, 'g.', 'MarkerSize', 30);%--------------------------------------------------------------------------------------------------% Find Valleys. Must be separated by 13000 elements.
[valleyValues, indexesOfValleys] = findpeaks(-smoothY, 'MinPeakDistance', 13000); % valleys
valleyValues = -valleyValues; % Make upright again.
% Remove an occasional outlier that is above the midpoint.
outlierIndexes = valleyValues > meanSignal;valleyValues(outlierIndexes) = [];indexesOfValleys(outlierIndexes) = [];% Plot valleys.
plot(x(indexesOfValleys), valleyValues, 'c.', 'MarkerSize', 30);message = sprintf('Found %d peaks, and %d valleys', length(indexesOfPeaks), length(indexesOfValleys));title(message, 'FontSize', fontSize);% Maximize the figure window.
g = gcf;g.WindowState = 'maximized'fprintf('%s\n', message);uiwait(helpdlg(message));fprintf('Done running %s.m ...\n', mfilename);
Best Answer