(๑•͈ᴗ•͈)❀送花给你

DCT实现代码

import  math

#输入图片数据   输出离散余弦变换后的图片数据
def DCT(IN,N):
    OUT=[[0 for i in range(N)] for j in range(N)]
    for p in range(N):
        for q in range(N):
            coefficient1 = 0.0
            coefficient2 = 0.0
            if p==0:
                coefficient1=math.sqrt(1.0/N)
            else:
                coefficient1=math.sqrt(2.0/N)
            if q==0:
                coefficient2=math.sqrt(1.0/N)
            else:
                coefficient2=math.sqrt(2.0/N)
            temp=0.0
            for i in range(N):
                for j in range(N):
                    temp+=IN[i][j]*math.cos((2*i+1)*math.pi*p/(2*N))*math.cos((2*j+1)*math.pi*q/(2*N))
            OUT[p][q]=round(coefficient1*coefficient2*temp)
    return OUT

def IDCT(IN,N):
    OUT=[[0 for i in range(N)] for j in range(N)]
    for i in range(N):
        for j in range(N):
            tmp=0.0
            for p in range(N):
                for q in range(N):
                    if p == 0:
                        coefficient1 = math.sqrt(1.0 / N)
                    else:
                        coefficient1 = math.sqrt(2.0 / N)
                    if q == 0:
                        coefficient2 = math.sqrt(1.0 / N)
                    else:
                        coefficient2 = math.sqrt(2.0 / N)
                    tmp+=coefficient1*coefficient2*IN[p][q]*math.cos((2*i+1)*math.pi*p/(2*N))*math.cos((2*j+1)*math.pi*q/(2*N))
            OUT[i][j]=round(tmp)
    return OUT
posted @ 2021-10-04 17:08  胸前小红花  阅读(68)  评论(0)    收藏  举报