imutils--图像处理工具包

2019-07-21  本文已影响0人  DayDayUp_hhxx

在opencv基础上对一些方法进行了再次加工,使这些方法更加简单易用,包括 translation, rotation, resizing, skeletonization, and displaying Matplotlib images 等。
github主页: https://github.com/jrosebr1/imutils

安装

pip install imutils

查询opencv中的函数

可以使用关键词搜索opencv中的相应函数

import imutils
imutils.find_function("area")
#output:
1. CC_STAT_AREA
2. INTER_AREA
3. contourArea
4. minAreaRect

图像平移Translation

图像在x轴方向左右平移,y轴方向上下平移,

#向右平移25像素,向上平移75像素
translated = imutils.translate(image,25,-75)

图像旋转Rotation

rotated = imutils.rotate(image,90)

图像大小Resizing

改变图像大小,但保持原来图像的长宽比不变。
可以只单独设置width或者height;

resized = imutils.resize(image,width=300)
resized = imutils.resize(image,height=300)

骨架化Skeletonization

gray = cv2.cvtColor(logo, cv2.COLOR_BGR2GRAY)
skeleton = imutils.skeletonize(gray, size=(3, 3))
cv2.imshow("Skeleton", skeleton)

使用Matplotlib展示图片

opencv读取的图片格式为BGR,使用matplotlib显示的时候也是这个顺序,需要先将BGR转换为RGB才能正常显示,imutils提供了转换函数

plt.imshow(imutils.opencv2matplotlib(image))

URL转换为Image

image = imutils.url_to_image(url)

自动边缘检测Automatic Canny Edge Detection

gray = cv2.cvtColor(logo, cv2.COLOR_BGR2GRAY)
edgeMap = imutils.auto_canny(gray)
#源码
def auto_canny(image, sigma=0.33):
    # compute the median of the single channel pixel intensities
    v = np.median(image)
    # apply automatic Canny edge detection using the computed median
    lower = int(max(0, (1.0 - sigma) * v))
    upper = int(min(255, (1.0 + sigma) * v))
    edged = cv2.Canny(image, lower, upper)
    # return the edged image
    return edged

返回图片列表

from imutils import paths
for imagePath in paths.list_images("../demo_images"):
    print imagePath
上一篇下一篇

猜你喜欢

热点阅读