1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
|
import cv2 as cv import numpy as np import matplotlib.pyplot as plt import pandas as pd from mpl_toolkits.mplot3d import Axes3D
import show_3d
def get_1D_2_2D(x): d = {0:[-1, -1], 1:[-1, 0], 2:[-1, 1], 3:[0, 1], 4:[1, 1], 5:[1, 0], 6:[1, -1], 7:[0, -1]} return d[x]
def get_dif(img): raw, col = img.shape dif = np.zeros((raw - 1, col - 1, 8), dtype=int)
for i in range(raw - 1): for j in range(col - 1): for m in range(8): d = get_1D_2_2D(m) if(i + d[0] >= 0 and i + d[0] + 1 <= raw - 1 and j + d[1] >= 0 and j + d[1] + 1 <= col -1): dif[i][j][m] = abs(int(img[i][j]) - int(img[i + d[0]][j + d[1]])) + \ abs(int(img[i][j + 1]) - int(img[i + d[0]][j + d[1] + 1])) + \ abs(int(img[i + 1][j + 1]) - int(img[i + d[0] + 1][j + d[1] + 1])) + \ abs(int(img[i + 1][j]) - int(img[i + d[0] + 1][j + d[1]])) return dif
def drew_graph(array, flag): cnt = np.zeros(100) raw, col, _ = array.shape for i in range(raw): for j in range(col): d = sorted(array[i][j]) if int(d[flag]) < 100: cnt[int(d[flag])] += 1 plt.plot(cnt, color='red') plt.rcParams['axes.unicode_minus'] = False plt.title('max', fontsize=24, color='black') plt.savefig('fenbu.png') plt.show()
def get_show(ary): raw, col, _ = ary.shape imin = np.zeros((raw, col)) for i in range(raw): for j in range(col): d = sorted(ary[i][j]) if d[0] <= 20: imin[i][j] = 255 cv.imwrite('min.png', imin)
imax = np.zeros((raw, col)) for i in range(raw): for j in range(col): d = sorted(ary[i][j]) if d[7] > 60: imax[i][j] = 255 cv.imwrite('max.png', imax)
def get_3d(ary): raw, col, _ = ary.shape minmax = 0 for i in range(raw): for j in range(col): d = sorted(ary[i][j]) if d[0] > minmax: minmax = d[0]
maxmax = 0 for i in range(raw): for j in range(col): d = sorted(ary[i][j]) if d[7] > maxmax: maxmax = d[7] data = np.zeros((int(minmax) + 1, int(maxmax) + 1)) for i in range(raw): for j in range(col): d = sorted(ary[i][j]) data[int(d[0])][int(d[7])] += 1 np.savetxt('data.csv', data, delimiter=',') ''' fig = plt.figure() ax = fig.add_subplot(111, projection='3d') for i in range(int(minmax) + 1): for j in range(int(maxmax) + 1): ax.scatter(i, j, data[i][j], marker='o') plt.savefig('3d.png') plt.show() '''
def main(imgdir): img = cv.imread(imgdir, 0) differ = get_dif(img) get_3d(differ)
if __name__ == '__main__': img_dir = '03.png' main(img_dir)
|