picture degrade

feli-Xie發表於2020-10-16
  1. degrade the picture for N degree
  2. plot the degraded pictures with different start pixel
import numpy as np

def gen_mesh(x,y,step):
    x0 = np.arange(-x,x,step)
    y0 = np.arange(-y,y,step)
#    xx0,yy0 = np.meshgrid(x0,y0,sparse=True)
    xx,yy = np.meshgrid(x0,y0)
    return xx,yy

def degradeN(zz,N):
    def genzz(zz,i,j,N):
        newzz = 0
        for m in range(N):
            for n in range(N):
                newzz += zz[i*N+m][j*N+n]
        return newzz
                
    m = zz.shape[0]
    n = zz.shape[1]
    
    newzz = np.zeros((int(m/N),int(n/N)))
    for i in range(0,int(m/N)):
        for j in range(0,int(n/N)):
            newzz[i][j] = genzz(zz,i,j,N)    
    return newzz


def degradeNN(zz,orx,ory,N):
    def genzz(zz,i,j,orx,ory,N):
        newzz = 0
        for m in range(N):
            for n in range(N):
                mx = i*N + orx + m
                ny = j*N + ory + n
#                if mx < zz.shape[0]-N+1 and ny < zz.shape[1]-N+1:
                newzz += zz[mx][ny]
        return (newzz)/N/N
                
    m = zz.shape[0]
    n = zz.shape[1]
    
    newzz = np.zeros((int((m-orx)/N),int((n-ory)/N)))
    for i in range(0,int((m-orx)/N)):
        for j in range(0,int((n-ory)/N)):
            newzz[i][j] = genzz(zz,i,j,orx,ory,N)    
    return newzz



import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

xx,yy = gen_mesh(100,100,2)

fig = plt.figure(1,figsize=(12,8))
zz = ((xx-10)**4 + (yy+10)**4)/20000
ax = fig.add_subplot(3,3,1,projection='3d')
ax.set_top_view()
ax.plot_surface(xx,yy,zz,rstride=1, cstride=1, cmap='rainbow')

zz2 = degradeNN(zz,1,1,10)
xx2,yy2 =gen_mesh(zz2.shape[1],zz2.shape[0],2)
ax2 = fig.add_subplot(3,3,4,projection='3d')
ax2.set_top_view()
ax2.plot_surface(xx2,yy2,zz2,rstride=1, cstride=1, cmap='rainbow')

zz3 = degradeNN(zz,8,3,10)
xx3,yy3 =gen_mesh(zz3.shape[1],zz3.shape[0],2)
ax3 = fig.add_subplot(3,3,7,projection='3d')
ax3.set_top_view()
ax3.plot_surface(xx3,yy3,zz3,rstride=1, cstride=1, cmap='rainbow')

zz4 = degradeNN(zz,8,8,10)
xx4,yy4 =gen_mesh(zz4.shape[1],zz4.shape[0],2)
ax4 = fig.add_subplot(3,3,8,projection='3d')
ax4.set_top_view()
ax4.plot_surface(xx4,yy4,zz4,rstride=1, cstride=1, cmap='rainbow')

plt.show()