Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Compressing Matrix
#1
I want to compress a gray image whose dimension is 64x64 into 40x40 image

I tried scipy.fftpack.dct and scipy.fftpack.idct but the resulted image is not in high quality

Also tried numpy.savez_compressed the resulted image quality is very good but it has two disadvantage, One it save the compressed array in a file !!! I dont need it to save the compressed array in a file and the other disadvantage is that the compressed array dimension is arbitrary and not controlled


Does anyone know how to compress a gray image in a pre specified dimension compressed array with high quality and without saving it in a file ?
Reply
#2
You are mixing 2 concepts:
  • Data Compression: This is what numpy.savez_compressed by just storing the full 64x64 matrix in a file and then using gzip with it. If your matrix is completely random (say, np.random.randint(0, 256, (64, 64)) compressing will almost no change the size, but for normal matrices with a lot of patterns the compression can be huge. There is no loss of quality.
  • Image Compression: This is what you obtain with scipy.fftpack.dct or when you create a jpg from a png. You will always have some data loss as what you are doing is to reduce the size at the expenses of loosing quality. In this case you are putting the information of a 64x64 array in a 40x40 so there is no way you can achieve this without loosing quality. Nevertheless it is possible to choose a way to loose quality in parts of the image "not visible"... this is more or less what jpg does choosing wisely some weights for the dct.

both strategies can be combined so you can obtain the dct coefficients of your image and then the result stored it in a compressed file (like the npz files from numpy)
Reply
#3
Thanks a lot

So scipy.fftpack.dct and scipy.fftpack.idct is the only choice I have for image compression ?
Reply
#4
If what you want is to represent in a matrix of 40x40 the information of a matrix of 64x64... yes, you need some form of resampling and is going to lose information if you try then to expand again to a 64 x 64 matrix.
Using the dct transforms is one of the best ways when you deal with general information, like a photo reduced to grayscale, but not the only one.
You can see an image in grayscale like a function gray=f(x, y) and interpolate the values using iterp2d or even better RectBivariateSpline.

This functions are going to use spline to interpolate, so the results for a normal image are normally not good, but if the image is really for example the lecture of a sensor they can produce more accurate results.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020