Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import numpy, math, cv2, pandas, seaborn
- from matplotlib import pyplot
- d = cv2.imread('1.jpg', cv2.IMREAD_GRAYSCALE)
- #基本資料
- print("basic information of picture:")
- print("xsize =", d.shape[1])
- print("ysize =", d.shape[0])
- ##高階處理
- fd = d.tolist()
- def toGraph(originalGraph, shrinkRate):
- xSize = len(originalGraph)
- ySize = len(originalGraph[0])
- fixed_xSize = xSize // shrinkRate
- fixed_ySize = ySize // shrinkRate
- fixed_graph = numpy.zeros((fixed_xSize, fixed_ySize))
- for xi in range(fixed_xSize):
- for yi in range(fixed_ySize):
- for xj in range(shrinkRate):
- for yj in range(shrinkRate):
- fixed_graph[xi][yi] += originalGraph[xi * shrinkRate + xj][yi * shrinkRate + yj]
- fixed_graph[xi][yi] //= shrinkRate * shrinkRate
- fixed_result = numpy.array(pandas.value_counts(fixed_graph.flatten()))
- returnVar = numpy.arange(1, len(fixed_result) + 1)
- pyplot.figure(figsize=(15, 5))
- pyplot.subplot(1, 2, 1)
- pyplot.plot(returnVar, fixed_result)
- pyplot.title(f"{shrinkRate} * {shrinkRate}")
- pyplot.ylabel('appear time')
- pyplot.xlabel('rank')
- pyplot.subplot(1, 2, 2)
- pyplot.plot(returnVar, fixed_result)
- pyplot.xscale('log')
- pyplot.yscale('log')
- pyplot.title(f"{shrinkRate} * {shrinkRate}")
- pyplot.ylabel('appear time(log_10)')
- pyplot.xlabel('rank(log_10)')
- return [returnVar, fixed_result]
- fixed_resultList = []
- for i in range(1, 10 + 1):
- fixed_resultList.append(toGraph(fd, i))
- #畫總圖
- pyplot.figure(figsize=(15, 5))
- pyplot.subplot(1, 2, 1)
- for i in fixed_resultList:
- pyplot.plot(i[0], i[1])
- pyplot.title('total')
- pyplot.ylabel('appear time')
- pyplot.xlabel('rank')
- pyplot.subplot(1, 2, 2)
- for i in fixed_resultList:
- pyplot.plot(i[0], i[1])
- pyplot.xscale('log')
- pyplot.yscale('log')
- pyplot.title('total')
- pyplot.ylabel('appear time(log_10)')
- pyplot.xlabel('rank(log_10)')
- #分析圖
- logarithmic_resultList = [] #[x, y]
- for i in fixed_resultList:
- logarithmic_resultList.append([numpy.log10(i[0]), numpy.log10(i[1])])
- pyplot.figure(figsize=(15, 5))
- pyplot.subplot(1, 2, 1)
- for i in range(10):
- u=seaborn.regplot(logarithmic_resultList[i][0],
- logarithmic_resultList[i][1],
- label = f"{i + 1} * {i + 1}")
- u.set_title('analyze')
- u.set_ylabel('appear time(log_10)')
- u.set_xlabel('rank(log_10)')
- u.legend(loc = 'upper right')
- #線條
- pyplot.subplot(1, 2, 2)
- for i in range(10):
- v = seaborn.regplot(logarithmic_resultList[i][0],
- logarithmic_resultList[i][1],
- label = f"{i + 1} * {i + 1}",
- scatter = False)
- v.set_title('analyze')
- v.set_ylabel('appear time(log_10)')
- v.set_xlabel('rank(log_10)')
- v.legend(loc = 'upper right')
- #取數值
- pyplot.figure(figsize = (0.001, 0.001))
- axList = []
- for i in range(10):
- pyplot.subplot(1, 10, i + 1)
- axList.append(seaborn.regplot(logarithmic_resultList[i][0],
- logarithmic_resultList[i][1],
- scatter = False))
- slopeList = []
- for i in range(10):
- slope, intercept = numpy.polyfit(axList[i].get_lines()[0].get_xdata(),
- axList[i].get_lines()[0].get_ydata(),
- 1)
- print(f"slope {i + 1} = {slope}")
- slopeList.append(slope)
- print('the average of slopes =', sum(slopeList) / 10)
Add Comment
Please, Sign In to add comment