SHARE
TWEET

Untitled

a guest Mar 26th, 2020 53 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import cv2
  2. import numpy as np
  3. from random import randint
  4. input = cv2.imread('input.jpg',cv2.IMREAD_GRAYSCALE)
  5. h,w = input.shape
  6. img = np.zeros((h,w), np.uint8)
  7. H = np.zeros((256),np.int64)
  8. nr = 0
  9. for i in range(h):
  10.     for j in range(w):
  11.         H[input[i,j]]+=1
  12.         nr += 1
  13. nl = 0
  14. ul = 0.0
  15. ur = 0.0
  16. I = -1
  17. M = -1
  18. for i in range(256):
  19.     ur += i*H[i]
  20. ur /= nr
  21. #print(ur)
  22. for i in range(256):
  23.     ul = ul*nl+H[i]*i
  24.     nl += H[i]
  25.     ul /= nl
  26.     ur = ur*nr-H[i]*i
  27.     #print(i,ur)
  28.     nr -= H[i]
  29.     ur /= nr
  30.     if((ul-ur)*(ul-ur)*nl*nr>M):
  31.         M = (ul-ur)*(ul-ur)*nl*nr
  32.         I = i
  33. #print(H[254]*254)
  34. #    print(i,ul,ur,nl,nr)
  35. #print(I)
  36. for i in range(h):
  37.     for j in range(w):
  38.         if(input[i,j] > I):
  39.             img[i,j] = 255
  40.            
  41.         else:
  42.             img[i,j] = 0
  43.  
  44. cv2.imshow('TEST', img)
  45. cv2.waitKey(0)
  46. cv2.destroyAllWindows()
  47. tmp = np.zeros((h,w), np.uint64)
  48. ct = 1
  49. cp = np.zeros((53218), np.uint64)
  50. top = 0
  51. print(h,w)
  52. F = np.zeros((50000), np.uint64)
  53. def f(x):
  54.     if(F[x]==x):
  55.         return x
  56.     else:
  57.         F[x] = f(F[x])
  58.         return F[x]
  59. def u(x,y):
  60.     A = f(x)
  61.     B = f(y)
  62.     if(A != B):
  63.         F[A] = B
  64. for i in range(50000):
  65.     F[i] = i
  66. for i in range(h):
  67.     for j in range(w):
  68.         if(img[i,j]==255):
  69.             if(i-1>=0 and tmp[i-1,j]!=0):
  70.                 tmp[i,j] = tmp[i-1,j]
  71.                 if(j-1>=0 and tmp[i,j-1]!=0 and tmp[i-1,j]!=tmp[i,j-1]):
  72.                     u(tmp[i,j-1],tmp[i-1,j])             
  73.             elif(j-1>=0 and tmp[i,j-1]!=0):
  74.                 tmp[i,j] = tmp[i,j-1]
  75.             else:
  76.                 tmp[i,j] = ct
  77.                 ct += 1
  78. num =  np.zeros((5000000), np.uint64)
  79. col = np.zeros((3,5000),np.uint64)
  80. top = ct
  81. ct = 1
  82. for i in range(top):
  83.     if(F[i]==i):
  84.         num[i] = ct
  85.         col[0,ct] = randint(0,256)
  86.         col[1,ct] = randint(0,256)
  87.         col[2,ct] = randint(0,256)
  88.         ct += 1
  89. print(top,ct)
  90. output = np.zeros((h,w,3), np.uint8)
  91. for i in range(h):
  92.     for j in range(w):
  93.         tmp[i,j] = num[f(tmp[i][j])]
  94. for i in range(h):
  95.     for j in range(w):
  96.         if(img[i,j]==255):
  97.             output[i,j,0] = col[0,tmp[i,j]]
  98.             output[i,j,1] = col[1,tmp[i,j]]
  99.             output[i,j,2] = col[2,tmp[i,j]]
  100.         else:
  101.             output[i,j,0] = 0
  102.             output[i,j,1] = 0
  103.             output[i,j,2] = 0
  104. cv2.imshow('TEST', output)
  105. cv2.waitKey(0)
  106. cv2.destroyAllWindows()
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top