For a question on an assignment I am required to implement image compression via thresholding in Discrete Cosine Transformation. I've converted my required greyscale image to a matrix of DCT co-efficients, and have thresholded values by setting co-efficients below a certain proportion of the maximum absolute co-efficient to 0.
Now I am also required to calculate the total number of bits after compression. Assuming that all co-efficients that have value 0 require 0 bits to represent, i.e, no overhead of RLE or whatever else, by my understanding the total number of bits should be (number of non-zero co-efficients) x (size of variable), which is double in this case (in Matlab).
The issue is, if I set my threshold to be 1% of the max. value, I end up with 36,714 non-zero values in the thresholded matrix. Since doubles in Matlab are 64 bits, the number of bits required is 64*36714 = 2349696 bits. Compare this with the original 512x512 greyscale image, which required 512*512*8 bits = 2097152 bits. Thus my "compressed" image is actually larger than the original one!
Given that I can only use thresholding, and no quantisation of any sort, is this really the number of bits required with a threshold of 1%? Or is there a way to store doubles in Matlab without having to use 64 bits?
Related
To find displacement of a particle, I calculated the cross correlation between two instants (represented by two images with the same size). Then, I padded the images with zeros to see if a translation will have an effect on the displacement.
Thus I found a difference in displacement vector( the difference can reach 1.5 pixel and the size of image is 56x56 pixels)
Is it normal to find a difference after padding?
N.B: To pad the image, I used
new_image(end+1:56,end+1:56)=0;
EDIT
The difference can even be more for some cases (22 px)
Yes, this is weird. The cross-correlation is calculated by multiplying values in both matrices with eachother and taking the sum of these. Adding zeros should not result in a greater sum.
The problem in the code you've posted is that end+1:56 should likely be end+1:end+56, since you pad it with 56 extra zeros below and to the right of the image this way.
Since your goal appears to be to get the cross-correlation of 2 matrices, I recommend you to look at the xcorr2() and xcorr() functions in Matlab. An explanation for xcorr2() and why zero padding should not have any influence (besides searching a larger image) can be found here.
In the matlab code, i apply the discrete cosine transform in an image,after applying the dct, the file size of the dct image is increased from the original file size.Is it true without applying quantization and entropy coding,the file size of the dct image is normally high?
The DCT alone effectively doubles the amount of storage required for an image. 8-bit samples require 16-bits after the DCT.
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 need a matlab code for a perceptual hashing algorithm descried here:
http://www.hackerfactor.com/blog/index.php?/archives/432-Looks-Like-It.html
Basically I want this to remove deatails in an image and only leave the major structure components information.
To do so, I think I need the following steps:
1. Reduce the DCT. Suppose the DCT is 32x32 (), just keep the top-left 8x8. Those represent the lowest frequencies in the picture.
Compute the average value. Like the Average Hash, compute the mean DCT value (using only the 8x8 DCT low-frequency values and excluding the first term since the DC coefficient can be significantly different from the other values and will throw off the average).
Further reduce the DCT. Set the 64 hash bits to 0 or 1 depending on whether each of the 64 DCT values is above or below the average value. The result doesn't tell us the actual low frequencies; it just tells us the very-rough relative scale of the frequencies to the mean. The result will not vary as long as the overall structure of the image remains the same; this can survive gamma and color histogram adjustments without a problem.
reconstruct image after the processing.
Anyone can help on any one of above steps?
I have tried some code that gives some results (in the below link), it is not yet perfect:
https://stackoverflow.com/questions/26748051/extract-low-frequency-from-dct-coeffecients-of-an-image-in-matlab
Try this:
% read image
I = imread('cameraman.tif');
% cosine transform and reduction
d = dct2(I);
d = d(1:8,1:8);
% compute average
a = mean(mean(d));
% set bits, here unclear whether > or >= shall be used
b = d > a;
% maybe convert to string:
string = num2str(b(:)');
I have an RGB image m in MATLAB
I normalized it using the formula
im = mean(m,3);
im = (im-min(im(:))) / (max(im(:))-min(im(:)));
I read that the normalized module stretches the image pixel values to cover the entire pixel value range (0-1) but I still have some steps between 0 and 1 in the histogram of my normalized image.
Can anyone help me out here by explaining the reason of these grey values.
Thank You
I assume you use the mean of the three components instead of the function rgb2gray because it has some advantages in your case. (rgb2gray does something similar: it uses a weighted sum).
Subtracting the minimum and dividing by the maximum doesn't convert your image to binary. It will only scale all values to the range (0,1), which is the case in your example. This only means that all values are between 0 and 1, not exactly 0 or 1. If you want a binary image, i.e. only 0 and 1, you will need to use a threshold to convert the grayscale image to binary.
A method which is often used, is to calculate a threshold using Otsu's method:
threshold = graythresh(im); % calculate threshold
binaryImage = im2bw(im,threshold); % convert image to binary
If your image is saved as uint8 then normalizing the image should convert your image to binary, as uint8 only can handle integer numbers and any numbers between are rounded. That means you assume the optimal threshold to be at 0.5.