# How to compare multiple polynomials in MATLAB?

I am trying to fit a second order polynomial to various curves on a binary image using MATLAB polyfit function.There are many such curves on the image, and the coordinates of the pixels forming the curve are known.

How do I compare the co-efficients, so as to arrive at one approximation to all such curves?

Here are some co-efficients of such curves:

 0.0010126     -1.5981       1074.8
0.0024871     -3.3327       1866.3
0.002662      -3.7067       1631.4
0.0025574     -4.5129       2148.4
0.002114      -3.3457       2195.6
0.0015383     -2.6023       1682.9

• Can you use polyfit on a all the data at once? – Pier-Yves Lessard Oct 21 '18 at 2:53
• No. These curves are the edges of the ribs on a chest x ray. They are slightly different from each other, but have a characteristic similarity. – Prashant Oct 21 '18 at 2:56
• Could you share a sample of data? What is the difference between the curves. You ask how to compare them, but according to what criteria?A little context would help a lot here. – Pier-Yves Lessard Oct 21 '18 at 2:56
• I want to detect the rib edges on a chest X ray. Each of the rib edges are different and are not a continuous lines. Some are only with fragments making up the whole rib edge. It is possible to fit polynomial to each segment of the edge. But I want to study on a possibility of a general or approximated polynomial applicable to many if not all of such curves. I have added an image for ref – Prashant Oct 21 '18 at 3:05
• To find a single polynomial, you could generate a new point cloud that would mix all of your polynomial, limiting them to a x that has a meaning (short rib generates less points than long ribs, kind of..), then rerun polyfit ont hat new cloud. Or, you could fit a curve to find the polynomial coefficient. For example, you gave 6 polynomials representing a rib each. You could make 3 polynomials (a0,a1,a2) that varies relatives to the ribs position. Actually, I tried to plot your coefficients (taken vertically) and they somewhat looks like a parabola, maybe the second approach may work. – Pier-Yves Lessard Oct 21 '18 at 3:10

## 1 Answer

Based on what you share, this is the best answer I can give. I see two methods:

Method #1,

Instead of making N polynomials (if N is the number of ribs), you could fit a curve for all the ribs at once. Of course, for that, you will need to have a common (0,0) coordinate, in other word, stack them one over the other on your image. Since you are able to make a distinction between the ribs, that means that you can already isolate them, so that shouldn't be an issue.

Or if you prefer, you can generate a new cloud of points with the polynomials you already found.

coeffs = [0.0010126, -1.5981, 1074.8; 0.0024871, -3.3327, 1866.3];
x = linspace(0,1000,1000); % fictives points
merged_x = [x x];
merged_y = [ polyval(coeffs(1,:), x), polyval(coeffs(2,:),x)];
coeffs_final = polyfit(merged_x, merged_y, 2);
figure
hold on
plot(polyval(coeffs(1,:), x), 'red')
plot(polyval(coeffs(2,:), x), 'blue')
plot(polyval(coeffs_final, x), 'black')


Method #2

You could fit 3 polynomials to find the values of the coefficient you already have found. If we take the first polynomial you provided, then $$a_2=0.0010126$$ $$a_1=-1.5981$$ $$a_0=1074.8$$

You could fit a curve that would follow all $$a_2$$, another for all $$a_1$$, and same for all $$a_0$$. This would yield 9 new coefficients (3 polynomials) that will allow you to find your polynomials based on the rib position.

Here's an example

coeffs = [ 0.0010126     -1.5981       1074.8;
0.0024871     -3.3327       1866.3;
0.002662      -3.7067       1631.4;
0.0025574     -4.5129       2148.4;
0.002114      -3.3457       2195.6;
0.0015383     -2.6023       1682.9 ];

x = linspace(0,1000,1000); %fictive value
ribs_count = size(coeffs, 1);
a2 = polyfit(1:ribs_count, coeffs(:,1)', 2); % careful to the ' symbol that transpose the column into a row
a1 = polyfit(1:ribs_count, coeffs(:,2)', 2);
a0 = polyfit(1:ribs_count, coeffs(:,3)', 2);

figure
for rib=1:ribs_count
subplot(6,1,rib);
hold on
new_coeffs = [polyval(a2,rib), polyval(a1,rib), polyval(a0,rib)];
plot(x, polyval(new_coeffs, x), 'red')
plot(x, polyval(coeffs(rib,:), x), 'blue')
end