Using output of the fit function in further calculations

13 次查看(过去 30 天)
Hi,
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
plot(F);
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 个)

Mathieu NOE
Mathieu NOE 2023-11-27
hello
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 (mathworks.com)
you can change the amount of smoothing if you want
backGround = im2double(imread('E3W.JPG'));
backGround2 = smoothn(backGround);
backGround3 = backGround- backGround2;
figure(1),
subplot(1,3,1),imagesc(backGround);
subplot(1,3,2),imagesc(backGround2);
subplot(1,3,3),imagesc(backGround3);

类别

Help CenterFile Exchange 中查找有关 Interpolation 的更多信息

产品


版本

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by