zhilaizhiwang:imutils--图像处理工具包
来源:简书 发布时间:2020-05-26 14:10:07

在opencv基础上对一些方法进行了再次加工,使这些方法更加简单易用,包括 translation, rotation, resizing, skeletonization, and displaying Matplotlib images 等。

安装

undefined

pip install imutils

查询opencv中的函数

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

undefined

import imutils

imutils.find_functions("area")

#output:

1. CC_STAT_AREA

2. INTER_AREA

3. contourArea

4. minAreaRect

图像平移Translation

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

undefined

#向右平移25像素,向上平移75像素

translated = imutils.translate(image,25,-75)

图像旋转Rotation

undefined

rotated = imutils.rotate(image,90)

图像大小Resizing

改变图像大小,但保持原来图像的长宽比不变。

可以只单独设置width或者height;

undefined

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

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

骨架化Skeletonization

undefined

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提供了转换函数

undefined

plt.imshow(imutils.opencv2matplotlib(image))

URL转换为Image

undefined

image = imutils.url_to_image(url)

自动边缘检测Automatic Canny Edge Detection

undefined

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

返回图片列表

undefined

from imutils import paths

for imagePath in paths.list_images("../demo_images"):

print imagePath

作者:zhilaizhiwang

链接:https://www.jianshu.com/p/bb34ddf2a947

来源:简书

著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

猜你喜欢