自訂SDK

      在〈自訂SDK〉中尚無留言

底下是本人將常用的功能,重新包裝成SDK, 使用者只需import即可使用

from MahalCv import MahalCv as cv
img=cv.read('tiger.jpg')
img=cv.resize(img, 800,600)
img=cv.shift(img, 100,50)
cv.show('tiger', img)
cv.wait()

 

SDK

import platform
from enum import Enum, IntEnum
import cv2
import numpy as np
from PIL import Image, ImageFont, ImageDraw
from PyQt5.QtGui import QImage, QPixmap
import pylab as plt

class Direction(IntEnum):
HORIZONTAL=1
VERTICAL=0
BOTH=-1
class MahalCv():
# 支援讀取中文圖檔
@staticmethod
def read(filePath):
return cv2.imdecode(np.fromfile(filePath, dtype=np.uint8), cv2.IMREAD_UNCHANGED)

# 支援儲存中文圖檔
@staticmethod
def write(img, file):
file_ext = file.splitext(file)[1]
cv2.imencode(file_ext, img)[1].tofile(file)

#顯示圖片
@staticmethod
def show(title, src):
cv2.imshow(title, src)

#等待使用者按任何鍵
@staticmethod
def wait(t=0):
cv2.waitKey(t)
cv2.destroyAllWindows()

# 縮放
@staticmethod
def resize(img , width=None, height=None, scale=1):
if width is None or height is None:
h, w, _ = img.shape
width = int(w * scale)
height = int(h * scale)
return cv2.resize(img, (width, height), interpolation=cv2.INTER_LINEAR)

# 裁切
@staticmethod
def crop(img, p1, p2):
return img[p1[1]:p2[1], p1[0]:p2[0]].copy()

#平移
@staticmethod
def shift(img, x, y):
m = np.float32([[1, 0, x], [0, 1, y]])
h, w, _ = src.shape
return cv2.warpAffine(img, m, (w, h))

#旋轉
@staticmethod
def rotation(img, angle, x=None, y=None, scale=1):
h, w, _ = img.shape
if x is None or y is None:#以中心點旋轉
x=(w-1)/2
y=(h-1)/2
m = cv2.getRotationMatrix2D((x, y), angle, scale)
return cv2.warpAffine(img, m, (w, h))

#鏡射
@staticmethod
def flip(img, direction=Direction.HORIZONTAL):
return cv2.flip(img, direction)

#輸入中文字
@staticmethod
def text(img, str, x, y , size, color):
pil = Image.fromarray(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))
if platform.system() == "Linux":
font = ImageFont.truetype('/usr/share/fonts/truetype/wqy/wqy-zenhei.ttc', size)
else:
font = ImageFont.truetype('simsun.ttc', size)
if not isinstance(str, np.unicode):
str = str.decode('utf-8')
color=color[::-1]
ImageDraw.Draw(pil).text((x, y), str, font=font, fill=color)
return cv2.cvtColor(np.asarray(pil), cv2.COLOR_RGB2BGR)

#畫線
@staticmethod
def line(src, x1, y1, x2, y2, color=(0,0,0), width=1):
cv2.line(src, (x1, y1), (x2, y2), color, width, cv2.LINE_AA)
return src

#畫矩型
@staticmethod
def rect(src, x1, y1, x2, y2, color=(0,0,0), width=-1):
cv2.rectangle(src, (x1, y1), (x2, y2), color, width, cv2.LINE_AA)
return src

#畫正圓
@staticmethod
def circle(src, x, y, r, color=(0,0,0), width=-1):
cv2.circle(src, (x, y), r , color, width, cv2.LINE_AA)
return src

#畫橢圓
@staticmethod
def ellipse(src, x, y, r1, r2, angle=0, startAngle=0, endAngle=360, color=(0,0,0), width=-1):
cv2.ellipse(src, (x, y), (r1, r2) , angle, startAngle, endAngle, color, width, cv2.LINE_AA)
return src

#多邊型
@staticmethod
def polylines(src, points, color=(0,0,0), width=1, isClosed=True):
cv2.polylines(src, pts=[points], isClosed=isClosed, color=color, thickness=width)
return src

#cv to QImage
@staticmethod
def cv2qimage(src):
img = src[..., ::-1].copy()# QImage只能使用深度複製
h, w=img.shape[:2]
img = QImage(img, w, h, w * 3, QImage.Format_RGB888)
return img

# cv to QPixmap
@staticmethod
def cv2pixmap(src):
img = src[..., ::-1].copy()# QImage只能使用深度複製
h, w=img.shape[:2]
img = QImage(img, w, h, w * 3, QImage.Format_RGB888)
pix = QPixmap(img)
return pix
#blur
@staticmethod
def blur(src, w=1, h=1):
return cv2.blur(src, (w, h))

#bgr 2 rgb
@staticmethod
def bgr2rgb(src):
return src[:,:,::-1]

#blur
@staticmethod
def pltshow(src, w=1, h=1, p=1):
a=int(f'{w}{h}{p}')
axes=plt.subplot(a)
axes.set_xticks([])
axes.set_yticks([])
axes.imshow(src)

#sharp
@staticmethod
def sharp(src, kernel=np.array([[0, -1, 0], [-1, 5, -1], [0, -1, 0]], np.float32)):
return cv2.filter2D(src, -1, kernel=kernel)

@staticmethod
def plot(src, row, col, pos, title=None, cmap=None):
axes=plt.subplot(row, col, pos)
axes.set_xticks([])
axes.set_yticks([])
if title is not None:
axes.set_title(title, fontproperties="Simsun")
if cmap is None:
axes.imshow(src)
else:
axes.imshow(src, cmap='gray')

@staticmethod
def canny(src, h1, h2):
gray = cv2.cvtColor(src, cv2.COLOR_BGR2GRAY)
blurred = cv2.GaussianBlur(gray, (5, 5), 0)
return cv2.Canny(blurred, h1, h2)
@staticmethod
def erode(src, kernel=None, iterations=1):
if kernel is None:
kernel=np.ones((3,3), np.uint8)
return cv2.erode(src, kernel=kernel, iterations=iterations)
@staticmethod
def dilate(src, kernel=None, iterations=1):
if kernel is None:
kernel=np.ones((3,3), np.uint8)
return cv2.dilate(src, kernel=kernel, iterations=iterations)

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *