MATLAB Answers

Peak Identification in Image

2 views (last 30 days)
Kavya Sudhir
Kavya Sudhir on 1 Jul 2020
Commented: Kavya Sudhir on 1 Jul 2020
Hi! I am trying to determine the peak coordinates of just a plain image using matlab in order to determine the length of the peak. I have attached a picture of a sample of what im trying to analyze. Would love to get some ideas on the best way to approach asap! Thank you in advance.

  0 Comments

Sign in to comment.

Answers (1)

Image Analyst
Image Analyst on 1 Jul 2020
What's your definition of peak here? And "length of peak"? Have you tried thresholding and scanning across columns to see what line the blue line is at?
% By Image Analyst
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 22;
%--------------------------------------------------------------------------------------------------------
% READ IN IMAGE
folder = pwd;
baseFileName = 'image.png';
% Get the full filename, with path prepended.
fullFileName = fullfile(folder, baseFileName);
% Check if file exists.
if ~exist(fullFileName, 'file')
% The file doesn't exist -- didn't find it there in that folder.
% Check the entire search path (other folders) for the file by stripping off the folder.
fullFileNameOnSearchPath = baseFileName; % No path this time.
if ~exist(fullFileNameOnSearchPath, 'file')
% Still didn't find it. Alert user.
errorMessage = sprintf('Error: %s does not exist in the search path folders.', fullFileName);
uiwait(warndlg(errorMessage));
return;
end
end
grayImage = imread(fullFileName);
% Get the dimensions of the image.
% numberOfColorChannels should be = 1 for a gray scale image, and 3 for an RGB color image.
[rows, columns, numberOfColorChannels] = size(grayImage);
if numberOfColorChannels > 1
% It's not really gray scale like we expected - it's color.
% Use weighted sum of ALL channels to create a gray scale image.
% grayImage = rgb2gray(grayImage);
% ALTERNATE METHOD: Convert it to gray scale by taking only the green channel,
% which in a typical snapshot will be the least noisy channel.
grayImage = grayImage(:, :, 1); % Take red channel.
end
% Crop it to the right lines.
% grayImage = grayImage(1750:2250, 800:end);
subplot(2, 2, 1);
imshow(grayImage, []);
impixelinfo;
title('Red Channel Image', 'FontSize', fontSize, 'Interpreter', 'None');
hFig = gcf;
hFig.WindowState = 'maximized'; % May not work in earlier versions of MATLAB.
drawnow;
subplot(2, 2, 2);
imhist(grayImage);
grid on;
%--------------------------------------------------------------------------------------------------------
% SEGMENTATION OF IMAGE
% Get a binary image by interactively thresholding using the function at
% https://www.mathworks.com/matlabcentral/fileexchange/29372-thresholding-an-image
% 156 seems good for this image.
lowThreshold = 0;
highThreshold = 130;
% [lowThreshold, highThreshold] = threshold(lowThreshold, highThreshold, grayImage);
mask = grayImage > lowThreshold & grayImage < highThreshold;
se = strel('disk', 1, 0);
mask = imclose(mask, se);
subplot(2, 2, 3);
imshow(mask, []);
impixelinfo;
title('Binary Image', 'FontSize', fontSize, 'Interpreter', 'None');
hFig = gcf;
hFig.WindowState = 'maximized'; % May not work in earlier versions of MATLAB.
drawnow;
[rows, columns] = size(mask);
topRows = zeros(1, columns);
for col = 1 : columns
t = find(mask(:, col), 1, 'first');
if ~isempty(t)
topRows(col) = t;
end
end
subplot(2, 2, 4);
plot(topRows, 'b-', 'LineWidth', 2);
grid on;
Obviously you should be doing some background leveling. And then because your line is broken, you'll need to do some outlier removal and correction. But I still have no idea what you mean by peak length.

  1 Comment

Kavya Sudhir
Kavya Sudhir on 1 Jul 2020
I am trying to assign a reference line where the line starts (x-axis) and then identify the maximum y-value of the curve given, I am having trouble mapping coordinates of the curve in this image.

Sign in to comment.


Translated by