Calculate equation of a curve from an image?

78 ビュー (過去 30 日間)
Vrinda
Vrinda 2014 年 7 月 8 日
コメント済み: Image Analyst 2023 年 5 月 12 日
Is there a way to calculate the equation of the curve in an image like the one given below:

回答 (3 件)

Michael scheinfeild
Michael scheinfeild 2014 年 7 月 8 日
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 2014 年 7 月 12 日
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 件のコメント
Image Analyst
Image Analyst 2021 年 4 月 13 日
You just plug the x value into the spline function.
Albertus Calvin Pratama
Albertus Calvin Pratama 2021 年 4 月 14 日
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
Panos Tzovaras 2023 年 5 月 12 日
@Image Analyst Hey! Is there a way I can cite you in my PhD thesis where I am using your code?
  1 件のコメント
Image Analyst
Image Analyst 2023 年 5 月 12 日
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.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeGeometric Transformation and Image Registration についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by