Using output of the fit function in further calculations

조회 수: 9 (최근 30일)
Hannah 2023년 11월 27일
댓글: Hannah 2023년 11월 28일
I am trying to correct for the change in intensity of an image due to the lighting position. I thought that if I found the polynomial of a fitted surface I could use that to correct the image. I have used the fit function to fit a surface and have managed to generate a polynomial along with the variable values. My problem is that I can't figure out how to calculate val(x,y) for each pixel in the image using the generated polynomial and variables (which I then plan to subtract from my original image). How can I get the formula and variables back into usable code, please?
Many thanks.
Here is the code that I have been using:
backGround = im2double(imread('E3W.JPG'));
subset = backGround(1:50:3000,1:50:4096);
B = reshape(subset,[],1);
si = size(subset);
[x,y] = ndgrid(1:si(2),1:si(1));
x = reshape(x,[],1);
y = reshape(y,[],1);
sf = fit([x, y],B,'poly33');
coefficientNames = coeffnames(sf);
coefficientValues = coeffvalues(sf);
curve = formula(sf);
curve_string = convertCharsToStrings(curve);
curve_symbol = str2sym(curve_string);
curve_usable = str2func(curve_string);

채택된 답변

Steven Lord
Steven Lord 2023년 11월 27일
If all you want to do is evaluate the surface fit there's no need to break it into its components parts and re-assemble it. I'll show an example with a curve fit, but the principle is the same for surfaces.
load census
% Create fit using data from census.mat
F = fit(cdate, pop, 'poly2', 'Normalize', 'on')
F =
Linear model Poly2: F(x) = p1*x^2 + p2*x + p3 where x is normalized by mean 1890 and std 62.05 Coefficients (with 95% confidence bounds): p1 = 25.18 (23.58, 26.79) p2 = 75.43 (74.04, 76.83) p3 = 61.74 (59.7, 63.79)
% Evaluate fit for year 1955
p1955 = F(1955)
p1955 = 168.4030
% Plot original data, fit, and point for year 1955
hold on
plot(cdate, pop, 'ko', 1955, p1955, 'bx')
I'd say that fitted curve fits the data reasonably well and that the evaluated point is on the fitted curve.
  댓글 수: 1
Hannah 2023년 11월 28일
Thanks for your help and clear explanation, that is what I needed

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

추가 답변 (1개)

Mathieu NOE
Mathieu NOE 2023년 11월 27일
why not use the smoothed (low pass filtered) image as a new background and you remove that from the original image. In fact this is the same as applying a 2D high pass filter
for the smoothed image I used smoothn available here : smoothn - File Exchange - MATLAB Central (
you can change the amount of smoothing if you want
backGround = im2double(imread('E3W.JPG'));
backGround2 = smoothn(backGround);
backGround3 = backGround- backGround2;


Help CenterFile Exchange에서 Get Started with Curve Fitting Toolbox에 대해 자세히 알아보기




Community Treasure Hunt

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

Start Hunting!

Translated by