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
   | import cv2  import numpy as np  import mathplotlib.pyplot as plt img=cv2.imread('1.tif')  H=img.shape[0]  W=img.shape[1]  img15=np.zeros((H,W,3),np.uint8)  img15T=np.zeros((H,W,3),np.unit8)  img15T2=np.zeros((H,W,3),np.unit8)  maxPix=0  
  for i in range(H):     for j in range(W):         sum=0         count=0         for m in range(-7,8):            for n in range(-7,8):               if 0<=i+m<H and 0<=j+n<W:                  count+=1                  sum+=img[i+m,j+n,0]         img15[i,j,0]=sum // conut         img15[i,j,1]=img15[i,j,0]         img15[i,j,2]=img15[i,j,0]         if img15[i,j,0]>maxPix:            maxPix=img15[i,j,0]
  for i in range(H):    for j in range(W):       if img15[i,j,0]>maxPix*0.25:          img15T[i,j]=[255,255,255]       if img15[i,j,0]>63:          img15T2[i,j]=[255,255,255]
  plt.subplot(2,2,1)  plt.axis('off')  plt.title('Original image')  plt.imshow(img) 
  plt.subplot(2,2,2) plt.axis('off') plt.title('15*15 smoothing') plt.imshow(img15)
  plt.subplot(2,2,3) plt.axis('off') plt.title('After maxpixel*25% Threshold') plt.imshow(img15T)
  plt.subplot(2,2,4) plt.axis('off') plt.title('After 255*25% Threshold') plt.imshow(img15T2)
  plt.show()
   |