OpenCV實(shí)現(xiàn)圖片編解碼實(shí)踐
原圖:

圖像信息,可以看到圖像是一個(gè)816*2100像素的圖片:

python代碼:
import cv2
import numpy as np
import matplotlib.pyplot as plt
img = cv2.imread('11.jpg', 0)
img1 = img.astype('float')
img_dct = cv2.dct(img1)
img_dct_log = np.log(abs(img_dct))
img_recor = cv2.idct(img_dct)
recor_temp = img_dct[0:100,0:100]
recor_temp2 = np.zeros(img.shape)
recor_temp2[0:100,0:100] = recor_temp
print recor_temp.shape
print recor_temp2.shape
img_recor1 = cv2.idct(recor_temp2)
plt.subplot(221)
plt.imshow(img)
plt.title('original')
plt.subplot(222)
plt.imshow(img_dct_log)
plt.title('dct transformed')
plt.subplot(223)
plt.imshow(img_recor)
plt.title('idct transformed')
plt.subplot(224)
plt.imshow(img_recor1)
plt.title('idct transformed2')
plt.show()
僅僅提取一個(gè)100*100的DCT系數(shù)后的效果:


當(dāng)用800*1000的DCT系數(shù):

可以看到圖像細(xì)節(jié)更豐富了一些:

import cv2
import numpy as np
import matplotlib.pyplot as plt
img = cv2.imread('11.jpg', 0)
img1 = img.astype('float')
img_dct = cv2.dct(img1)
img_dct_log = np.log(abs(img_dct))
img_recor = cv2.idct(img_dct)
recor_temp = img_dct[0:800,0:1000]
recor_temp2 = np.zeros(img.shape)
recor_temp2[0:800,0:1000] = recor_temp
print recor_temp.shape
print recor_temp2.shape
img_recor1 = cv2.idct(recor_temp2)
plt.subplot(221)
plt.imshow(img)
plt.title('original')
plt.subplot(222)
plt.imshow(img_dct_log)
plt.title('dct transformed')
plt.subplot(223)
plt.imshow(img_recor)
plt.title('idct transformed')
plt.subplot(224)
plt.imshow(img_recor1)
plt.title('idct transformed2')
plt.show()
當(dāng)用816*1200的DCT系數(shù):

可以看出圖像恢復(fù)到原來的質(zhì)量了.

分析代碼:
img_dct保存的是dct變換后的矩陣,img_dct_log是矩陣中的元素首先取絕對(duì)值,再求對(duì)數(shù)的矩陣.
img_dct_log = np.log(abs(img_dct))
那么對(duì)數(shù)的底是多少呢?
打印出來img_dct_log和abs(img_dct)看一下:

打印結(jié)果:

其中9.45971865e+04=9.45971865 x 10^4 =94597.1865表示的是科學(xué)計(jì)數(shù)法.

我們看到只有在底數(shù)取e的時(shí)候,對(duì)應(yīng)的對(duì)數(shù)才符合題目輸出要求,所以,python numpy.log函數(shù)取的是以自然常數(shù)e為地的對(duì)數(shù).
到此這篇關(guān)于OpenCV實(shí)現(xiàn)圖片編解碼實(shí)踐的文章就介紹到這了,更多相關(guān)OpenCV 圖片編解碼內(nèi)容請(qǐng)搜索本站以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持本站!
版權(quán)聲明:本站文章來源標(biāo)注為YINGSOO的內(nèi)容版權(quán)均為本站所有,歡迎引用、轉(zhuǎn)載,請(qǐng)保持原文完整并注明來源及原文鏈接。禁止復(fù)制或仿造本網(wǎng)站,禁止在非maisonbaluchon.cn所屬的服務(wù)器上建立鏡像,否則將依法追究法律責(zé)任。本站部分內(nèi)容來源于網(wǎng)友推薦、互聯(lián)網(wǎng)收集整理而來,僅供學(xué)習(xí)參考,不代表本站立場(chǎng),如有內(nèi)容涉嫌侵權(quán),請(qǐng)聯(lián)系alex-e#qq.com處理。
關(guān)注官方微信