csp认证202212-3 第三题JPEG解码

文章描述了一个使用Python编写的算法,涉及矩阵操作、图像量化以及坐标变换,通过四个方向的函数(f1-f4)对输入数据进行处理,最后计算并输出量化后的图像。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

from math import *
global cnt
def hj(x):
    if x>=255:
        return 255
    elif x<=0:
        return 0
    elif x%1<0.5:
        return x//1
    else:
        return x//1 +1
def arf(x):
    if x==0:
        return sqrt(0.5)
    else:
        return 1
def f1(sy1,sx1):#向左下
    global cnt
    while sy1>=0:
        Q[sx1][sy1]=sm[cnt]
        sx1+=1  #sx是第几个数组,即第几行
        sy1-=1
        cnt+=1
def f2(sy1,sx1):#向右上
    global cnt
    while sx1>=0:
        Q[sx1][sy1]=sm[cnt]
        sx1-=1
        sy1+=1
        cnt+=1

def f3(sy1,sx1):#向左下
    global cnt
    while sx1<=7:
        Q[sx1][sy1]=sm[cnt]
        sx1+=1  #sx是第几个数组,即第几行
        sy1-=1
        cnt+=1
def f4(sy1,sx1):#向右上
    global cnt
    while sy1<=7:
        Q[sx1][sy1]=sm[cnt]
        sx1-=1
        sy1+=1
        cnt+=1

lh=[] #量化
cnt=0
for i in range(8):
    lh.append(list(map(int,input().split())))
n=int(input())
T=int(input())
sm=list(map(int,input().split()))  #扫描数据
for i in range(64-n):
    sm.append(0) #补0
Q=[[0 for i in range(8)] for j in range(8)]

Q[0][0]=sm[cnt]
cnt+=1
x=1
y=0
while x!=7 and y!=8:
    f1(x,y)
    x,y=y,x
    y+=1
    f2(x,y)
    x,y=y,x
    x+=1
f1(x,y)
x,y=y,x
x+=1
#开始向右上走


while cnt<=63:
    f4(x,y)
    x,y=y,x
    y+=1
    f3(x,y)
    x,y=y,x
    x+=1
for i in range(8):
    for j in range(8):
        lh[i][j]*=Q[i][j]

ans=[[0 for i in range(8)] for j in range(8)]
for i in range(8):
    for j in range(8):
        for v in range(8):
            for u in range(8):
                ans[i][j]+=arf(u)*arf(v)*lh[u][v]*cos((pi/8)*(i+0.5)*u)*cos((pi/8)*(j+0.5)*v)

for i in range(8):
    for j in range(8):
        ans[i][j]*=0.25
        ans[i][j]+=128
        ans[i][j]=int(hj(ans[i][j]))
if T==0:
    for i in Q:
        print(*i)
elif T==1:
    for i in lh:
        print(*i)
elif T==2:
    for i in ans:
        print(*i)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值