How to obtain piecewise polynomial from the acquired piecewise data?

I am using the following code to obtain the piecewise polynomial :
clc
close all
clear all
%Step 1 : Read Image
X=imread('cameramannn.tif');
figure,imshow(X);
[r c noc]=size(X);
% Step 2 : Gray Scale Conveersion
if noc > 1
Xn=rgb2gray(X);
else
Xn=X;
end
% Step 3 : Extracting Histogram from image
figure,imhist(Xn);
[counts, centers] = imhist(Xn);
bar(centers, counts, 'hist');
histpatch = findobj(gca, 'type', 'patch');
edgemat = get(histpatch, 'XData');
countmat = get(histpatch, 'YData');
counts = countmat(2,:);
centers = mean(edgemat(2,3,:))
% Step 4 : Extracting break points for polynomial
a=1;
while a<=4
for b=1:r*c
y=edgemat(a,b);
%round(y);
disp(y);
end
a=a+1;
end
breaks=[1.5000 2.5000 3.5000 4.5000 5.5000 6.5000 7.5000 8.5000 9.5000 10.5000 11.5000 12.5000 13.5000 14.5000 15.5000 16.5000 17.5000]; % manually taken from step 4
% Step 5 : Acquiring Piecewise Polynomial from the extracted data
pp=mkpp(breaks,Xn);
coefs = pp.coefs ;
breaks = pp.breaks ;
peices = pp.pieces ;
order = pp.order ;
dim = pp.dim ;
disp(pp);
% Step 6 : Obtaining Piecewise Polynomial from the acquired data
a=polyval(breaks,pp);
My Piecewise Output is :
form: 'pp'
breaks: [1x17 double]
coefs: [16x16 uint8]
pieces: 16
order: 16
dim: 1
But when MATLAB is compiling the polyval() I get the following error :
Undefined function 'isfinite' for input arguments of type 'struct'.
Error in polyval (line 54)
if isscalar(x) && (nargin < 3) && nc>0 && isfinite(x) && all(isfinite(p(:)))
Error in extracting_polynomial_try0 (line 50)
a=polyval(breaks,pp);
Please help me in resolving this issue.

Answers (1)

The issue you are facing is due to the misuse of the "polyval" function. The "polyval" function is meant to evaluate a polynomial at specific points, but it expects the coefficients of a single polynomial as its first argument, not a piecewise polynomial structure like "pp" from "mkpp".
  • The "mkpp" function returns a structure containing fields like "breaks", "coefs", "pieces", "order", and "dim". Try avoiding to pass this structure directly to "polyval".
  • Use "ppval" to evaluate the piecewise polynomial created by "mkpp". This function is designed to handle the "pp" structure.
Revised version of the code is given below:
% Step 1 : Read Image
% Step 2 : Gray Scale Conversion
% Step 3 : Extracting Histogram from image
% Step 4 : Extracting break points for polynomial
% Define manual breakpoints (as provided)
% same as given code
% Step 5 : Acquiring Piecewise Polynomial from the extracted data
% Since Xn is an image matrix, you need to define meaningful coefficients
% For demonstration, let's assume a simple linear fit for each segment
coeffs = repmat([0, 1], length(breaks)-1, 1); % Example: linear pieces
% Create the piecewise polynomial
pp = mkpp(breaks, coeffs);
% Step 6 : Evaluate the Piecewise Polynomial
x = linspace(min(breaks), max(breaks), 100); % Define points to evaluate
y = ppval(pp, x); % Evaluate the piecewise polynomial
% Plot the piecewise polynomial
figure;
plot(x, y, 'r', 'LineWidth', 2);
title('Piecewise Polynomial');
xlabel('Intensity Value');
ylabel('Polynomial Output');
grid on;
For better understanding of "mkpp" and "polyval" function of MATLAB refer to the following documentation:
Hope this helps!

Categories

Asked:

on 13 May 2019

Answered:

on 26 Nov 2024

Community Treasure Hunt

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

Start Hunting!