konchin_shih

07/20 18:30

Jul 20th, 2021 (edited)
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.52 KB | None | 0 0
  1. import numpy, math, cv2, pandas, seaborn
  2. from matplotlib import pyplot
  3.  
  4. d = cv2.imread('1.jpg', cv2.IMREAD_GRAYSCALE)
  5.  
  6. #基本資料
  7. print("basic information of picture:")
  8. print("xsize =", d.shape[1])
  9. print("ysize =", d.shape[0])
  10.  
  11.  
  12. ##高階處理
  13. fd = d.tolist()
  14.  
  15. def toGraph(originalGraph, shrinkRate):
  16.  
  17.     xSize = len(originalGraph)
  18.     ySize = len(originalGraph[0])
  19.     fixed_xSize = xSize // shrinkRate
  20.     fixed_ySize = ySize // shrinkRate
  21.     fixed_graph = numpy.zeros((fixed_xSize, fixed_ySize))
  22.  
  23.     for xi in range(fixed_xSize):
  24.         for yi in range(fixed_ySize):
  25.             for xj in range(shrinkRate):
  26.                 for yj in range(shrinkRate):
  27.                     fixed_graph[xi][yi] += originalGraph[xi * shrinkRate + xj][yi * shrinkRate + yj]
  28.             fixed_graph[xi][yi] //= shrinkRate * shrinkRate
  29.  
  30.     fixed_result = numpy.array(pandas.value_counts(fixed_graph.flatten()))
  31.     returnVar = numpy.arange(1, len(fixed_result) + 1)
  32.  
  33.     pyplot.figure(figsize=(15, 5))
  34.     pyplot.subplot(1, 2, 1)
  35.     pyplot.plot(returnVar, fixed_result)
  36.     pyplot.title(f"{shrinkRate} * {shrinkRate}")
  37.     pyplot.ylabel('appear time')
  38.     pyplot.xlabel('rank')
  39.  
  40.     pyplot.subplot(1, 2, 2)
  41.     pyplot.plot(returnVar, fixed_result)
  42.     pyplot.xscale('log')
  43.     pyplot.yscale('log')
  44.     pyplot.title(f"{shrinkRate} * {shrinkRate}")
  45.     pyplot.ylabel('appear time(log_10)')
  46.     pyplot.xlabel('rank(log_10)')
  47.  
  48.     return [returnVar, fixed_result]
  49.  
  50. fixed_resultList = []
  51. for i in range(1, 10 + 1):
  52.     fixed_resultList.append(toGraph(fd, i))
  53.  
  54.  
  55. #畫總圖
  56.  
  57. pyplot.figure(figsize=(15, 5))
  58. pyplot.subplot(1, 2, 1)
  59. for i in fixed_resultList:
  60.     pyplot.plot(i[0], i[1])
  61. pyplot.title('total')
  62. pyplot.ylabel('appear time')
  63. pyplot.xlabel('rank')
  64.  
  65.  
  66. pyplot.subplot(1, 2, 2)
  67. for i in fixed_resultList:
  68.     pyplot.plot(i[0], i[1])
  69. pyplot.xscale('log')
  70. pyplot.yscale('log')
  71. pyplot.title('total')
  72. pyplot.ylabel('appear time(log_10)')
  73. pyplot.xlabel('rank(log_10)')
  74.  
  75.  
  76. #分析圖
  77.  
  78. logarithmic_resultList = [] #[x, y]
  79. for i in fixed_resultList:
  80.     logarithmic_resultList.append([numpy.log10(i[0]), numpy.log10(i[1])])
  81.  
  82.  
  83. pyplot.figure(figsize=(15, 5))
  84. pyplot.subplot(1, 2, 1)
  85.  
  86. for i in range(10):
  87.     u=seaborn.regplot(logarithmic_resultList[i][0],
  88.                       logarithmic_resultList[i][1],
  89.                       label = f"{i + 1} * {i + 1}")
  90. u.set_title('analyze')
  91. u.set_ylabel('appear time(log_10)')
  92. u.set_xlabel('rank(log_10)')
  93. u.legend(loc = 'upper right')
  94.  
  95. #線條
  96. pyplot.subplot(1, 2, 2)
  97.  
  98. for i in range(10):
  99.     v = seaborn.regplot(logarithmic_resultList[i][0],
  100.                       logarithmic_resultList[i][1],
  101.                       label = f"{i + 1} * {i + 1}",
  102.                       scatter = False)
  103. v.set_title('analyze')
  104. v.set_ylabel('appear time(log_10)')
  105. v.set_xlabel('rank(log_10)')
  106. v.legend(loc = 'upper right')
  107.  
  108. #取數值
  109. pyplot.figure(figsize = (0.001, 0.001))
  110.  
  111.  
  112. axList = []
  113.  
  114. for i in range(10):
  115.     pyplot.subplot(1, 10, i + 1)
  116.     axList.append(seaborn.regplot(logarithmic_resultList[i][0],
  117.                                   logarithmic_resultList[i][1],
  118.                                   scatter = False))
  119.  
  120. slopeList = []
  121.  
  122. for i in range(10):
  123.     slope, intercept = numpy.polyfit(axList[i].get_lines()[0].get_xdata(),
  124.                                      axList[i].get_lines()[0].get_ydata(),
  125.                                      1)
  126.     print(f"slope {i + 1} = {slope}")
  127.     slopeList.append(slope)
  128.  
  129. print('the average of slopes =', sum(slopeList) / 10)
Add Comment
Please, Sign In to add comment