Calculate equation of a curve from an image?
Mostra commenti meno recenti
Is there a way to calculate the equation of the curve in an image like the one given below:

Risposte (3)
Michael scheinfeild
il 8 Lug 2014
if all image is zeros and the line value>0. use
ii=find(image(:));
after you find indexes convert it to x,y
[yy,xx]=ind2sub(size(image),ii);
the use command like polyfit or other fit
n=2 , or n=3
p = polyfit(xx,yy,n)
it will give you coeficients !!
Image Analyst
il 12 Lug 2014
Try this code I wrote for you. Simply copy and paste, and change the folder name to wherever you stored your image.

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.
workspace; % Make sure the workspace panel is showing.
format longg;
format compact;
fontSize = 20;
%===============================================================================
% Read in a Vrinda's gray scale demo image.
folder = 'C:\Users\Vrinda\Documents';
baseFileName = 'curve1.jpg';
% Get the full filename, with path prepended.
fullFileName = fullfile(folder, baseFileName);
% Check if file exists.
if ~exist(fullFileName, 'file')
% File doesn't exist -- didn't find it there. Check the search path for it.
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.
% numberOfColorBands should be = 1.
[rows, columns, numberOfColorBands] = size(grayImage);
if numberOfColorBands > 1
% It's not really gray scale like we expected - it's color.
% Convert it to gray scale by taking only the green channel.
grayImage = grayImage(:, :, 2); % Take green channel.
end
% Display the original gray scale image.
subplot(2, 2, 1);
imshow(grayImage, []);
title('Original Grayscale Image', 'FontSize', fontSize);
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0 0 1 1]);
% Give a name to the title bar.
set(gcf, 'Name', 'Demo by ImageAnalyst', 'NumberTitle', 'Off')
% Binarize to get rid of horrible jpeg noise
binaryImage = grayImage < 100;
% Display the original gray scale image.
subplot(2, 2, 2);
imshow(binaryImage, []);
title('Binary Image', 'FontSize', fontSize);
axis on;
% Get the rows (y) and columns (x).
[rows, columns] = find(binaryImage);
% Fit a cubic
coefficients = polyfit(columns, rows, 3); % Gets coefficients of the formula.
% Fit a curve to 500 points in the range that x has.
fittedX = linspace(min(columns), max(columns), 500);
% Now get the y values.
fittedY = polyval(coefficients, fittedX);
% Plot the fitting:
subplot(2,2,3:4);
plot(fittedX, fittedY, 'b-', 'linewidth', 4);
grid on;
xlabel('X', 'FontSize', fontSize);
ylabel('Y', 'FontSize', fontSize);
% Overlay the original points in red.
hold on;
plot(columns, rows, 'r+', 'LineWidth', 2, 'MarkerSize', 10);
11 Commenti
Image Analyst
il 12 Lug 2014
Note that the y direction of images and line plots go in different directions. If that is a problem, then let me know what you want to do in more detail.
Ziv Kassner
il 15 Gen 2015
This script is truly helpful! How can I use it if my line is in a shape of a half ellipse? it means that in a few points, X will have 2 Y values, Sincerely, Ziv.
Image Analyst
il 15 Gen 2015
Ziv, fitting an ellipse is not so easy. The FAQ has a link to a paper that shows you how to do it: http://matlab.wikia.com/wiki/FAQ#How_can_I_fit_an_ellipse_or_other_shape_to_a_set_of_XY_data.3F
Hi image analyst. I'm doing something similar to Vrinda except in my image I want to iterate through each column and identify the pixel of highest intensity and the plot this curve. Any help would be greatly appreciated.
Image Analyst
il 27 Mar 2016
Nick, I don't think that would get you what you want, which I think might be a line along the centerline of the bright path. Why don't you start your own question? If you do, it would be something like
for col = 1 : columns
[maxValue(col), row(col)] = max(grayImage(:, col));
end
nick.x101
il 27 Mar 2016
Ok I will try this. I also looked at your Image Segmentation tutorial to try and obtain the line along the centerline but it didn't work too well. It gave the 'blobs' of highest intensity and I tried to find a way around it by changing the property of regionprops but it didn't work to well. Also I will post it as a new question. Thanks Image Analyst.
jon tan
il 1 Nov 2020
Hi image analyst, I am doing something similar but I am not sure how to go about to do this. Basically I have collected raw data in my experiment to plot a displacement time graph and I need to plot the gradient for it which is essentially the velocity time graph as well as its derivative for the acceleration time graph. However, I did not collect sufficient data points to be able to use (y1-y2)/(x1-x2) formula into my points to generate the gradient. Is there anyway that the code you wrote above can be applied to my problem? Thank you
Image Analyst
il 1 Nov 2020
You can generate more points with spline. See attached demos. Start a new question with your data attached if you need more help.
Albertus Calvin Pratama
il 13 Apr 2021
I wondering how if we want to know the Y value by inputting X in command window or script based on the graph?
Image Analyst
il 13 Apr 2021
You just plug the x value into the spline function.
Albertus Calvin Pratama
il 14 Apr 2021
Is it possible to do if i don''t know the spline function and get the Y value if we input X value? or this code can give us the spline function either?
Panos Tzovaras
il 12 Mag 2023
0 voti
@Image Analyst Hey! Is there a way I can cite you in my PhD thesis where I am using your code?
1 Commento
Image Analyst
il 12 Mag 2023
I imagine it would be similar to how it suggests you cite File Exchange items
Cite As
Image Analyst (2023). Image Segmentation Tutorial (https://www.mathworks.com/matlabcentral/fileexchange/25157-image-segmentation-tutorial), MATLAB Central File Exchange. Retrieved May 12, 2023.
Make changes as appropriate for code on Answers pages.
Categorie
Scopri di più su Blue in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!