MATLAB Answers

Calculate equation of a curve from an image?

조회 수: 236(최근 30일)
Vrinda 8 Jul 2014
댓글: Albertus Calvin Pratama 14 Apr 2021 8:24
Is there a way to calculate the equation of the curve in an image like the one given below:


Michael scheinfeild
Michael scheinfeild 8 Jul 2014
if all image is zeros and the line value>0. use
after you find indexes convert it to x,y
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 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);
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.
% 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:
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
Albertus Calvin Pratama
Albertus Calvin Pratama 14 Apr 2021 8:24
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?

댓글을 달려면 로그인하십시오.


Community Treasure Hunt

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

Start Hunting!

Translated by