Calculate equation of a curve from an image?

85 views (last 30 days)
Vrinda
Vrinda on 8 Jul 2014
Commented: Image Analyst on 12 May 2023
Is there a way to calculate the equation of the curve in an image like the one given below:

Answers (3)

Michael scheinfeild
Michael scheinfeild on 8 Jul 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
Image Analyst on 12 Jul 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 Comments
Image Analyst
Image Analyst on 13 Apr 2021
You just plug the x value into the spline function.
Albertus Calvin Pratama
Albertus Calvin Pratama on 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?

Sign in to comment.


Panos Tzovaras
Panos Tzovaras on 12 May 2023
@Image Analyst Hey! Is there a way I can cite you in my PhD thesis where I am using your code?
  1 Comment
Image Analyst
Image Analyst on 12 May 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.

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!