Files
ocv_template_matching/util.py
T
2024-07-04 18:57:32 +02:00

57 lines
1007 B
Python

import cv2
import numpy as np
(major_ver, minor_ver, subminor_ver) = cv2.__version__.split('.')
print(cv2.__version__)
def to_rect(bbox):
top_left = (bbox[0], bbox[1])
bottom_right = (top_left[0] + bbox[2], top_left[1] + bbox[3])
return top_left, bottom_right
def bbox_add_position(bbox, bbox_add):
res = (bbox[0] + bbox_add[0], bbox[1] + bbox_add[1], bbox[2], bbox[3])
return res
def bbox_round(src):
res = ()
for s in src:
res += (int(round(s)),)
return res
def bbox_center(bbox):
x = bbox[0] + bbox[2]/2
y = bbox[1] + bbox[3]/2
return x, y
def bbox_extend(bbox: np.array, factor: float = 0):
we2 = bbox[2]*factor/2
he2 = bbox[3]*factor/2
return bbox_round((bbox[0]-we2, bbox[1]-he2, bbox[2]+2*we2, bbox[3]+2*he2))
def image_crop(src, bbox: np.array):
x = int(bbox[0])
y = int(bbox[1])
w = int(bbox[2])
h = int(bbox[3])
dst = src[y:y+h, x:x+w]
return dst
def process_image_bbox(src):
# convert to gray
gray = cv2.cvtColor(src, cv2.COLOR_BGR2GRAY)
return gray