Facebook
From Subtle Macaque, 6 Years ago, written in Python.
Embed
Download Paste or View Raw
Hits: 231
  1. import cv2
  2. import numpy as np
  3. from matplotlib import pyplot as plt
  4.  
  5.  
  6. def auto_canny(image, sigma=0.33):
  7.     median = np.median(image)
  8.     lower = int(max(0, (1.0 - sigma) * median))
  9.     upper = int(min(255, (1.0 + sigma) * median))
  10.     edged = cv2.Canny(image, lower, upper, L2gradient=True)
  11.     return edged
  12.  
  13. def findPlane(pic):
  14.     img = cv2.imread(pic,0) #0 -w odc szarosci
  15.     edges = cv2.GaussianBlur(img, (3, 3), 0)  #filtr gaussowski
  16.     edges = auto_canny(edges)                  #canny
  17.     edges = cv2.blur(edges, (5, 5))             #blur
  18.     image, contours, hierarchy = cv2.findContours(edges, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)   #pobieram krawedzie
  19.     edges = cv2.drawContours(edges, contours, -1, (255, 255, 255), cv2.FILLED)  #nakladam wypelnione krawedzie
  20.     edges = auto_canny(edges)
  21.     cv2.imwrite("edged-" + pic, edges)  #zapis obrazka
  22.     return edges
  23.  
  24. pics = ['samolot17.jpg','samolot07.jpg','samolot08.jpg','samolot09.jpg','samolot10.jpg','samolot14.jpg']
  25.  
  26. fig = plt.figure(1)
  27. fig.subplots_adjust(wspace=0, hspace=0, top=1, bottom=0, left=0, right=1)
  28. for each in pics:
  29.     ax = fig.add_subplot(3, 2, pics.index(each) + 1)
  30.     new_pic = findPlane(each)
  31.     height, width = new_pic.shape[:2]     #skalowanie dla lepszego rysowania
  32.     max_height = 200
  33.     max_width = 300
  34.     scaling_factor = max_height / float(height)
  35.     new_pic = cv2.resize(new_pic, None, fx=scaling_factor, fy=scaling_factor, interpolation=cv2.INTER_AREA)
  36.     ax.imshow(new_pic, cmap="gray", aspect='auto')
  37.  
  38.  
  39. plt.show()
  40. key = cv2.waitKey()