Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from PIL import Image
- import numpy as np
- import cv2
- def region_of_interest(image):
- h, w = image.shape[:2]
- rect = np.array([[(round(w/2),h),(round(w/2),0),(w,0),(w,h)]])
- mask = np.zeros_like(image)
- cv2.fillPoly(mask, rect, 255)
- masked_image = cv2.bitwise_and(image, mask)
- return masked_image
- image = 'z1.jpg'
- img = cv2.imread(image, 1)
- orig_img = img.copy()
- gray_img = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
- #bilateralny wiecej detekcji, nie poprawil detekcji znaku na odleglosc
- #img = cv2.GaussianBlur(gimg, (5, 5), cv2.BORDER_DEFAULT)
- img = cv2.bilateralFilter(gray_img,7, 10, 10, cv2.BORDER_DEFAULT)
- img = cv2.medianBlur(img, 5)
- img = region_of_interest(img)
- # param1=165, param2=45, minRadius=5, maxRadius=80
- cimg = cv2.cvtColor(gray_img, cv2.COLOR_GRAY2BGR)
- circles = cv2.HoughCircles(img,cv2.HOUGH_GRADIENT, 1, 100, param1=165, param2=50, minRadius=5, maxRadius=80)
- circles_round = np.uint16(np.around(circles))
- detec_circ = []
- padding = 5
- for i in circles_round[0,:]:
- # draw the outer circle
- cv2.circle(cimg,(i[0],i[1]),i[2],(0,255,0),2)
- # draw the center of the circle
- cv2.circle(cimg,(i[0],i[1]),2,(0,0,255),3)
- 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])
- #wyciete obrazy sa zachowane w detec_circ
- #cv2.namedWindow('crap',cv2.WINDOW_NORMAL)
- #cv2.resizeWindow('crap', 500,600)
- #cv2.imshow('crap',cimg)
- #print(circles_round)
- '''print(circles_round[0,0]) #ignore it
- print(circles_round[0,1])
- print(circles_round[0,1,0])
- print(circles_round[0,1,2])
- print(len(circles_round[0]))'''
- #cv2.imshow('test',test[5])
- for x in range (0, len(detec_circ)):
- #cv2.imshow('detec_circ' + str(x),detec_circ[x])
- img = Image.fromarray(detec_circ[x], 'RGB')
- filename = f'{image.split(".")[0]}-wycinek{x}.jpg'
- img = img.resize((256, 256), Image.ANTIALIAS)
- img.save(filename)
- cv2.waitKey(0)
- cv2.destroyAllWindows()
- img = cv2.imread('z1-wycinek0.jpg', cv2.IMREAD_GRAYSCALE)
- img2= cv2.imread('70.png', cv2.IMREAD_GRAYSCALE)
- img = cv2.medianBlur(img, 5)
- img2 = cv2.medianBlur(img2, 5)
- orb = cv2.ORB_create(nfeatures=1500)
- #detekcja ORB zurzywa duzo mocy obliczeniowej, nie nadaje sie do obrazu z kamery
- kp1, des1 = orb.detectAndCompute(img, None)
- kp2, des2 = orb.detectAndCompute(img2, None)
- #sprawdzanie podobienstwa bruteforce
- bf = cv2.BFMatcher(cv2.NORM_HAMMING, crossCheck=True) #hamming dla orb, crosscheck bierze tylko najlepsze podobienstwa dla true
- matches = bf.match(des1, des2)
- matches = sorted(matches, key=lambda x:x.distance) #sortuje wyniki od najmn do najwieks
- for m in matches:
- print(m.distance) #pokazuje jak daleko jestesmy od podobienstwa, im mniej tym lepiej
- avg = sum((m.distance for m in matches)) / len(matches)
- print(f'Wartosc srednia: {avg}')
- imgmatch = cv2.drawMatches(img, kp1, img2, kp2, matches[:], None, flags=2)
- #print(len(matches))
- #img = cv2.drawKeypoints(img, kp1, None)
- cv2.imshow("Image", imgmatch)
- cv2.waitKey(0)
- cv2.destroyAllWindows()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement