How to predict each pixel of image using regression model?

I have the following code that loops over each pixel of a .tif image to predict responses using ensemble of regression models.
X is a 753*6 numeric array which has 6 variables (also columns), and 753 rows. NR = 1380, NC = 1464.
I understand the error's meaning (The dimensions on both sides do not match each other), but I really do not know how to fix it. I imagine the result I need should be a 1380*1464 numeric array.
a = imread('LE71250521999276_b1.tif')
[NR,NC] = size(a);
Yfit = zeros(NR,NC);
for i = 1:NR
for j = 1:NC
Yfit(i,j) = predict(Mdl1999276,X);
end
end
ERROR: Assignment has more non-singleton rhs dimensions than non-singleton subscripts
Thank you for helping!!

 Accepted Answer

Why are you using all of X to do the prediction each time?
Why are you reading in the image if you are not going to predict based on its values?
Ensembles often make one prediction per ensemble member per sample; if so then you might need to analyze a vector of results to decide what one output you want.
Predictions sometimes output a probability per class rather than a single class number.

5 Comments

Plus, it's risky to do
[NR,NC] = size(a);
with an image. The (badly-named) a might be color rather than gray scale, in which case NC will be 3 times the number of columns. It's much better to do
[rows, columns, numberOfColorChannels] = size(a) % No semicolon!
What does that spew out to the command window?
Thank both of you for answering my question! (a is a grey-scale tiff image, so I guess my old code would be fine?)
The training sample i used to train my ensemble regression model has 6 predictor variables, so that the testing sample (X in the code) should also be a 6-column matrix or table. These 6 variables are DN values of different bands. Therefore I think the predictors and the image itself are related somehow.
I also tried this following code (changed a bit), the for loop DID work, but it returned me a 753*1 numeric array (same # of rows to the X input), but not a NR*NC array of predicted values that I expected. So the problem is how can I get a NR*NC array of predicted Y value then? Thank you again!
for i = 1:NR
for j = 1:NC
Yfit = predict(Mdl1999276,X); % I deleted (i,j)
end
end
Given any one pixel, do you need the full 753*6 X array to predict for it?
I suspect that you already built the knowledge in X into Mdl1999276 and should not be passing it in. I suspect you should be using
for j = 1 : NC
Yfit(:,j) = predict(Mdl1999276, a(:,j));
end
or something similar.
Does any paper or research that relevant with predict number of pixel using machine learning, Best Regards,
It might be related to
https://www.sciencedirect.com/science/article/pii/S0016236121003203

Sign in to comment.

More Answers (0)

Asked:

Zoe
on 7 Jul 2017

Commented:

on 28 Jun 2021

Community Treasure Hunt

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

Start Hunting!