JJJ
JJJ
import numpy as np
from hsvfilter import HsvFilter
from edgefilter import EdgeFilter
class Vision:
# constants
TRACKBAR_WINDOW = "Trackbars"
# properties
needle_img = None
needle_w = 0
needle_h = 0
method = None
# constructor
def __init__(self, needle_img_path, method=cv.TM_CCOEFF_NORMED):
if needle_img_path:
# load the image we're trying to match
# https://docs.opencv.org/4.2.0/d4/da8/group__imgcodecs.html
self.needle_img = cv.imread(needle_img_path, cv.IMREAD_UNCHANGED)
# Get the all the positions from the match result that exceed our threshold
locations = np.where(result >= threshold)
locations = list(zip(*locations[::-1]))
#print(locations)
return rectangles
return points
# given a list of [x, y, w, h] rectangles and a canvas image to draw on, return
an image with
# all of those rectangles drawn
def draw_rectangles(self, haystack_img, rectangles):
# these colors are actually BGR
line_color = (0, 255, 0)
line_type = cv.LINE_4
return haystack_img
# given a list of [x, y] positions and a canvas image to draw on, return an
image with all
# of those click points drawn on as crosshairs
def draw_crosshairs(self, haystack_img, points):
# these colors are actually BGR
marker_color = (255, 0, 255)
marker_type = cv.MARKER_CROSS
return haystack_img
# returns a Canny edge filter object based on the control GUI values
def get_edge_filter_from_controls(self):
# Get current positions of all trackbars
edge_filter = EdgeFilter()
edge_filter.kernelSize = cv.getTrackbarPos('KernelSize',
self.TRACKBAR_WINDOW)
edge_filter.erodeIter = cv.getTrackbarPos('ErodeIter',
self.TRACKBAR_WINDOW)
edge_filter.dilateIter = cv.getTrackbarPos('DilateIter',
self.TRACKBAR_WINDOW)
edge_filter.canny1 = cv.getTrackbarPos('Canny1', self.TRACKBAR_WINDOW)
edge_filter.canny2 = cv.getTrackbarPos('Canny2', self.TRACKBAR_WINDOW)
return edge_filter
# given an image and an HSV filter, apply the filter and return the resulting
image.
# if a filter is not supplied, the control GUI trackbars will be used
def apply_hsv_filter(self, original_image, hsv_filter=None):
# convert image to HSV
hsv = cv.cvtColor(original_image, cv.COLOR_BGR2HSV)
# if we haven't been given a defined filter, use the filter values from the
GUI
if not hsv_filter:
hsv_filter = self.get_hsv_filter_from_controls()
return img
# given an image and a Canny edge filter, apply the filter and return the
resulting image.
# if a filter is not supplied, the control GUI trackbars will be used
def apply_edge_filter(self, original_image, edge_filter=None):
# if we haven't been given a defined filter, use the filter values from the
GUI
if not edge_filter:
edge_filter = self.get_edge_filter_from_controls()
return img
FLANN_INDEX_LSH = 6
index_params = dict(algorithm=FLANN_INDEX_LSH,
table_number=6,
key_size=12,
multi_probe_level=1)
search_params = dict(checks=50)
try:
flann = cv.FlannBasedMatcher(index_params, search_params)
matches = flann.knnMatch(descriptors_needle, descriptors_haystack, k=2)
except cv.error:
return None, None, [], [], None