Advertisement
Guest User

Untitled

a guest
May 29th, 2015
267
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.07 KB | None | 0 0
  1. #!/usr/bin/python
  2. import cv2
  3. import numpy as np
  4. from matplotlib import pyplot as plt
  5. import matplotlib.cm as cm
  6. import random
  7. import sys
  8. # from PIL import Image
  9. PATH = 'lab_6_images'
  10.  
  11. def BGR2RGB(img):
  12. res = np.zeros(img.shape , np.uint8)
  13. res[:,:,0]=img[:,:,2]
  14. res[:,:,1]=img[:,:,1]
  15. res[:,:,2]=img[:,:,0]
  16. return res
  17.  
  18. def process(img1):
  19. QY=np.array([[16,11,10,16,24,40,51,61]
  20. ,[12,12,14,19,26,48,60,55]
  21. ,[14,13,16,24,40,57,69,56]
  22. ,[14,17,22,29,51,87,80,62]
  23. ,[18,22,37,56,68,109,103,77]
  24. ,[24,35,55,64,81,104,113,92]
  25. ,[49,64,78,87,103,121,120,101]
  26. ,[72,92,95,98,112,100,103,99]])
  27.  
  28. QC=np.array([[17,18,24,47,99,99,99,99]
  29. ,[18,21,26,66,99,99,99,99]
  30. ,[24,26,56,99,99,99,99,99]
  31. ,[47,66,99,99,99,99,99,99]
  32. ,[99,99,99,99,99,99,99,99]
  33. ,[99,99,99,99,99,99,99,99]
  34. ,[99,99,99,99,99,99,99,99]
  35. ,[99,99,99,99,99,99,99,99]])
  36.  
  37. B=8 # blocksize
  38.  
  39. h,w=np.array(img1.shape[:2])/B * B
  40. img1=img1[:h,:w]
  41.  
  42. img2 = BGR2RGB(img1)
  43. # plt.imshow(img2)
  44.  
  45. #convert to YCrCb
  46. transcol=cv2.cvtColor(img1, cv2.cv.CV_BGR2YCrCb)
  47.  
  48. #sampling
  49. SSV=2
  50. SSH=2
  51. crs=cv2.boxFilter(transcol[:,:,1],ddepth=-1,ksize=(2,2))
  52. cbs=cv2.boxFilter(transcol[:,:,2],ddepth=-1,ksize=(2,2))
  53.  
  54. imSub=[transcol[:,:,0],crs[::SSV,::SSH],cbs[::SSV,::SSH]]
  55.  
  56. Qf=1
  57. if Qf < 50 and Qf > 1:
  58. scale = np.floor(5000/Qf)
  59. elif Qf < 100:
  60. scale = 200-2*Qf
  61. else:
  62. raise Exception('wat?')
  63.  
  64. scale=scale/100.0
  65.  
  66.  
  67. Q=[QY*scale,QC*scale,QC*scale]
  68. TransAll=[]
  69. TransAllFiltred=[]
  70. TransAllQuant=[]
  71. TransAllQuantFiltred=[]
  72.  
  73.  
  74. for idx,channel in enumerate(imSub):
  75. channelrows=channel.shape[0]
  76. channelcols=channel.shape[1]
  77. Trans = np.zeros((channelrows,channelcols), np.float32)
  78. TransQuant = np.zeros((channelrows,channelcols), np.float32)
  79. blocksV=channelrows/B
  80. blocksH=channelcols/B
  81. vis0 = np.zeros((channelrows,channelcols), np.float32)
  82. vis0[:channelrows, :channelcols] = channel
  83. vis0=vis0-128
  84. for row in xrange(blocksV):
  85. for col in xrange(blocksH):
  86. currentblock = cv2.dct(vis0[row*B:(row+1)*B,col*B:(col+1)*B])
  87. Trans[row*B:(row+1)*B,col*B:(col+1)*B]=currentblock
  88. TransQuant[row*B:(row+1)*B,col*B:(col+1)*B]=np.round(currentblock/Q[idx])
  89. TransAll.append(Trans)
  90. TransAllQuant.append(TransQuant)
  91.  
  92. tq1 = TransQuant[TransQuant!=0]
  93. tq2 = tq1[tq1!=1]
  94. tq3 = tq2[tq2!=-1]
  95.  
  96.  
  97. TransAllQuantFiltred.append(tq3)
  98. TransAllFiltred.append(Trans[(Trans!=0)&(Trans!=1)&(Trans!=-1)])
  99.  
  100. return TransAllQuantFiltred
  101.  
  102. def test(path, bins, cfactor):
  103. img = cv2.imread(path, cv2.CV_LOAD_IMAGE_UNCHANGED)
  104. img2 = img[cfactor:len(img)-cfactor, cfactor:len(img[0])-cfactor]
  105. # img2 = cv2.GaussianBlur(img2,(3,3),0)
  106. # img = cv2.GaussianBlur(img,(3,3),0)
  107. t1 = process(img)
  108. t2 = process(img2)
  109.  
  110. plt.figure(path+'0')
  111.  
  112. plt.hist(t1[0], bins, alpha=0.75)
  113. plt.hist(t2[0], bins, alpha=0.75)
  114. # plt.figure(path+'1')
  115. # plt.hist(t1[1], bins, alpha=0.75)
  116. # plt.hist(t2[1], bins, alpha=0.75)
  117.  
  118. # plt.figure(path+'2')
  119. # plt.hist(t1[2], bins, alpha=0.75)
  120. # plt.hist(t2[2], bins, alpha=0.75)
  121. plt.show()
  122.  
  123. def main(filename):
  124. bins = 240
  125. cfactor = 5
  126. test(filename, bins, cfactor)
  127.  
  128. if __name__ == '__main__':
  129. print(sys.argv)
  130. main(sys.argv[1])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement