这两种算法在它们可以检测到的和不能检测到的方面都有其起伏。

OpenCV 是用 C++ 在后端进行编程的,并作为一个机器学习包,来分析 Python 中的图像模式。

Skimage 也称为 Scikit-Image ,是一个机器学习软件包,用于图像预处理以发现隐藏模式。

两者的最佳平台

OpenCV 建议在基于服务器的 notebook 上完成,比如 google colab,或者 google cloud、Azure cloud 甚至 IBM 中的 notebook 扩展。

而对于 Skimage 来说,即使是 Jupyter Lab/Notebooks 也能很好地工作,因为它在处理上没有 OpenCV 那么复杂。

使用 Skimage 分析面部数据的 Python 代码

from skimage import data
from skimage.feature import Cascade


import matplotlib.pyplot as plt
from matplotlib import patches


# Load the trained file from the module root.
trained_file = data.lbp_frontal_face_cascade_filename()


# Initialize the detector cascade.
detector = Cascade(trained_file)


img = data.astronaut()


detected = detector.detect_multi_scale(img=img,
scale_factor=1.2,
step_ratio=1,
min_size=(60, 60),
max_size=(90, 500))


plt.imshow(img)
img_desc = plt.gca()
plt.set_cmap('gray')


for patch in detected:


img_desc.add_patch(
patches.Rectangle(
(patch['c'], patch['r']),
patch['width'],
patch['height'],
fill=False,
color='r',
linewidth=2
)
)


plt.show()

【CV】图像分析用 OpenCV 与 Skimage,哪一个更好?_机器学习

# We have detected a face using Skimage in python
# Obtain the segmentation with default 100 regions
segments = slic(img)


# Obtain segmented image using label2rgb
segmented_image = label2rgb(segments, img, kind=’avg’)


# Detect the faces with multi scale method
detected = detector.detect_multi_scale(img=segmented_image,
scale_factor=1.2,
step_ratio=1,
min_size=(10, 10), max_size=(1000, 1000))


# Show the detected faces
show_detected_face(segmented_image, detected)

【CV】图像分析用 OpenCV 与 Skimage,哪一个更好?_python_02

因此我们在这里看到了如何使用 python 中的 Skimage 检测人脸和推断图像。

使用 OpenCV 分析数据的 Python 代码

from google.colab import drive
drive.mount('/content/drive')
image = cv2.imread(r'/content/drive/MyDrive/12-14-2020-tout.jpg')
# check properties of the image
image.shape
# This image has 1333 pxl width, 2000 pxl height and 3 channels(red, green, blue)
from google.colab.patches import cv2_imshow
cv2_imshow(image)

这里我们使用OpenCV上传了一张图片:

【CV】图像分析用 OpenCV 与 Skimage,哪一个更好?_机器学习_03

eye_detector = cv2.CascadeClassifier('/content/drive/MyDrive/haarcascade_frontalcatface.xml')
eye_detections = eye_detector.detectMultiScale(image)
eye_detections
# detect face with eyes on one of the faces
eye_detections = eye_detector.detectMultiScale(image)
for (x,y,w,h) in eye_detections:
cv2.rectangle(image, (x,y), (x+w, y+h), (0,300,0), 2)
cv2_imshow(image)

【CV】图像分析用 OpenCV 与 Skimage,哪一个更好?_opencv_04

在这里,我们使用 OpenCV 中的 Hascade 参数技术检测了其中一张人脸,该技术也可以调整以检测所有人脸。