How to reverse this normalization - matlab

Can any one help me please to reverse the following normalization?
image_normalized = uint8(255*mat2gray(image));
This command normalizes a matrix values to 0-255. So, after doing some filtering (denoising), how can I transform the new filtered values from 0-255 to the original value space?

I do not exactly how mat2gray works, but I think that it perfomrs a "contrast expansion".
Ioutput[i,j] = ( Iinput[i,j]-min )* K / (max - min)
where min = min(Iinput(:)) and max = max(Iinput(:)) , and K is a constant.
And what you can do to reverse the normalization is to find K and save the max and min value of the input image.
On the other hand, if you are dealing with noise you should consider that the min or max value could be a corrupted pixel.

Related

using Matlab, how to find the maximum value over a certain range?

i want to know the coding to find the maximum value, over a certain range.
i already coded like below.
f=f'
ac_yyyy_f=ac_yyyy_f'
[row,col] = ind2sub(size(ac_yyyy_f),find(ac_yyyy_f==max(ac_yyyy_f)))
but the problem is, sometimes the maximum value of Y axis choosen by my code is not what i want.
the X axis has the range of 0 to 100000 and i want the maximum between 20000 to 100000. the problem is sometimes the max value show up at the range of 0 to 20000.
How can i figure this out?
Use the max() function:
%let R be your range of values
R = [2 1 7 4];
[value, index] = max(R);
In the above example, value will be 7 and index will be 3
For more info: http://fr.mathworks.com/help/matlab/ref/max.html
I'm using a vector of random integers to stand in for your function output.
a = floor(rand(1,100000)*100);
[val, idx] = max(a(20000:100000));
You want to use the max function here to find the maximum value rather than find.
Now, the other part of the task is getting the max value from a certain part of your matrix. You can pass just a subset of a vector or matrix to a function by indexing it with a range of values. Note that idx gives you the position of val within a(20000:100000). If you need the position within a, you need to use idx+19999.
Also, you should take a look at the matrix indexing reference—there are many different and fun ways to index a matrix—because indexing is one of the most important features of matlab.
Here's the reference for the max function:
http://www.mathworks.com/help/matlab/ref/max.html
And the reference for indexing:
http://www.mathworks.com/help/matlab/math/matrix-indexing.html
You can use the max function with a subset of your array. It will return the maximum value, as well as the index where it is located. Be sure to correct the index it returns you based on your desired range. Like this:
%//create an array of 100,000 values to play with.
f=floor(rand(100000,1).*100);
%//find the max between f(20000) and f(100000)
[myMax, I] = max( f(20000:100000) );
%//correct the index based on where we started looking
%//for the max. Subtract 1 because it's MATLAB!
myIndex = I+20000-1;
This results in:
>> myMax
myMax =
99
>> myIndex
myIndex =
20045
>> f(myIndex)
ans =
99

finding the location of max and min and replacing their values in a matrix

If I generated a random 5-by-5 matrix using the code r = rand(5).
I would like to find the location of the max value and replace this value by 10, and the min value and replace the value by -10.
How can I do it?
i tried to do the following:
r=rand(5)
find(max(max(r)))
Will this line give me the correct location of max value? And if it was correct now how can I replace the value by 10?
r = rand(5);
maxr = max(r(:));%//get maximum
r(r==maxr) = 10; %// replace maximum with 10
Use logical indexing to replace the maximum value with 10.
max returns two output arguments, first the value and then the index. Using (:) to convert your matrix to a vector and linear indexing to access, you can use this code:
[value,index]=max(r(:));
r(index)=10;

Operation on specific part of a vector in MATLAB

I am new to MATLAB and I have a question which I think should have an easy solution.However, I am stuck now.
My program produces a vector as a result which contains positive and negative values.
I wish to find a solution that I could assign only positive values of the vector to a new vector and replace the negative values with 0. Of course the size of the vectors should be the same.
The vector size is 1*345600
Pbat(t) ...... (has both negative and positive numbers)
Pbat1(t) ...... (should have the same size as Pbat(t) while changing negative values to 0)
Thanks in advance,
Hamed
Easy, using logical indexing...
initial_vector = rand(1,345600);
new_vector = initial_vector;
new_vector(initial_vector<0)=0;
Just use max(..., 0):
initial_vector = randn(1,345600); %// example data
new_vector = max(initial_vector, 0); %// set negative values to 0

NaN produced from division of values put a Vector

Below is some MATLAB code which is inside a loop that increments position every cycle. However after this code has run, the values in the vector F1 are all NaN. I print A to the screen and it outputs the expected values.
% 500 Hz Bands
total_energy = sum(F(1:(F_L*8)));
A = (sum(F(1:F_L))) /total_energy %% correct value printed to screen
F1(position) = (sum(F(1:F_L))) /total_energy;
Any help is appreciated
Assuming that F_L = position * interval, I suggest you use something like:
cumulative_energy_distribution = cumsum(abs(F).^2);
positions = 1:q;
F1 = cumulative_energy_distribution(positions * interval) ./ cumulative_energy_distribution(positions * 8 * interval);
sum-of-squares, which is the energy density (and also seen in Parseval's theorem) is monotonically increasing, so you don't need to worry about the energy going back down to zero.
In MATLAB, if you divide zero by zero, you get a NaN. Therefore, its always better to add an infinitesimal number to the denominator such that its addition doesn't change the final result, but it avoids division by zero. You can choose any small value as that infinitesimal number (such as 10^-10) but MATLAB already has a variable called eps.
eps(n) gives a distance to the next-largest number (whose precision is same as n) which could be represented in MATLAB. For example, if n=1, next double-precision number you could get to from 1 is 1+eps(1)=1+2.2204e-16. If n=10^10, then the next number is 10^10+1.9073e-06. eps is same as eps(1)=2.2204e-16. Adding eps doesn't change the output and avoids the situation of 0/0.

Using find on non-integer MATLAB array values

I've got a huge array of values, all or which are much smaller than 1, so using a round up/down function is useless. Is there anyway I can use/make the 'find' function on these non-integer values?
e.g.
ind=find(x,9.5201e-007)
FWIW all the values are in acceding sequential order in the array.
Much appreciated!
The syntax you're using isn't correct.
find(X,k)
returns k non-zero values, which is why k must be an integer. You want
find(x==9.5021e-007);
%# ______________<-- logical index: ones where condition is true, else zeros
%# the single-argument of find returns all non-zero elements, which happens
%# at the locations of your value of interest.
Note that this needs to be an exact representation of the floating point number, otherwise it will fail. If you need tolerance, try the following example:
tol = 1e-9; %# or some other value
val = 9.5021e-007;
find(abs(x-val)<tol);
When I want to find real numbers in some range of tolerance, I usually round them all to that level of toleranace and then do my finding, sorting, whatever.
If x is my real numbers, I do something like
xr = 0.01 * round(x/0.01);
then xr are all multiples of .01, i.e., rounded to the nearest .01. I can then do
t = find(xr=9.22)
and then x(t) will be every value of x between 9.2144444444449 and 9.225.
It sounds from your comments what you want is
`[b,m,n] = unique(x,'first');
then b will be a sorted version of the elements in x with no repeats, and
x = b(n);
So if there are 4 '1's in n, it means the value b(1) shows up in x 4 times, and its locations in x are at find(n==1).