I have a 8x1 uint8 vector that I want to bitwise XOR with a 8x8 logical matrix, so I need to xor 64 bits with 64 bits in different formats. The result must be a 8x8 logical matrix.
The reason for this is that I am working with a .bmp image and a binary image.
How do I do this in Matlab R2012a?
xor(de2bi(double(A)),B)
If you don't have de2bi.m (Communications Toolbox), take the de2bi.m from an octave installation (open source) and replace "endfunction" with "end" and "endif" with "end" or use this definition: de2bi=#(x)dec2bin(x)=='1'.
Related
MATLAB's built in function to compute local binary patterns, from the computer vision toolbox, produces real valued numbers.
The function is named: extractLBPFeatures()
I understood the values from LBP to be binary - it's in the name!
What is it doing to get real valued numbers rather than binary values?
According to Matlab documentation, the call extractLBPFeatures(<your_image>) returns a 1-by-59 vector representing the L2-normalized histogram of the non rotation-invariant uniform LBPs, i.e. the relative frequencies of these 59 possible patterns throughout the image. The code of a non rotation-invariant uniform LBP formed by 8 neighbouring pixels is a whole number ranging from 0 to 58, whereas the 59 components of the feature vector yielded by extractLBPFeatures are floating point numbers in the range 0..1.
If you wish to obtain an image where the pixel intensity values are replaced by the LBP codes you could use the Matlab implementation developed by the creators of the LBP descriptor.
I need to take a very large matrix that is generated by using the function imread(). This turns an image (mine is a jpg file; 691x763) into a matrix.
I need to divide each element in the matrix by 255 and show each element with at least 3 decimal places.
What I've tried:
output_precision(4) but it didn't work for the elements inside the matrix.
format long e but the values inside the matrix were still unaffected.
You should use im2double http://octave.sourceforge.net/image/function/im2double.html which does the scaling to 0..1 for you independently of the input format which might be uint, uint16, int16 and so on.
I'm guessing imread() gave you a matrix of uint8. Try this instead:
I = imread('image.jpg'); %// your image
A = double(I)/255; %//convert matrix to double before dividing it by 255
When creating a sparse matrix in Matlab it seems that you can create a sparse matrix either filled with logicals or double valued numbers.
While reading around I understood that Matlab does not have support for other type of sparse matrices, i.e. uint8 or other integers. In my application I know that max(values)==16, and the memory is a crucial thing, therefore I would like to have uint8 sparse matrices.
Is there a way of creating a unit8 sparse matrix?
If not (most likely), is there any apparent reason of why Matlab has not implemented uint8 sparse matrices?
I can see how using uint8 instead of a double would be no or little improvement.
A dense matrix is a continuous array so no extra indexing or structuring is required, the position of each element is given by its physical location in the memory.
But a sparse matrix should additionally require to store each element index, which in case of a 2D matrix would be two integers 32 or 64 bits in size to remember each element row and column number. On top of that there might be some implementation related overhead, such as a tree structure, or something else, used to make sparse matrix operations efficient.
So it is not 8 uint8 vs 64 double, eight times more less memory usage, but rather (8+32+32+log(n)+..) vs (64+32+32+log(n)+..), which i guess might end up being 10-20% savings at the best?
Furthermore each memory address now stores 64 bits if I remember correctly, which is one double or 8 uint8 packed together. This means a few extra bits needs to be used per entry just to remember which uint8 packed at that memory address we need, and adds some extra bit masking operations to perform.
So the guys at Mathworks probably did similar estimate, and decided to just do double sparse matrices.
This question already has answers here:
How to know the size of a variable in MATLAB
(6 answers)
Closed 8 years ago.
I need to know how to enumerate the number of bits in an image (in MATLAB) after applying the DCT algorithm (compressing the image) to an image, I need to see the number of bits of an image after applying the DCT algorithm. Because I am applying it to a 512 x 512 image but I am getting the same number of bit before and after compression was wondering is there any Matlab code which calculates the number of bits for an image.
DCT is transform, not compression. No information is lost after applying it to input signal.
Normaly, algorithms like jpg, after DCT transform applies quantization of DCT coefficients to reduce amount of data. After then quantized coefficients arę compressed using huffman or other lossless compression algorithm.
So DCT is not able to say how many bits you are going to have after. You should ask huffman.
The DCT algorithm itself does not reduce the size of the image matrix.
It applies a transform to the spatial domain matrix input, and outputs a
matrix in the frequency domain. See this for a more detailed explanation.
http://www.mathworks.com/help/images/discrete-cosine-transform.html#f21-16149
Depending on the number of DCT coefficients discarded, which dictates the amount
of compression you are applying, the remaining non-zero coefficients tells you how
many bits remains. The number of bits per coefficient is determined by the class of
the image X.
e.g.
class(X)
ans =
double
double is 64 bit
single is 32 bit
I am working with largish binary matrices, at the moment up to 100x100.
Lets say I am working with 30x30 binary matrices. Then there are a total of 2^(30x30) binary matrices. I want to select a binary matrix at random, where each of the 2^(30x30) matrices has the same probability of being selected.
My solution attempt was to pick a number between 1 and 2^(30x30) using the function randi(n) with n = 2^(30x30) and then converting the result to the appropriate binary matrix. The problem I ran into was that randi(n) does not take values for n larger than 2^54. Matlab in general does not seem to like very large numbers.
Any suggestions?
If each matrix of booleans has equal probability, then the elements of the matrix each have equal probability of 0 and 1. You can just fill a matrix of the appropriate size with n² uniform random booleans.
I don't have MATLAB handy, but in Octave you'd do something like unidrnd(2, n, n) - 1.
You can use randint in the range [0 1]:
matrix=randint(30,30,[0 1]);
You can also use rand and threshold the resulting matrix:
matrix=rand(30,30);
matrix=round(matrix);
EDIT: just realized it also works with randi with the following syntax:
matrix=randi([0 1],30,30);