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
|
import cv2 as cv import numpy as np import matplotlib.pyplot as plt
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))
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 main(imgdir): img = cv.imread(imgdir, 0) differ = get_dif(img) get_show(differ)
if __name__ == '__main__': img_dir = '03.png' main(img_dir)
|