Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- __author__ = 'Mike'
- from PIL import Image, ImageFilter
- import sys, math, numpy, copy
- import numpy as np
- #testing Images
- #kels = Image.open("C:\\Users\\Mike\\Pictures\\cell pics\\Kelsey test.jpg")
- #kblur = kels.filter(ImageFilter.BLUR)
- #kels.show()
- #kblur.show()
- def frequency_domain(img):
- (x,y) = img.size
- imgout = img
- c_factorx = 0
- c_factory = 0
- cos_factor = 0.0
- for u in range(0,x):
- if(u is 0):
- c_factorx = 1/(math.sqrt(2))
- else:
- c_factorx = 1
- for v in range(0,y):
- if(v is 0):
- c_factory = 1/(math.sqrt(2))
- else:
- c_factory = 1
- for i in range(0,x):
- for j in range(0,y):
- helps = float(img.getpixel((i,j)))
- 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))))
- imgout.putpixel((u,v),0.25*c_factorx*c_factory*cos_factor)
- print("cos factor: ",cos_factor)
- cos_factor = 0
- return imgout
- def limitedWindow(img,xmin,ymin):
- (x,y) = img.size
- imgout = img
- c_factorx = 0
- c_factory = 0
- cos_factor = 0.0
- for u in range(xmin,xmin+8):
- if(u is 0):
- c_factorx = 1/(math.sqrt(2))
- else:
- c_factorx = 1
- for v in range(ymin,y+8):
- if(v is 0):
- c_factory = 1/(math.sqrt(2))
- else:
- c_factory = 1
- for i in range(0,8):
- for j in range(0,8):
- helps = float(img.getpixel((u,v)))
- cos_factor = cos_factor + helps*(math.cos(u*math.pi*((2*i+1)/(16)))*math.cos(v*math.pi*((2*j+1)/(16))))
- imgout.putpixel((u,v),0.25*c_factorx*c_factory*cos_factor)
- print("cos factor: ",cos_factor)
- #cos_factor = 0
- """def cosdom(img):
- imgout = img
- (x,y) = img.size
- for i in x:
- for j in y:
- imgout[i,j] ="""
- def dct(img):#1D dct
- temp = copy.deepcopy(img)
- out = np.zeros(len(temp), dtype=float)
- for i in range (len(temp)):
- cos_factor = 0
- for j in range(0,len(temp)):
- cos_factor += temp[j]*math.cos(math.pi/len(temp)*(j + 0.5)*i)
- out[i] = cos_factor
- out[0] *=1/math.sqrt(2)
- out *= math.sqrt(float(2)/len(img))
- return out
- def idct(img):
- temp = copy.deepcopy(img)
- out = np.zeros(len(img), dtype=float)
- for k in range(len(temp)):
- tot = 0
- first = temp[0]/math.sqrt(len(temp))
- for n in range(1,len(temp)):
- tot += temp[n]*math.cos(math.pi/len(temp)*n*(k+.5))
- tot *= math.sqrt(2.0/len(temp))
- tot += first
- out[k] = tot
- return out
- def dct2d(img):
- imglen = len(img)
- temp = copy.deepcopy(img)
- out2d = np.empty_like(img)
- for i in range(0,imglen):
- out2d[i] = dct(temp[i])
- for i in range(0,imglen):
- out2d[:,i] = dct(out2d[:,i])
- return out2d
- def idct2d(img):
- temp = copy.deepcopy(img)
- imglen = len(img)
- out2d = np.empty_like(img)
- for i in range(0,imglen):
- out2d[i] = idct(temp[i])
- for i in range(0,imglen):
- out2d[:,i] = idct(out2d[:,i])
- return out2d
- def __transKernel(N):
- A = np.zeros((N,N))
- for x in range(0,N):
- for u in range(0,N):
- if u==0:
- A[x][u] = math.sqrt(1/float(N))
- else:
- A[x][u] = math.sqrt(2/float(N))*math.cos(math.pi*(2*x+1)*u/float(2*N))
- return A
- def __itransKernel(N):
- A = np.zeros((N,N))
- for x in range(0,N):
- for u in range(0,N):
- if x==0:
- A[x][u] = math.sqrt(1/float(N))
- else:
- A[x][u] = math.sqrt(2/float(N))*math.cos(math.pi*(2*u+1)*x/float(2*N))
- return A
- def transform(m):
- h = copy.deepcopy(m)
- tk = __transKernel(len(h))
- t1 = np.dot(h,tk)
- t1 = np.transpose(t1)
- t1 = np.dot(t1,tk)
- return np.transpose(t1)
- def itransform(m):
- h = copy.deepcopy(m)
- tk = __itransKernel(len(h))
- t1 = np.dot(h,tk)
- t1 = np.transpose(t1)
- t1 = np.dot(t1,tk)
- return np.transpose(t1)
- def subImages(arr, nrows, ncols):
- h, w = arr.shape
- return (arr.reshape(h//nrows, nrows, -1, ncols)
- .swapaxes(1,2)
- .reshape(-1, nrows, ncols))
- def fromSubImages(arr, h, w):
- n, nrows, ncols = arr.shape
- return (arr.reshape(h//nrows, -1, nrows, ncols)
- .swapaxes(1,2)
- .reshape(h, w))
- def centerValue(arr,val,n,m):
- arr2 = copy.deepcopy(arr)
- for i in range(n):
- for j in range(m):
- arr2[i][j] -= val
- return arr2
- def main():
- #Load image and convert into correct format
- before = Image.open("C:\\Users\\Mike\\Google Drive\\School\\DCT Image Compression Project\\JPEG DCT Compressor Sample\\beforesquare.bmp")
- before.show()
- before.convert("YCbCr")
- x, y = before.size
- q = [[16,11,10,16,24,40,51,61],
- [12,12,14,19,26,58,60,55],
- [14,13,16,24,40,57,69,56],
- [14,17,22,29,51,87,80,62],
- [18,22,37,56,68,109,103,77],
- [24,35,55,64,81,104,113,92],
- [49,64,78,87,103,121,120,101],
- [72,92,95,98,112,100,103,99]]
- #Split image into Y, Cb, Cr components
- Y, Cb, Cr = before.split()
- #Transform elements into matrices from Image for calculations
- Yarry = copy.deepcopy(np.array(Y))
- Cbarry = np.array(Cb)
- Crarry = np.array(Cr)
- """temp = subImages(Yarry,8,8)
- for i in range(len(temp)):
- temp[i] = centerValue(temp[i],128,8,8)
- for i in range(len(temp)):
- temp[i] = centerValue(temp[i],-128,8,8)
- temp2 = fromSubImages(temp,y,x) #Testing reconstruction
- Image.fromarray(temp2).show()"""
- print(Yarry)
- #create other 8x8 matrices to hold the DCT values
- Yarrydct = copy.deepcopy(Yarry)
- Cbarrydct = Cbarry
- Crarrydct = Crarry
- #Split into 16x16 neighborhoods for sub-sampling and DCT computation.
- Ysub16 = subImages(Yarry,1024,1024)
- Cbsub16 = subImages(Cbarry,8,8)
- Crsub16 = subImages(Crarry,8,8)
- #Create other 16x16 matrices for DCT output.
- Ysub16dct = copy.deepcopy(Ysub16)
- Cbsub16dct = subImages(Cbarry,8,8)
- Crsub16dct = subImages(Crarry,8,8)
- #Test code. This is messy.
- print("LENGTH OF SUB ARRAY",len(Ysub16))
- dctArr = np.empty_like(Ysub16)
- for i in range(len(Ysub16)):
- print("i is",i)
- idctArr = transform(Ysub16[i])
- Image.fromarray(idctArr).show()#correct image
- #Image.fromarray(idctArr).show()#incorrect
- dctArr[i] = numpy.empty_like(idctArr)
- dctArr[i][:] = idctArr
- #print("Type of dctArr[i] is {}, type of idctArr is {}.".format(type(dctArr[i]), type(idctArr)))
- Image.fromarray(dctArr[i]).show()
- if (dctArr[i]==idctArr).all():
- print("what?")
- else:
- print("Now what?")
- #Image.fromarray(arr[0]).show()
- #Image.fromarray(Ysub16[0]).show()
- #Fuse the sub-matrices together
- Yarrydct = fromSubImages(dctArr, y, x)
- Yarrypost = fromSubImages(idctArr, y, x)
- Image.fromarray(Yarrypost).show()
- Image.fromarray(Yarrydct).show()
- #Cbarrydct = fromSubImages(Cbsub16dct, x, y)
- #Crarrydct = fromSubImages(Crsub16dct, x, y)
- #c = np.divide(Yarrydct,q)
- #print(c)
- #x = transform(Yarrypost)
- #Image.fromarray(x).show()
- #y = itransform(x)
- #Image.fromarray(y).show()
- #Image.fromarray(Yarrydct).show()######THIS WORKS FOR A NxN PHOTO Loop is proven to be loopy!!!!!!
- #Image.fromarray(Yarrypost).show()
- """Y.show()
- Cb.show()
- Cr.show()
- (x,y) = before.size
- x_iterations = math.floor(x/8)
- y_iterations = math.floor(y/8)
- for i in range(0,x):
- for j in range(0,y):
- Y.putpixel((i,j),Y.getpixel((i,j))-128)
- Cb.putpixel((i,j),Y.getpixel((i,j))-128)
- Cr.putpixel((i,j),Y.getpixel((i,j))-128)"""
- if __name__ == "__main__":
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement