How to convert uint8 to int in Matlab? - matlab

I have converted the image from RGB to grayscale and have the image-matrix in uint8. I'm trying to make some image processing with convolution which means that I want to use the values as int when I'm summing up everything. So, how can I convert from uint8 to int in Matlab?

Matlab support multiple integer formats. The main difference is the required space in memory and if the sign (+ or -) is used.
For example, uint8 means that the integer is unsigned and that it uses 8 bit to store the value. The number of used bits determines the maximal value. The uint8 can store a number between 0 and 2^8-1.
You can find a whole list of all supported integer here
If you would like to convert your uint8 into another format you can just write the desired format as a function and pass the value as parameter:
I2 = uint16(I);

In matlab you can use double type.
for example type cast with:
I2=double(I);
It is easy to understand that you can use
int8(I)
int16(I)
too.

Related

Type casting in Matlab

What is the proper way to cast data types in Matlab?
I have my data stream (instrument data) as undifferentiated elements of a uint8 array, which I want to cast into a composite data types, like for instance a 3-byte, 24-bit integer, or a 3-byte, 3 character string.
I normally do this on the fly specifying a format in fread(), but once the data is already in a uint8 array, is typecast() the proper way to go about it, or is there a nicer syntax?
As I know, there is not any data type for integer 3 or 24 bytes (see documentaion). However, about casting from uint8 to string, it can be done by num2str function (like num2str(uint8(123)). Also if you want get a 2 char string (from left) you can do it by num2str(uint8(123))(end-1:end);

Why are MATLAB uint8 value and uint16 values different

I have declared a uint8 value as A = [4, 8, 16, 32]; and I casted to the value B = typecast(uint8(A), 'uint16'), but the answer is 2052 8208 I would be very thankful if anyone could help me to understand the reason behind it.
You are probably expecting for Matlab to put your uint8 values just into uint16 variables. This is not what the typecast function is doing. It will preserve the number of bytes from the input to the output. So in your example, it merges the bit representation of 4 and 8 into a uint16 number and equivalently also 16 and 32.
So the binary representation of 4 is 00000100 and of 8 is 00001000 and merged together (to a 16bit number) they give 0000100000000100, which is 2052.
MATLAB's typecast function converts the data type without changing the underlying data. In other words, it doesn't changes the underlying memory representation of the data structure, and simply treats it as a uint16 instead of uint8.
In your case, you want to preserve to original values of your data after casting. Therefore, you do want MATLAB to change the memory representation of the data structure.
There are two ways to perform this type of casting:
-using cast function
B = cast(uint8(A), 'uint16');
-using a direct call to uint16 function:
B = uint16(A);
result:
B =
4 8 16 32

Matlab reading 24-bit ascii-hex file into a 32-bit signed

I'd like to plot every 9th element from an ascii file containg a single column of 24-bit signed hex values
ex.
813457
123456
241566
etc..
The problem is, I can't get Matlab to treat values 800000-FFFFFF as negative, presumably because it's not sign extending it into 32-bits.
I thought of breaking it up into strings, then converting, but sscanf requires the '0x' to convert signed %i hex values so I'm forced to use unsigned:
C = textscan(fp, '%s') %generates 16380x1 cell (instead of normal array?!)
sscanf (C{1,1}{1,1}, '%x') %convert first ascii hex element from cell to unsigned hex
Interestingly, as a test, just doing hex2dec('FFC00000') results in a postive number, how can I force all the ascii lines in the file to be imported as an array of signed 24 or 32bit data?
To do the conversion you need, you can follow these steps:
Convert from hex string to a positive integer of double type, using herx2dec, as you've already done.
Convert that value to uint32 type.
Interpret that uint32 value as an int32 (convert data type without changing underlying data), using typecast.
Example:
>> typecast(uint32(hex2dec('FFC00000')),'int32')
ans =
-4194304

How to use Bitxor for Double Numbers?

I want to use xor for my double numbers in matlab,but bitxor is only working for int numbers. Is there a function that could convert double to int in Matlab?
The functions You are looking for might be: int8(number), int16(number), uint32(number) Any of them will convert Double to an Integer, but You must pick the best one for the result You want to achieve. Remember that You cannot cast from Double to Integer without rounding the number.
If I understood You correcly, You could create a function that would simply remove the "comma" from the Double number by multiplying your starting value by 2^n and then casting it to Integer using any of the functions mentioned earlier, performing whatever you want and then returning comma to its original position by dividing the number by 2^n
Multiplying the starting value by 2^n is a hack that will decrease the rounding error.
The perfect value for n would be the number of digits after the comma if this number is relatively small.
Please also specify, why are You trying to do this? This doesn't seem to be the optimal solution.
You can just cast to an integer:
a = 1.003
int8(a)
ans =
1
That gives you an 8 bit signed integer, you can also get other size i.e. int16 or else unsigned i.e. uint8 depending on what you want to do

MATLAB image summation confusion

I am trying to sum over my image (it is a 128x128 Uint8) in MATLAB (just a simple for loop), however, my sum will only go up to a value of 255. Afterwords it just keeps displaying 255 over and over again.
Does this mean that my variable has been assigned a Uint8 or something? If so how do I change this?
Cheers!
Yes, presumably your data is of type Uint8. But you don't have to loop to sum, just use the sum function. Assuming your data is in x:
total = sum(double(x(:)))
sum will operate over a single dimension, so if you just passed it double(x) directly, it would return a 1x128 matrix; here we have passed it all the data reshaped to a single-dimension vector (using (:)), which has been converted to double using the double function.
Note that the type of your variable will be displayed along with its name and size in the Workspace window.