unity Mathf.PerlinNoise not between 0 and 1 - unity3d

Does anybody know why this:
Debug.Log(Mathf.PerlinNoise(190911.45f, 2290937.40f));
gives me: 1.044323 It should have been between 0 and 1 isn't it?
And if it can get bigger than 1 can it get smaller than 0? I'm making a map with sprites and everything works :) except that i get empty spaces if the value is bigger than 1.
I use a random seed, that's why the numbers are so big, if you wondering.
I hope someone can help me out, Thanks :)

From the Unity Documentation,
Note: It is possible for the return value to slightly exceed 1.0f. You may need to clamp the return value if the 0.0 to 1.0 range is important to you.
So you need to use float normalized = Mathf.Clamp(Mathf.PerlinNoise(190911.45f, 2290937.40f),0,1f)
Where second argument is minimum value while third argument is maximum value.

Related

How to change pixel values of an RGB image in MATLAB?

So what I need to do is to apply an operation like
(x(i,j)-min(x)) / max(x(i,j)-min(x))
which basically converts each pixel value such that the values range between 0 and 1.
First of all, I realised that Matlab saves our image(rows * col * colour) in a 3D matrix on using imread,
Image = imread('image.jpg')
So, a simple max operation on image doesn't give me the max value of pixel and I'm not quite sure what it returns(another multidimensional array?). So I tried using something like
max_pixel = max(max(max(Image)))
I thought it worked fine. Similarly I used min thrice. My logic was that I was getting the min pixel value across all 3 colour planes.
After performing the above scaling operation I got an image which seemed to have only 0 or 1 values and no value in between which doesn't seem right. Has it got something to do with integer/float rounding off?
image = imread('cat.jpg')
maxI = max(max(max(image)))
minI = min(min(min(image)))
new_image = ((I-minI)./max(I-minI))
This gives output of only 1s and 0s which doesn't seem correct.
The other approach I'm trying is working on all colour planes separately as done here. But is that the correct way to do it?
I could also loop through all pixels but I'm assuming that will be time taking. Very new to this, any help will be great.
If you are not sure what a matlab functions returns or why, you should always do one of the following first:
Type help >functionName< or doc >functionName< in the command window, in your case: doc max. This will show you the essential must-know information of that specific function, such as what needs to be put in, and what will be output.
In the case of the max function, this yields the following results:
M = max(A) returns the maximum elements of an array.
If A is a vector, then max(A) returns the maximum of A.
If A is a matrix, then max(A) is a row vector containing the maximum
value of each column.
If A is a multidimensional array, then max(A) operates along the first
array dimension whose size does not equal 1, treating the elements as
vectors. The size of this dimension becomes 1 while the sizes of all
other dimensions remain the same. If A is an empty array whose first
dimension has zero length, then max(A) returns an empty array with the
same size as A
In other words, if you use max() on a matrix, it will output a vector that contains the maximum value of each column (the first non-singleton dimension). If you use max() on a matrix A of size m x n x 3, it will result in a matrix of maximum values of size 1 x n x 3. So this answers your question:
I'm not quite sure what it returns(another multidimensional array?)
Moving on:
I thought it worked fine. Similarly I used min thrice. My logic was that I was getting the min pixel value across all 3 colour planes.
This is correct. Alternatively, you can use max(A(:)) and min(A(:)), which is equivalent if you are just looking for the value.
And after performing the above operation I got an image which seemed to have only 0 or 1 values and no value in between which doesn't seem right. Has it got something to do with integer/float rounding off?
There is no way for us to know why this happens if you do not post a minimal, complete and verifiable example of your code. It could be that it is because your variables are of a certain type, or it could be because of an error in your calculations.
The other approach I'm trying is working on all colour planes separately as done here. But is that the correct way to do it?
This depends on what the intended end result is. Normalizing each colour (red, green, blue) seperately will result in a different result as compared to normalizing the values all at once (in 99% of cases, anyway).
You have a uint8 RGB image.
Just convert it to a double image by
I=imread('https://upload.wikimedia.org/wikipedia/commons/thumb/0/0b/Cat_poster_1.jpg/1920px-Cat_poster_1.jpg')
I=double(I)./255;
alternatively
I=im2double(I); %does the scaling if needed
Read about image data types
What are you doing wrong?
If what you want todo is convert a RGB image to [0-1] range, you are approaching the problem badly, regardless of the correctness of your MATLAB code. Let me give you an example of why:
Say you have an image with 2 colors.
A dark red (20,0,0):
A medium blue (0,0,128)
Now you want this changed to [0-1]. How do you scale it? Your suggested approach is to make the value 128->1 and either 20->20/128 or 20->1 (not relevant). However when you do this, you are changing the color! you are making the medium blue to be intense blue (maximum B channel, ) and making R way way more intense (instead of 20/255, 20/128, double brightness! ). This is bad, as this is with simple colors, but with combined RGB values you may even change the color itsef, not only the intensity. Therefore, the only correct way to convert to [0-1] range is to assume your min and max are [0, 255].

Subtracting matrices of unequal dimension

When I try to do the following command I get an error.
err = sqrt(mean((xi256-xc256).^2))
I am aware that the matrix sizes are different.
whos xi256 xc256` gives:
Name Size Bytes Class Attributes
xc256 27x1 216 double
xi256 513x1 4104 double
I am supposed to negate find the difference of these two matrices. In fact the command given at the top was in the course notes and the course has been running for several years! I have tried googling ways to resolve this error to perform this subtraction regardless but have found no solution. Maybe one of the matrices can be scaled to match the dimensions of the other? However, I have not been able to find any such functions that would let me do this.
I need to find the RMS error in a given set of data. xc256 was calculated through a numerical method, xi256 gives the true value.
Edit: I was able to use another set of results.
First check that xc256 is correctly computed and that you do not have a matrix transposition gone wrong or something like that. Computing the RMS between two vector of different sizes does not make sense, and padding or replicating will get you rid of the error, but is most probably not what you actually want.
There are just two situations that I can think of, I will list them here:
The line is wrong (not likely as it looks pretty normal, but make sure to check the book!)
The input of the line is wrong
Assuming it is in point 2, again there are two possibilities:
xi256 is of incorrect size (likelyhood of this depends on how you got it)
xc256 is of incorrect size
Assuming it is again point 2, there are yet again 2 likely possibilities:
xc256 should be a scalar
xc256 should be a vector with the same size as xi256
From here on there is insufficient information to continue, but check whether you accidentally made it 27 times as long, or 19 times too short somewhere. Just use some breakpoints throughout the code to see how the size develops.

VPL doesn't calculate cube root

I was trying to calculate cube root using VPL:
https://www.dropbox.com/s/vjng6fjf081ovkq/shot_140214_212725.png
What I'm doing is setting the value to the power of 1/3
https://www.dropbox.com/s/4f8eqep8rm3jpw0/shot_140214_212742.png
And as a result it gives me 1 all the time.
Where the problem could be?
Thank you.
I bet 1/3 is getting truncated to 0, because the numerator and denominator are both integers.
Any number raised to the 0 power gives 1.
If you use 1.0/3.0 instead, it might work.

Matlab: 'Matrix dimensions must agree' less than operator (<)?

A quick question because i fear there may already be an answer (although i cant find it)
i am getting the error: Matrix dimensions must agree.
because i am useing '<'
now with all the other operators there is a way around this either by putting '.' infront or by using a different formula. So what do people do about the less than operator????
i don't see why the greater than or equal to (>=) works but yet less than does not!?
am i being stupid and missed something really obvious??
code snippet
matrix 1 represents an array of 16 numbers
matrix 2 can represents anywhere between 10 and 20 numbers
idx = (matrix2 >= matrix1 * 0.1 & matrix2 < matrix1 * 1.5);
any help guidance or advice on the topic would be much appreciated! thank you!
EDIT
i know the matrices are different sizes but is there a way to use less then with different size arrays? as im not bothered about the size of the array but the numbers within
If you want to compare parts of matrices, like M(1:3,10:12)>A(5:7,1:3), you, probably, have to use the function squeeze():
squeeze(M(1:3,10:12))>squeeze(A(5:7,1:3))
This function remotes singleton dimensions and everything works fine after.

using linspace in matlab

I want to create a vector which runs from 1 to 260 with increments of 360 between every whole number.
I can do this manually by: y=linspace(1,2,360); y1=linspace(2,3,360);... and so on.
By combining these I would have a vector which was 260*360=93600 long. However, there must be a easier way of doing this? preferably without a loop.
Maybe you can just do:
n=261;
linspace(1,n,(n-1)*360);
And what about y=(1:1/360:260) ?
Or if you want to have exactly 360 elements between 1 and 2 (included) as it seems from your use of linspace(1,2,360) you could do y=(1:1/359:260).
Also, your final vector would have less than 360*260 elements as you have to account for duplicates.