SHARE
TWEET

Untitled

a guest May 26th, 2019 79 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. from PIL import Image
  2. import numpy as np
  3. import cv2
  4.  
  5. def region_of_interest(image):
  6.     h, w = image.shape[:2]
  7.     rect = np.array([[(round(w/2),h),(round(w/2),0),(w,0),(w,h)]])
  8.     mask = np.zeros_like(image)
  9.     cv2.fillPoly(mask, rect, 255)
  10.     masked_image = cv2.bitwise_and(image, mask)
  11.     return masked_image
  12.  
  13. image = 'z1.jpg'
  14.  
  15. img = cv2.imread(image, 1)
  16. orig_img = img.copy()
  17. gray_img = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
  18.  
  19. #bilateralny wiecej detekcji, nie poprawil detekcji znaku na odleglosc
  20.  
  21. #img = cv2.GaussianBlur(gimg, (5, 5), cv2.BORDER_DEFAULT)
  22. img = cv2.bilateralFilter(gray_img,7, 10, 10, cv2.BORDER_DEFAULT)
  23. img = cv2.medianBlur(img, 5)
  24.  
  25. img = region_of_interest(img)
  26.  
  27. # param1=165, param2=45, minRadius=5, maxRadius=80
  28. cimg = cv2.cvtColor(gray_img, cv2.COLOR_GRAY2BGR)
  29.  
  30. circles = cv2.HoughCircles(img,cv2.HOUGH_GRADIENT, 1, 100, param1=165, param2=50, minRadius=5, maxRadius=80)
  31. circles_round = np.uint16(np.around(circles))
  32.  
  33. detec_circ = []
  34. padding = 5
  35. for i in circles_round[0,:]:
  36.     # draw the outer circle
  37.     cv2.circle(cimg,(i[0],i[1]),i[2],(0,255,0),2)
  38.     # draw the center of the circle
  39.     cv2.circle(cimg,(i[0],i[1]),2,(0,0,255),3)
  40.     detec_circ.append(orig_img[i[1] - i[2] -padding :i[1] + i[2] + padding, i[0] - i[2] - padding : i[0] + i[2] + padding])
  41.  
  42. #wyciete obrazy sa zachowane w detec_circ
  43.  
  44. #cv2.namedWindow('crap',cv2.WINDOW_NORMAL)
  45. #cv2.resizeWindow('crap', 500,600)
  46. #cv2.imshow('crap',cimg)
  47. #print(circles_round)
  48.  
  49. '''print(circles_round[0,0]) #ignore it
  50. print(circles_round[0,1])
  51. print(circles_round[0,1,0])
  52. print(circles_round[0,1,2])
  53. print(len(circles_round[0]))'''
  54. #cv2.imshow('test',test[5])
  55.  
  56. for x in range (0, len(detec_circ)):
  57.     #cv2.imshow('detec_circ' + str(x),detec_circ[x])
  58.     img = Image.fromarray(detec_circ[x], 'RGB')
  59.     filename = f'{image.split(".")[0]}-wycinek{x}.jpg'
  60.     img = img.resize((256, 256), Image.ANTIALIAS)
  61.     img.save(filename)
  62. cv2.waitKey(0)
  63. cv2.destroyAllWindows()
  64.  
  65. img = cv2.imread('z1-wycinek0.jpg', cv2.IMREAD_GRAYSCALE)
  66. img2= cv2.imread('70.png', cv2.IMREAD_GRAYSCALE)
  67. img = cv2.medianBlur(img, 5)
  68. img2 = cv2.medianBlur(img2, 5)
  69. orb = cv2.ORB_create(nfeatures=1500)
  70.  
  71. #detekcja ORB  zurzywa duzo mocy obliczeniowej, nie nadaje sie do obrazu z kamery
  72. kp1, des1 = orb.detectAndCompute(img, None)
  73. kp2, des2 = orb.detectAndCompute(img2, None)
  74.  
  75. #sprawdzanie podobienstwa bruteforce
  76. bf = cv2.BFMatcher(cv2.NORM_HAMMING, crossCheck=True) #hamming dla orb, crosscheck bierze tylko najlepsze podobienstwa dla true
  77. matches = bf.match(des1, des2)
  78. matches = sorted(matches, key=lambda x:x.distance) #sortuje wyniki od najmn do najwieks
  79.  
  80. for m in matches:
  81.     print(m.distance) #pokazuje jak daleko jestesmy od podobienstwa, im mniej tym lepiej
  82.  
  83. avg = sum((m.distance for m in matches)) / len(matches)
  84. print(f'Wartosc srednia: {avg}')
  85. imgmatch = cv2.drawMatches(img, kp1, img2, kp2, matches[:], None, flags=2)
  86. #print(len(matches))
  87. #img = cv2.drawKeypoints(img, kp1, None)
  88.  
  89. cv2.imshow("Image", imgmatch)
  90. cv2.waitKey(0)
  91. 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