How do I map the data using interp1()?
Show older comments
I am mapping the data using interp1(). The code is not working, please help me to solve this issue
clc
clear all
close all
x=imread('cameraman.tif');
[r c]=size(x);
[ri ci]=imhist(x);
for i=1:r
for j=1:c
z=x(i,j);
end
end
a=interp1(ci,ri,z,'cubic');
plot(a);
6 Comments
Jan
on 6 May 2019
You forgot to mention the purpose of the code. This
for i=1:r
for j=1:c
z=x(i,j);
end
end
overwrites the variable z r*c-1 times, such that it is equivalent to:
z = x(r,c);
What do you want to achieve instead?
The 1st input of interp1 is the original x value, the 2nd the original y value, and the 3rd the x values for which you want the interpolation. Then "ci, ri, z" is strange. I cannot guess, what you expect as output.
Rik
on 6 May 2019
What are you trying to do? Your code is hard to understand. The entire loop can be replaced with z=x;, and I don't understand what you're trying to interpolate.
Avinash Bhatt
on 7 May 2019
Rik
on 7 May 2019
What do you mean by mapping? What are you trying to do with the histogram? The function you're using only works for a one dimensional input.
Rik
on 7 May 2019
Sir,
I mean to say I want to map the image pixels(x-axis) to the frequency of image image pixels(y-axis) on histogtam of image and I am trying to map the image data using cubic spline interpolation on histogram therefore I am using interp1().
The image attached clearly dipicts which I ma trying to do.
Answers (1)
Rik
on 7 May 2019
I have no clue why you would want to do this, but the code below will treat the histogram as a lookup table. You need to convert the image to a vector of double type for this to work, and picking a good normalization to get back to grayscale might be tricky, but you should be able to edit this code.
x=imread('cameraman.tif');
[counts,centers]=imhist(x);
x2=interp1(centers,counts,double(x(:)));
x2=reshape(x2,size(x));%note x2 is a double, not uint8
figure(1),clf(1)%use clf only during debuggin
imshow(x2,[])
5 Comments
Rik
on 13 May 2019
Dear Sir,
Is it possible to use the data array in x2 can be used in polyval() function for obtaining piecewise polynomial.
Rik
on 13 May 2019
No, that is not how polyval works. Why don't you explain what it is you want to do? Because it doesn't make any sense.
Also, please don't post comments in the answer field. It makes the conversation more confusing, as their order can change.
Avinash Bhatt
on 13 May 2019
Rik
on 13 May 2019
This is a really strange way to threshold an image. You need to do a lot of assumptions about your input before you can do a reasonable fit and determine a reasonable threshold value from that fit. I can't really get it to work with fit, even with this image (cameraman.tif), which contains two distinct groups of intensity levels. You need to assume the order of the polynomial you want to fit, as well as the number of pieces. If your initial guesses are too far off, you have no hope of getting a reasonable fit, and there is no real way to predict the initial guesses without first performing the thresholding operation in a more reasonable way.
If this is homework I would ask your instructor for advice on how to proceed, otherwise I would suggest rethinking this strategy.
Avinash Bhatt
on 13 May 2019
Edited: Avinash Bhatt
on 13 May 2019
Categories
Find more on Logical in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!