Files
ocv_template_matching/util.py
T
2024-07-03 22:03:59 +02:00

60 lines
1.0 KiB
Python

import cv2
import sys
import numpy as np
import imutils as im
(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: cv2.typing.Rect, 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: cv2.typing.Rect):
bb = bbox_round(bbox)
x = bb[0]
y = bb[1]
w = bb[2]
h = bb[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