SHARE
TWEET

Untitled

Paarzivall Jan 4th, 2019 (edited) 193 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. from skimage import data, img_as_float, img_as_ubyte
  2. from skimage import io, img_as_ubyte, exposure, util, morphology
  3. from skimage.color import rgb2gray, rgb2hsv
  4. import matplotlib.pyplot as plt
  5. import cv2
  6. import numpy as np
  7. import random
  8.  
  9. def show2imgs(im1, im2, title1='Obraz pierwszy', title2='Obraz drugi', size=(10,10)):
  10.    
  11.     import matplotlib.pyplot as plt
  12.    
  13.     f, (ax1, ax2) = plt.subplots(1,2, figsize=size)
  14.     ax1.imshow(im1, cmap='gray')
  15.     ax1.axis('off')
  16.     ax1.set_title(title1)
  17.  
  18.     ax2.imshow(im2, cmap='gray')
  19.     ax2.axis('off')
  20.     ax2.set_title(title2)
  21.     plt.show()
  22.  
  23. #Segmentacja i scalanie obiekt贸w
  24. def segmentuj(img):
  25.     for i in range(img.shape[0]-4):
  26.         for j in range(img.shape[1]-4):
  27.             if img[i][j] != 255.0:
  28.                 if img[i][j] == 0.0:
  29.                     img[i][j] = random.randint(1, 250)
  30.                     if img[i-1][j-1] != 0.0 and img[i-1][j-1] != 255.0:
  31.                         img[i][j] = img[i-1][j-1]
  32.                     elif img[i-1][j] != 0.0 and img[i-1][j] != 255.0:
  33.                         img[i][j] = img[i-1][j]
  34.                     elif img[i-1][j+1] != 0.0 and img[i-1][j+1] != 255.0:
  35.                         img[i][j] = img[i-1][j+1]
  36.                     elif img[i][j-1] != 0.0 and img[i][j-1] != 255.0:
  37.                         img[i][j] = img[i][j-1]
  38.                     elif img[i][j+1] != 0.0 and img[i][j+1] != 255.0:
  39.                         img[i][j] = img[i][j+1]
  40.                     elif img[i+1][j-1] != 0.0 and img[i+1][j-1] != 255.0:
  41.                         img[i][j] = img[i-1][j-1]
  42.                     elif img[i+1][j] != 0.0 and img[i+1][j] != 255.0:
  43.                         img[i][j] = img[i+1][j]
  44.                     elif img[i+1][j+1] != 0.0 and img[i+1][j+1] != 255.0:
  45.                         img[i][j] = img[i+1][j+1]
  46.                     else:
  47.                         continue
  48.                     #scalanie obiekt贸w
  49.                 if img[i][j] != img[i-1][j-1] and img[i-1][j-1] != 255.0:
  50.                     img[i][j] = img[i-1][j-1]
  51.                    
  52.                 elif img[i][j] != img[i-1][j] and img[i-1][j] != 255.0:
  53.                     img[i][j] = img[i-1][j]
  54.                    
  55.                 elif img[i][j] != img[i-1][j+1] and img[i-1][j+1] != 255.0:
  56.                     img[i-1][j+1] = img[i][j]
  57.                    
  58.                 elif img[i][j] != img[i][j-1] and img[i][j-1] != 255.0:
  59.                     img[i][j-1] = img[i][j]
  60.                    
  61.                 elif img[i][j] != img[i][j+1] and img[i][j+1] != 255.0:
  62.                     img[i][j+1] = img[i][j]
  63.                    
  64.                 elif img[i][j] != img[i+1][j-1] and img[i+1][j-1] != 255.0:
  65.                     img[i][j] = img[i+1][j-1]
  66.                    
  67.                 elif img[i][j] != img[i+1][j] and img[i+1][j] != 255.0:
  68.                     img[i][j] = img[i+1][j]
  69.                 elif img[i][j] != img[i+1][j+1] and img[i+1][j+1] != 255.0:
  70.                     img[i][j] = img[i+1][j+1]
  71.                 else:
  72.                     continue
  73.     return img
  74.  
  75.  
  76. def scal(img):
  77.     for i in range(img.shape[0]-4):
  78.         for j in range(img.shape[1]-4):  
  79.             if img[i][j] != img[i-1][j-1] and img[i-1][j-1] != 255:
  80.                 img[i][j] = img[i-1][j-1]
  81.  
  82.             elif img[i][j] != img[i-1][j] and img[i-1][j] != 255:
  83.                 img[i][j] = img[i-1][j]
  84.  
  85.             elif img[i][j] != img[i-1][j+1] and img[i-1][j+1] != 255:
  86.                 img[i-1][j+1] = img[i][j]
  87.  
  88.             elif img[i][j] != img[i][j-1] and img[i][j-1] != 255:
  89.                 img[i][j-1] = img[i][j]
  90.  
  91.             elif img[i][j] != img[i][j+1] and img[i][j+1] != 255:
  92.                 img[i][j+1] = img[i][j]
  93.  
  94.             elif img[i][j] != img[i+1][j-1] and img[i+1][j-1] != 255:
  95.                 img[i][j] = img[i+1][j-1]
  96.  
  97.             elif img[i][j] != img[i+1][j] and img[i+1][j] != 255:
  98.                 img[i][j] = img[i+1][j]
  99.             elif img[i][j] != img[i+1][j+1] and img[i+1][j+1] != 255:
  100.                 img[i][j] = img[i+1][j+1]
  101.             else:
  102.                 continue
  103.                
  104.     return img
  105.  
  106.  
  107. im = io.imread('przeskalowane/0.09v2.jpg')
  108.  
  109. plt.figure(figsize=(15,10))
  110. plt.imshow(im, cmap="gray")
  111. plt.axis('off')
  112. plt.show()
  113.  
  114. th = 150
  115. img = img_as_ubyte(rgb2gray(im))
  116. plt.imshow(img, cmap="gray")
  117. plt.axis('on')
  118. plt.show()
  119.  
  120. th, bimm = cv2.threshold(img, thresh=th, maxval=255, type=cv2.THRESH_OTSU)
  121. mbim = cv2.medianBlur(bimm,  11)
  122. plt.imshow(mbim, cmap="gray")
  123. plt.axis('on')
  124. plt.show()
  125.  
  126. dt = cv2.distanceTransform(mbim, distanceType=cv2.DIST_L2, maskSize=cv2.DIST_MASK_PRECISE)
  127.  
  128. th = 45
  129. th, bim = cv2.threshold(dt, thresh=th, maxval=255, type=cv2.THRESH_BINARY_INV)
  130. show2imgs(bim, dt, title1='Obraz po binaryzacji', title2='Obraz po transformacie odleg艂o艣ciowej', size=(25,25))
  131.  
  132. kernel = np.ones((3,3),np.uint8)
  133. erodeBin = cv2.erode(bim, kernel=kernel, iterations=25)
  134.  
  135. plt.imshow(erodeBin, cmap="hot")
  136. plt.axis('on')
  137. plt.show()
  138.  
  139.  
  140. #Liczenie ilo艣ci obiekt贸w na obrazie
  141. def licz_obiekty(img):
  142.     ile_obiektow = 0
  143.     obiekty = []
  144.     for i in range(img.shape[0]):
  145.         for j in range(img.shape[1]):
  146.             if img[i][j] != 255:
  147.                 obiekty.append(img[i][j])
  148.     ile_obiektow = len(list(set(obiekty)))# zlicza niepowtarzaj膮ce si臋 elementy listy
  149.     return ile_obiektow
  150.  
  151.  
  152. nimg = segmentuj(bim)
  153. tmp = 0
  154. ile = licz_obiekty(nimg)
  155. print(ile)
  156. while tmp != ile:
  157.     print(tmp)
  158.     nimg = scal(nimg)
  159.     nimg = scal(nimg)
  160.     tmp = ile
  161.     ile = licz_obiekty(nimg)
  162.     print("Wykona艂o si臋!")
  163.  
  164. plt.imshow(nimg, cmap="hot")
  165. plt.axis('on')
  166. plt.show()
  167.  
  168. ile_ob = ile
  169.  
  170. print("Obiekt贸w jest:\t", ile_ob)
  171.  
  172.  
  173.  
  174. #od tego momentu pr贸by zliczenia ich nomina艂贸w
  175. nimg = erodeBin = cv2.erode(bim, kernel=kernel, iterations=48)
  176. mbim = cv2.medianBlur(bimm,  9)
  177. dist =  cv2.distanceTransform(mbim, cv2.DIST_L1 , 5)
  178. im2, contours, hierarchy = cv2.findContours(mbim, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
  179. cv2.drawContours(nimg, contours, -1, (0,0,0), 7)
  180. #show2imgs(mbim, nimg, title1='Obraz po binaryzacji', title2='Obraz po transformacie odleg艂o艣ciowej', size=(25,25))
  181.  
  182. plt.imshow(nimg, cmap="gray")
  183. plt.axis('on')
  184. plt.show()
  185. print(contours)
  186.  
  187. kolory = []
  188. jakie_obiekty = []
  189. for i in range(nimg.shape[0]):
  190.     for j in range(nimg.shape[1]):
  191.         if nimg[i][j] != 0:
  192.             if nimg[i][j] != 255:
  193.                 kolory.append(nimg[i][j])
  194. jakie_obiekty = list(set(kolory))
  195. print(jakie_obiekty)
  196.  
  197. pola_nowe = {}
  198. for x in jakie_obiekty:
  199.     ile_pikseli = 0
  200.     for i in range(nimg.shape[0]):
  201.         for j in range(nimg.shape[1]):
  202.             if nimg[i][j] == x:
  203.                 ile_pikseli += 1
  204.     pola_nowe[x] = ile_pikseli
  205.    
  206. for i in pola_nowe:
  207.     print(i, pola_nowe[i])
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
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top