How do I map the data using interp1()?

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

Probably related to this question.
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.
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.
Sir, If I have a image matrix as
x=imread('cameraman.tif');
I will get a matrix of mxn dimension. I have plotted a histogtram of this image using
figure, imhist(x);
Now if I want to map the data using interpolation i need x-data and y-data and I am using the following code
[counts centers]=imhist(x);
bar(centers, counts, 'hist');
histpatch = findobj(gca, 'type', 'patch');
edgemat = get(histpatch, 'XData');
countmat = get(histpatch, 'YData');
where x-data gives me the pixels ranging from 0-255, y data gives me the frequency of pixels in x('cameraman.tif') thus i am using
interp1(x,countmat,edgemat,'spline') % interp1(x,y,x_new,'spline')
but the code is not working.
Please help me in eradicating this error.
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.
Comment posted as answer by Avinash Bhatt moved here
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.

Sign in to comment.

Answers (1)

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

Comment mistakenly posted as answer by Avinash Bhatt:
Dear Sir,
Is it possible to use the data array in x2 can be used in polyval() function for obtaining piecewise polynomial.
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.
Sir,
I have attached the work I am doing, I am trying to construct the code for this.
Please, have a look in to it and help me
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
Avinash Bhatt on 13 May 2019
Edited: Avinash Bhatt on 13 May 2019
Sir,
I have compressed the image to 16X16, will it work with this kind of image.
Please suggest me some modifications I can perform in it.

Sign in to comment.

Asked:

on 6 May 2019

Edited:

on 13 May 2019

Community Treasure Hunt

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

Start Hunting!