SoFunction
Updated on 2024-11-20

OpenCV Image Coding and Decoding Practice

Original image:

Image information, you can see that the image is a 816*2100 pixel image.

python code:

import cv2
import numpy as np
import  as plt
 
img = ('', 0)
img1 = ('float')
img_dct = (img1)
img_dct_log = (abs(img_dct))
img_recor = (img_dct)
recor_temp = img_dct[0:100,0:100]
recor_temp2 = ()
recor_temp2[0:100,0:100] = recor_temp
print recor_temp.shape
print recor_temp2.shape
img_recor1 = (recor_temp2)
(221)
(img)
('original')
(222)
(img_dct_log)
('dct transformed')
(223)
(img_recor)
('idct transformed')
(224)
(img_recor1)
('idct transformed2')
 
()

The effect after extracting just one 100*100 DCT coefficient:

When using 800*1000 DCT coefficients:

You can see that the image is a little more detailed:

import cv2
import numpy as np
import  as plt
 
img = ('', 0)
img1 = ('float')
img_dct = (img1)
img_dct_log = (abs(img_dct))
img_recor = (img_dct)
recor_temp = img_dct[0:800,0:1000]
recor_temp2 = ()
recor_temp2[0:800,0:1000] = recor_temp
print recor_temp.shape
print recor_temp2.shape
img_recor1 = (recor_temp2)
(221)
(img)
('original')
(222)
(img_dct_log)
('dct transformed')
(223)
(img_recor)
('idct transformed')
(224)
(img_recor1)
('idct transformed2')
 
()

When using 816*1200 DCT coefficients:

You can see that the image is back to its original quality.

Analyzing the code:

img_dct holds the dct-transformed matrix, and img_dct_log is the matrix in which the elements of the matrix are first taken to their absolute values and then logarithmized.

img_dct_log = (abs(img_dct))

So what is the base of the logarithm?

Print out img_dct_log and abs(img_dct) to see:

Print results:

Where 9.45971865e+04 = 9.45971865 x 10^4 = 94597.1865 represents scientific notation.

We see that only when the base is taken as e, the corresponding logarithm matches the output of the question, so the python function takes the logarithm with the natural constant e as the ground.

To this article on OpenCV image codec practice is introduced to this article, more related OpenCV image codec content, please search for my previous articles or continue to browse the following related articles I hope you will support me more in the future!