Advertisement
Guest User

SADNESS

a guest
Nov 27th, 2014
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 8.16 KB | None | 0 0
  1. __author__ = 'Mike'
  2.  
  3. from PIL import Image, ImageFilter
  4. import sys, math, numpy, copy
  5. import numpy as np
  6.  
  7. #testing Images
  8.  
  9. #kels = Image.open("C:\\Users\\Mike\\Pictures\\cell pics\\Kelsey test.jpg")
  10. #kblur = kels.filter(ImageFilter.BLUR)
  11. #kels.show()
  12. #kblur.show()
  13.  
  14. def frequency_domain(img):
  15.     (x,y) = img.size
  16.     imgout = img
  17.     c_factorx = 0
  18.     c_factory = 0
  19.     cos_factor = 0.0
  20.     for u in range(0,x):
  21.         if(u is 0):
  22.             c_factorx = 1/(math.sqrt(2))
  23.         else:
  24.             c_factorx = 1
  25.         for v in range(0,y):
  26.             if(v is 0):
  27.                 c_factory = 1/(math.sqrt(2))
  28.             else:
  29.                 c_factory = 1
  30.             for i in range(0,x):
  31.                 for j in range(0,y):
  32.  
  33.  
  34.                     helps = float(img.getpixel((i,j)))
  35.                     cos_factor = cos_factor + helps*(math.cos(u*math.pi*((2*i+1)/(2*x)))*math.cos(v*math.pi*((2*j+1)/(2*x))))
  36.             imgout.putpixel((u,v),0.25*c_factorx*c_factory*cos_factor)
  37.             print("cos factor: ",cos_factor)
  38.             cos_factor = 0
  39.  
  40.     return imgout
  41.  
  42. def limitedWindow(img,xmin,ymin):
  43.     (x,y) = img.size
  44.     imgout = img
  45.     c_factorx = 0
  46.     c_factory = 0
  47.     cos_factor = 0.0
  48.     for u in range(xmin,xmin+8):
  49.         if(u is 0):
  50.             c_factorx = 1/(math.sqrt(2))
  51.         else:
  52.             c_factorx = 1
  53.         for v in range(ymin,y+8):
  54.             if(v is 0):
  55.                 c_factory = 1/(math.sqrt(2))
  56.             else:
  57.                 c_factory = 1
  58.             for i in range(0,8):
  59.                 for j in range(0,8):
  60.  
  61.  
  62.                     helps = float(img.getpixel((u,v)))
  63.                     cos_factor = cos_factor + helps*(math.cos(u*math.pi*((2*i+1)/(16)))*math.cos(v*math.pi*((2*j+1)/(16))))
  64.             imgout.putpixel((u,v),0.25*c_factorx*c_factory*cos_factor)
  65.             print("cos factor: ",cos_factor)
  66.             #cos_factor = 0
  67.  
  68.  
  69. """def cosdom(img):
  70.    imgout = img
  71.    (x,y) = img.size
  72.    for i in x:
  73.        for j in y:
  74.            imgout[i,j] ="""
  75.  
  76. def dct(img):#1D dct
  77.     temp = copy.deepcopy(img)
  78.     out = np.zeros(len(temp), dtype=float)
  79.     for i in range (len(temp)):
  80.         cos_factor = 0
  81.         for j in range(0,len(temp)):
  82.             cos_factor += temp[j]*math.cos(math.pi/len(temp)*(j + 0.5)*i)
  83.         out[i] = cos_factor
  84.  
  85.     out[0] *=1/math.sqrt(2)
  86.     out *= math.sqrt(float(2)/len(img))
  87.     return out
  88.  
  89. def idct(img):
  90.     temp = copy.deepcopy(img)
  91.     out = np.zeros(len(img), dtype=float)
  92.  
  93.     for k in range(len(temp)):
  94.         tot = 0
  95.         first = temp[0]/math.sqrt(len(temp))
  96.         for n in range(1,len(temp)):
  97.             tot += temp[n]*math.cos(math.pi/len(temp)*n*(k+.5))
  98.         tot *= math.sqrt(2.0/len(temp))
  99.         tot += first
  100.         out[k] = tot
  101.  
  102.     return out
  103.  
  104. def dct2d(img):
  105.     imglen = len(img)
  106.     temp = copy.deepcopy(img)
  107.     out2d = np.empty_like(img)
  108.  
  109.     for i in range(0,imglen):
  110.         out2d[i] = dct(temp[i])
  111.  
  112.     for i in range(0,imglen):
  113.         out2d[:,i] = dct(out2d[:,i])
  114.  
  115.     return out2d
  116.  
  117. def idct2d(img):
  118.     temp = copy.deepcopy(img)
  119.     imglen = len(img)
  120.     out2d = np.empty_like(img)
  121.  
  122.     for i in range(0,imglen):
  123.         out2d[i] = idct(temp[i])
  124.  
  125.     for i in range(0,imglen):
  126.         out2d[:,i] = idct(out2d[:,i])
  127.  
  128.     return out2d
  129.  
  130.  
  131. def __transKernel(N):
  132.     A = np.zeros((N,N))
  133.     for x in range(0,N):
  134.         for u in range(0,N):
  135.             if u==0:
  136.                 A[x][u] = math.sqrt(1/float(N))
  137.             else:
  138.                 A[x][u] = math.sqrt(2/float(N))*math.cos(math.pi*(2*x+1)*u/float(2*N))
  139.     return A
  140.  
  141. def __itransKernel(N):
  142.     A = np.zeros((N,N))
  143.     for x in range(0,N):
  144.         for u in range(0,N):
  145.             if x==0:
  146.                 A[x][u] = math.sqrt(1/float(N))
  147.             else:
  148.                 A[x][u] = math.sqrt(2/float(N))*math.cos(math.pi*(2*u+1)*x/float(2*N))
  149.     return A
  150.  
  151. def transform(m):
  152.     h = copy.deepcopy(m)
  153.     tk = __transKernel(len(h))
  154.     t1 = np.dot(h,tk)
  155.     t1 = np.transpose(t1)
  156.     t1 = np.dot(t1,tk)
  157.     return np.transpose(t1)
  158.  
  159. def itransform(m):
  160.     h = copy.deepcopy(m)
  161.     tk = __itransKernel(len(h))
  162.     t1 = np.dot(h,tk)
  163.     t1 = np.transpose(t1)
  164.     t1 = np.dot(t1,tk)
  165.     return np.transpose(t1)
  166.  
  167.  
  168. def subImages(arr, nrows, ncols):
  169.     h, w = arr.shape
  170.     return (arr.reshape(h//nrows, nrows, -1, ncols)
  171.                .swapaxes(1,2)
  172.                .reshape(-1, nrows, ncols))
  173.  
  174. def fromSubImages(arr, h, w):
  175.     n, nrows, ncols = arr.shape
  176.     return (arr.reshape(h//nrows, -1, nrows, ncols)
  177.                .swapaxes(1,2)
  178.                .reshape(h, w))
  179.  
  180. def centerValue(arr,val,n,m):
  181.     arr2 = copy.deepcopy(arr)
  182.     for i in range(n):
  183.         for j in range(m):
  184.             arr2[i][j] -= val
  185.     return arr2
  186.  
  187. def main():
  188.     #Load image and convert into correct format
  189.     before = Image.open("C:\\Users\\Mike\\Google Drive\\School\\DCT Image Compression Project\\JPEG DCT Compressor Sample\\beforesquare.bmp")
  190.     before.show()
  191.     before.convert("YCbCr")
  192.     x, y = before.size
  193.     q = [[16,11,10,16,24,40,51,61],
  194.          [12,12,14,19,26,58,60,55],
  195.          [14,13,16,24,40,57,69,56],
  196.          [14,17,22,29,51,87,80,62],
  197.          [18,22,37,56,68,109,103,77],
  198.          [24,35,55,64,81,104,113,92],
  199.          [49,64,78,87,103,121,120,101],
  200.          [72,92,95,98,112,100,103,99]]
  201.  
  202.     #Split image into Y, Cb, Cr components
  203.     Y, Cb, Cr =  before.split()
  204.  
  205.  
  206.     #Transform elements into matrices from Image for calculations
  207.     Yarry = copy.deepcopy(np.array(Y))
  208.     Cbarry = np.array(Cb)
  209.     Crarry = np.array(Cr)
  210.  
  211.     """temp = subImages(Yarry,8,8)
  212.    for i in range(len(temp)):
  213.        temp[i] = centerValue(temp[i],128,8,8)
  214.    for i in range(len(temp)):
  215.        temp[i] = centerValue(temp[i],-128,8,8)
  216.  
  217.    temp2 = fromSubImages(temp,y,x)   #Testing reconstruction
  218.    Image.fromarray(temp2).show()"""
  219.  
  220.     print(Yarry)
  221.  
  222.     #create other 8x8 matrices to hold the DCT values
  223.     Yarrydct = copy.deepcopy(Yarry)
  224.     Cbarrydct = Cbarry
  225.     Crarrydct = Crarry
  226.  
  227.     #Split into 16x16 neighborhoods for sub-sampling and DCT computation.
  228.     Ysub16 = subImages(Yarry,1024,1024)
  229.     Cbsub16 = subImages(Cbarry,8,8)
  230.     Crsub16 = subImages(Crarry,8,8)
  231.  
  232.     #Create other 16x16 matrices for DCT output.
  233.     Ysub16dct = copy.deepcopy(Ysub16)
  234.     Cbsub16dct = subImages(Cbarry,8,8)
  235.     Crsub16dct = subImages(Crarry,8,8)
  236.  
  237.     #Test code. This is messy.
  238.     print("LENGTH OF SUB ARRAY",len(Ysub16))
  239.  
  240.     dctArr = np.empty_like(Ysub16)
  241.  
  242.     for i in range(len(Ysub16)):
  243.         print("i is",i)
  244.         idctArr = transform(Ysub16[i])
  245.         Image.fromarray(idctArr).show()#correct image
  246.         #Image.fromarray(idctArr).show()#incorrect
  247.         dctArr[i] = numpy.empty_like(idctArr)
  248.         dctArr[i][:] = idctArr
  249.  
  250.         #print("Type of dctArr[i] is {}, type of idctArr is {}.".format(type(dctArr[i]), type(idctArr)))
  251.         Image.fromarray(dctArr[i]).show()
  252.  
  253.         if (dctArr[i]==idctArr).all():
  254.             print("what?")
  255.         else:
  256.             print("Now what?")
  257.         #Image.fromarray(arr[0]).show()
  258.         #Image.fromarray(Ysub16[0]).show()
  259.  
  260.  
  261.  
  262.  
  263.  
  264.     #Fuse the sub-matrices together
  265.     Yarrydct = fromSubImages(dctArr, y, x)
  266.     Yarrypost = fromSubImages(idctArr, y, x)
  267.  
  268.     Image.fromarray(Yarrypost).show()
  269.     Image.fromarray(Yarrydct).show()
  270.  
  271.     #Cbarrydct = fromSubImages(Cbsub16dct, x, y)
  272.     #Crarrydct = fromSubImages(Crsub16dct, x, y)
  273.  
  274.  
  275.  
  276.     #c = np.divide(Yarrydct,q)
  277.  
  278.     #print(c)
  279.  
  280.     #x = transform(Yarrypost)
  281.     #Image.fromarray(x).show()
  282.     #y = itransform(x)
  283.     #Image.fromarray(y).show()
  284.     #Image.fromarray(Yarrydct).show()######THIS WORKS FOR A NxN PHOTO Loop is proven to be loopy!!!!!!
  285.     #Image.fromarray(Yarrypost).show()
  286.  
  287.     """Y.show()
  288.    Cb.show()
  289.    Cr.show()
  290.    (x,y) = before.size
  291.    x_iterations = math.floor(x/8)
  292.    y_iterations = math.floor(y/8)
  293.    for i in range(0,x):
  294.        for j in range(0,y):
  295.            Y.putpixel((i,j),Y.getpixel((i,j))-128)
  296.            Cb.putpixel((i,j),Y.getpixel((i,j))-128)
  297.            Cr.putpixel((i,j),Y.getpixel((i,j))-128)"""
  298.  
  299. if __name__ == "__main__":
  300.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement