Facebook
From Soiled Finch, 6 Years ago, written in Python.
Embed
Download Paste or View Raw
Hits: 225
  1. # importy
  2. from __future__ import division
  3. from pylab import *
  4. import skimage
  5. from skimage import data, filters, exposure, feature, morphology
  6. from skimage.morphology import square
  7. from skimage.filters import rank
  8. from skimage.util.dtype import convert
  9. from skimage import img_as_float, img_as_ubyte
  10. from matplotlib.pyplot import imshow
  11. import skimage.io as io
  12. from skimage.color import rgb2hsv, hsv2rgb, rgb2gray, gray2rgb
  13. from skimage.filters.edges import convolve
  14. from matplotlib import pylab as plt  
  15. import numpy as np
  16. from numpy import array
  17. from IPython.display import display
  18. from ipywidgets import interact, interactive, fixed
  19. from IPython.core.display import clear_output
  20. import os
  21. import cv2
  22.  
  23. import warnings
  24. warnings.simplefilter("ignore")
  25.  
  26.  
  27. # funkcja do generowania krotki 3-elementowej z randomowym kolorem
  28. def generate_color():
  29.     return (randint(0,255), randint(0,255), randint(0,255))
  30.  
  31.  
  32.  
  33. # Funkcja do znajdywania konturów na pojedynczym obrazku.
  34. # Najpierw stosujemy funkcje do obróbki zdjęcia w celu uwydatnienia krawędzi,
  35. # a następnie szukamy kontury i rysujemy je, jeśli są konturem zewnętrznym i jeśli są większe niż zadana wielkość.
  36. # Dodatkowo obliczamy też centroid każdego konturu.
  37.  
  38. def fun(plane, im, canny_first, canny_second, coef_blur, count_morph, size, width):
  39.    
  40.     imgray = cv2.cvtColor(im,cv2.COLOR_BGR2GRAY)
  41.    
  42.     imgray = cv2.medianBlur(imgray, coef_blur)
  43.     imgray = cv2.Canny(imgray, canny_first, canny_second)
  44.    
  45.     kernel = np.ones((5,5),np.uint8)
  46.     imgray = cv2.dilate(imgray,kernel,iterations = count_morph)
  47.     imgray = cv2.erode(imgray,kernel,iterations = count_morph)
  48.  
  49.     img, contours, hierarchy = cv2.findContours(imgray,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
  50.     hier = hierarchy[0]
  51.    
  52.     moments = []
  53.    
  54.     for i in range(len(hier)):
  55.         if(hier[i][3] == -1):
  56.             if(len(contours[i]) > size):
  57.                 M = cv2.moments(contours[i])
  58.                 moments.append([int(M['m10']/M['m00']), int(M['m01']/M['m00'])])
  59.                 cv2.drawContours(plane, contours, i, generate_color(), width, hierarchy=hierarchy, maxLevel=0)
  60.            
  61.     return plane, moments
  62.  
  63.  
  64.  
  65. # Główna funkcja w programie.
  66. # Wczytuje kolejno każdy plik ze zdjęciem samolotu, wywołuje dla niego funkcję fun() i wyświetla rezultat.
  67. # Dorysowuje również centroidy w punktach zwróconych przez fun().
  68.  
  69. def load(n, col, canny_first, canny_second, coef_blur, count_morph, size, width, marker):
  70.     fig, axes = plt.subplots(nrows=n//col, ncols=col, figsize=(30,7*(n//col)))
  71.     no = 0
  72.     for ax in axes:
  73.         for i in range(col):
  74.             if(no < 10):
  75.                 file =  os.getcwd() + '//planes/samolot0' + str(no) + '.jpg'
  76.             else:
  77.                 file = os.getcwd() + '//planes/samolot' + str(no) + '.jpg'
  78.  
  79.             plane = data.load(file)
  80.             im = cv2.imread(file)
  81.  
  82.             result, moments = fun(plane, im, canny_first, canny_second, coef_blur, count_morph, size, width)
  83.             ax[i].imshow(result, cmap='gray')
  84.             for mom in moments:
  85.                 ax[i].plot([mom[0]], [mom[1]], 'wo', markersize=marker)
  86.             no = no + 1
  87.        
  88.     fig.savefig('planes.pdf')
  89.    
  90.    
  91.    
  92. # Wywołanie głównej funkcji programu z zadanymi parametrami    
  93. load(21, 3, 100, 200, 3, 1, 95, 3, 5)