引用包
import cv2 as cv
import numpy as np
import math
from PIL import ImageFont, ImageDraw, Image
两点距离
@staticmethod
def getDist_P2P(point0, point_a):
distance = math.pow((point0[0] - point_a[0]), 2) + math.pow((point0[1] - point_a[1]), 2)
distance = math.sqrt(distance)
return distance
填充孔洞
@staticmethod
def FillHole(img_cv):
im_floodfill = img_cv.copy()
h, w = img_cv.shape[:2]
mask = np.zeros((h + 2, w + 2), np.uint8)
is_break = False
for i in range(im_floodfill.shape[0]):
for j in range(im_floodfill.shape[1]):
if im_floodfill[i][j] == 0:
seedPoint = (i, j)
is_break = True
break
if is_break:
break
cv.floodFill(im_floodfill, mask, seedPoint, 255)
im_floodfill_inv = cv.bitwise_not(im_floodfill)
im_out = img_cv | im_floodfill_inv
return im_out
写入文本(中文)
@staticmethod
def put_string(img_cv, put_str, x, y):
font_path = "msyhbd.ttc"
b, g, r, a = 255, 0, 0, 0
font = ImageFont.truetype(font_path, 18)
img_pil = Image.fromarray(img_cv)
draw = ImageDraw.Draw(img_pil)
draw.text((x, y), put_str, font=font, fill=(b, g, r, a))
img_s = np.asarray(img_pil)
return img_s
图片对比度调整
@staticmethod
def scale_image_max(img_base):
max_value = np.max(img_base)
min_value = np.min(img_base)
img_float = np.array(img_base, dtype=np.float64)
img_scale_float = (img_float - min_value) / (max_value - min_value)
img_scale_255 = img_scale_float * 255.0
img_scale = np.array(img_scale_255, dtype=np.uint8)
return img_scale