Calculate equation of a curve from an image?
조회 수: 60 (최근 30일)
이전 댓글 표시
Is there a way to calculate the equation of the curve in an image like the one given below:
댓글 수: 0
답변 (3개)
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 !!
댓글 수: 0
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
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
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
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 Exchange에서 Image Processing Toolbox에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!